6
6
7
7
8
8
class ConfigLoader (BaseLoader ):
9
- """loader for configuration files"""
9
+ """Loader for configuration.
10
+
11
+ :param str data_path: path to the config
12
+
13
+ """
10
14
11
15
def __init__ (self , data_path = None ):
12
16
super (ConfigLoader , self ).__init__ ()
@@ -19,13 +23,15 @@ def parse(string):
19
23
20
24
@staticmethod
21
25
def load_config (file_path , sections ):
22
- """
23
- :param file_path: the path of config file
24
- :param sections: the dict of {section_name(string): Section instance}
25
- Example:
26
+ """Load section(s) of configuration into the ``sections`` provided. No returns.
27
+
28
+ :param str file_path: the path of config file
29
+ :param dict sections: the dict of ``{section_name(string): ConfigSection object}``
30
+ Example::
31
+
26
32
test_args = ConfigSection()
27
33
ConfigLoader("config.cfg", "").load_config("./data_for_tests/config", {"POS_test": test_args})
28
- :return: return nothing, but the value of attributes are saved in sessions
34
+
29
35
"""
30
36
assert isinstance (sections , dict )
31
37
cfg = configparser .ConfigParser ()
@@ -60,9 +66,12 @@ def load_config(file_path, sections):
60
66
61
67
62
68
class ConfigSection (object ):
69
+ """ConfigSection is the data structure storing all key-value pairs in one section in a config file.
70
+
71
+ """
63
72
64
73
def __init__ (self ):
65
- pass
74
+ super ( ConfigSection , self ). __init__ ()
66
75
67
76
def __getitem__ (self , key ):
68
77
"""
@@ -132,25 +141,12 @@ def data(self):
132
141
return self .__dict__
133
142
134
143
135
- if __name__ == "__main__" :
136
- config = ConfigLoader ('there is no data' )
137
-
138
- section = {'General' : ConfigSection (), 'My' : ConfigSection (), 'A' : ConfigSection ()}
139
- """
140
- General and My can be found in config file, so the attr and
141
- value will be updated
142
- A cannot be found in config file, so nothing will be done
143
- """
144
-
145
- config .load_config ("../../test/data_for_tests/config" , section )
146
- for s in section :
147
- print (s )
148
- for attr in section [s ].__dict__ .keys ():
149
- print (s , attr , getattr (section [s ], attr ), type (getattr (section [s ], attr )))
150
-
151
-
152
144
class ConfigSaver (object ):
145
+ """ConfigSaver is used to save config file and solve related conflicts.
146
+
147
+ :param str file_path: path to the config file
153
148
149
+ """
154
150
def __init__ (self , file_path ):
155
151
self .file_path = file_path
156
152
if not os .path .exists (self .file_path ):
@@ -244,9 +240,8 @@ def _write_section(self, sect_list, sect_key_list):
244
240
def save_config_file (self , section_name , section ):
245
241
"""This is the function to be called to change the config file with a single section and its name.
246
242
247
- :param section_name: The name of section what needs to be changed and saved.
248
- :param section: The section with key and value what needs to be changed and saved.
249
- :return:
243
+ :param str section_name: The name of section what needs to be changed and saved.
244
+ :param ConfigSection section: The section with key and value what needs to be changed and saved.
250
245
"""
251
246
section_file = self ._get_section (section_name )
252
247
if len (section_file .__dict__ .keys ()) == 0 : # the section not in the file before
0 commit comments