-
Notifications
You must be signed in to change notification settings - Fork 0
/
configdict.py
365 lines (339 loc) · 11.4 KB
/
configdict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env python
# coding=utf-8
"""
simple package to read and write configs in dict format from/to file.
supports two formats:
json (preferred)
ini (not implemented jet)
history:
see git commits
todo:
~ all fine :-)
"""
import sys
import os
import json
# from configparser import ConfigParser
##########################################
# globals
##########################################
# functions
def merge_deep(obj_1, obj_2):
"""
Merge dicts deeply.
obj_2 overwrittes keys with same values in obj_1.
(if they are dicts its recusive merged.)
"""
# work on obj_1
result = obj_1
# make copy
# result = obj_1.copy()
if isinstance(result, dict) and isinstance(obj_2, dict):
for key in obj_2:
if key in result:
result[key] = merge_deep(result[key], obj_2[key])
else:
# this adds the key to the dict
result[key] = obj_2[key]
else:
result = obj_2
return result
def extend_deep(obj_1, obj_2):
"""
Extend dicts deeply.
extends obj_1 with content of obj_2 if not allready there.
"""
# work on obj_1
if isinstance(obj_1, dict) and isinstance(obj_2, dict):
for key in obj_2:
if key not in obj_1:
# key from obj_2 not found in obj_1
# so add key:
obj_1[key] = obj_2[key]
else:
# key is available.
# test deeply if extension is needed:
extend_deep(obj_1[key], obj_2[key])
else:
pass
# don't overriede!
# obj_1 = obj_2
##########################################
# classes
#
# class ConfigDict:
# """abstract the reading / writing of configuration parameters."""
#
# def __init__(self, config_defaults={}, filename=None):
# """Initialize config to defaults."""
# self.config_defaults = config_defaults
# self.filename = filename
# self.config = {}
# if self.filename is not None:
# if os.path.isfile(filename):
# self.read_from_file()
# else:
# self.config = self.config_defaults.copy()
# self.write_to_file()
# else:
# self.config = self.config_defaults.copy()
#
# def set_filename(self, filename):
# """Set new filename."""
# self.filename = filename
#
# def _read_from_json_file(self, filename):
# config_temp = {}
# with open(self.filename, "r") as f:
# config_temp = json.load(f)
# f.closed
# return config_temp
#
# # def _list_to_string(self, value_list):
# # """try to convert string to a meaningfull datatype."""
# # value_str = ""
# # value_str = ", ".join(value_list)
# # value_str = "[{}]".format(value_str)
# # return value_str
# #
# # def _string_to_list(self, value_str):
# # """try to convert string to a meaningfull datatype."""
# # list = []
# # if value_str.startswith("[") and value_str.endswith("]"):
# # value_str = value_str.strip()
# # list = value_str.split(",")
# # else:
# # raise TypeError("input string is not a valid list format.")
# # return list
# #
# # def _try_to_convert_string(self, value_str):
# # """try to convert string to a meaningfull datatype."""
# # value = None
# # try:
# # value = self._string_to_list(value_str)
# # except Exception as e:
# # print("value not a list. ({})".format(e))
# # else:
# # try:
# # value = self._string_to_dict(value_str)
# # except Exception as e:
# # print("value not a list. ({})".format(e))
# # return value
#
# def _convert_string_to_None(self, value_str):
# """Test if string is None."""
# value = None
# value_str = value_str.strip()
# if value_str in ["None", "none", "NONE", "Null", "NULL", "null"]:
# value = None
# else:
# value = value_str
# raise TypeError("input string is not valid None")
# return value
#
# def _try_to_interpret_string(self, value_str):
# """Try to interprete string as something meaningfull."""
# value = None
# try:
# value = json.loads(value_str)
# except Exception as e:
# # print("value not valid json. ({})".format(e))
# try:
# value = self._convert_string_to_None(value_str)
# except Exception as e:
# # print("value not None. ({})".format(e))
# value = value_str
# return value
#
# def _configparser_get_converted(self, cp, section, option):
# """Get option and try to convert it to a meaningfull datatype."""
# # with this we try to convert the value to a meaningfull value..
# value = None
# try:
# # try to read as int
# value = cp.getint(section, option)
# except Exception as e:
# # print("value not a int. ({})".format(e))
# try:
# # try to read as float
# value = cp.getfloat(section, option)
# except Exception as e:
# # print("value not a float. ({})".format(e))
# # try to read as int
# try:
# value = cp.getboolean(section, option)
# except Exception as e:
# # print("value not a boolean. ({})".format(e))
# # read as string
# value = cp.get(section, option)
# # try to convert it to something meaningfull
# value = self._try_to_interpret_string(value)
# # return value
# return value
#
# def _read_from_ini_file(self, filename):
# """Read form ini file and combine sections."""
# config_temp = {}
# cp = ConfigParser()
# with open(self.filename, "r") as f:
# cp.readfp(f)
# f.closed
# # now converte ConfigParser to dict.
# for section in cp.sections():
# # print("section: {}".format(section))
# config_temp[section] = {}
# for option in cp.options(section):
# # get option and add it to the dict
# # print("option: {}".format(option))
# value = self._configparser_get_converted(cp, section, option)
# # print("value: {}".format(value))
# config_temp[section][option] = value
#
# return config_temp
#
# def read_from_file(self, filename=None):
# """Read configuration from file."""
# if filename is not None:
# self.filename = filename
# config_temp = {}
# if self.filename is not None:
# # read file
# filename_ext = os.path.splitext(self.filename)[1]
# if filename_ext is not "" and filename_ext in ".json .js":
# config_temp = self._read_from_json_file(self.filename)
# else:
# config_temp = self._read_from_ini_file(self.filename)
#
# # extend config with defaults
# self.config = config_temp
# extend_deep(self.config, self.config_defaults.copy())
#
# def _write_to_json_file(self, filename, config):
# with open(filename, "w") as f:
# json.dump(config, f, sort_keys=True, indent=4, separators=(",", ": "))
# f.closed
#
# def _value_to_string(self, value):
# value_str = ""
# if (
# isinstance(value, object)
# or isinstance(value, dict)
# or isinstance(value, list)
# ):
# value_str = json.dumps(value)
# else:
# value_str = "{}".format(value)
# return value_str
#
# def _write_to_ini_file(self, filename, config):
# cp = ConfigParser()
# for section in config:
# # add section.
# # print("section: {}".format(section))
# cp.add_section(section)
# for option in config[section]:
# # print("option: {}".format(option))
# value = None
# if isinstance(config[section], list):
# # option_index = config[section].index(option)
# # value = config[section][option_index]
# value = None
# else:
# value = config[section][option]
# # print("value: {}".format(value))
# # add option
# cp.set(section, option, self._value_to_string(value))
# with open(filename, "w") as f:
# cp.write(f)
# f.closed
#
# def write_to_file(self, filename=None):
# """Write configuration to file."""
# if filename is not None:
# self.filename = filename
# if self.filename is not None:
# # print("\nwrite file: {}".format(self.filename))
# filename_ext = os.path.splitext(self.filename)[1]
# if filename_ext is not "" and filename_ext in ".json .js":
# self._write_to_json_file(self.filename, self.config)
# else:
# self._write_to_ini_file(self.filename, self.config)
#
# def get_formated(self):
# """Return config as Formated string."""
# return json.dumps(self.config, sort_keys=True, indent=4, separators=(",", ": "))
#
##########################################
if __name__ == "__main__":
print(42 * "*")
print("Python Version: " + sys.version)
print(42 * "*")
print(__doc__)
print(42 * "*")
# parse arguments
filename = "test.json"
# only use args after script name
arg = sys.argv[1:]
if not arg:
print("using standard values.")
print(" allowed parameters:")
print(" filename for config file (default='test.json')")
print("")
else:
filename = arg[0]
# if len(arg) > 1:
# pixel_count = int(arg[1])
# print parsed argument values
print(
"""values:
filename: {}
""".format(
filename
)
)
config_defaults = {
"hello": {
"world": 1,
"space": 42,
},
"world": {
"books": [
0,
1,
2,
3,
4,
4,
3,
2,
1,
0,
],
"fun": True,
"python": True,
"trees": {
"fir": 1,
"birch": 9,
"poplar": 33,
"maple": 11,
"cherry": 5,
"walnut": 2,
},
},
"blubber": ["water", "air"],
}
my_config = ConfigDict(config_defaults, filename)
print("my_config.config: {}".format(my_config.config))
# wait for user to hit key.
try:
raw_input(
"\n\n" + 42 * "*" + "\nhit a key to stop the mapper\n" + 42 * "*" + "\n\n"
)
except KeyboardInterrupt:
print("\nstop.")
except:
print("\nstop.")
# as last thing we save the current configuration.
print("\nwrite config.")
my_config.write_to_file()
# ###########################################