-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpblUtilities_config.py
258 lines (223 loc) · 10.5 KB
/
cpblUtilities_config.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os, re, sys, copy
"""This module provides dicts paths and defaults, which contain any parameters needed by multiple other modules.
These parameters are generally unchanging over time, but may vary from one installation/environment to another.
There are four places these config settings could be set. In order of priority:
(1) local config.cfg file
(2) local config-template.cfg file
(3) cpblUtilities source folder config.cfg file
(4) cpblUtilities source folder config-template.cfg file
- Specify structure of file.
- Load cascaded values from config files.
- Then rearrange as we need to put them into the dict arrays paths and defaults.
Note that some key functions located in other modules of cpblUtilities are reproduced
here, in order to avoid circular dependencies. e.g. dsetset, dgetget,
and even a version of read_hierarchy_of_config_files()
"""
UTILS_config_file_structure={
'paths': [
'working',
'input',
'graphics',
'outputdata',
'output',
'tex',
'scratch',
'bin',
'svgmaptemplates',
],
'defaults': [
('rdc',bool),
'mode',
],
}
# The file config-template.cfg contains an example of a file which should be renamed config.cfg
def readConfigFile(inpath, config_file_structure):
"""
"""
import ConfigParser
config = ConfigParser.SafeConfigParser({'pwd': os.getcwd(),'cwd': os.getcwd()})
config.read(inpath)
outdict={}
for section in config_file_structure:
if config.has_section(section):
for option in config_file_structure[section]:
if config.has_option(section,option if isinstance(option,str) else option[0]):
if isinstance(option,str):
dsetset(outdict,(section,option), config.get(section,option))
elif option[1]==bool:
dsetset(outdict,(section,option[0]), config.getboolean(section,option[0]))
elif option[1]==int:
dsetset(outdict,(section,option[0]), config.getint(section,option[0]))
elif option[1]==float:
dsetset(outdict,(section,option[0]), config.getfloat(section,option[0]))
elif option[1]=='commasep':
dsetset(outdict,(section,option[0]), config.get(section,option[0]).split(','))
else:
raise('Do not know config value type '+str(option[1]))
return(outdict)
def _notgeneral_readConfigFile(inpath):
import ConfigParser
config = ConfigParser.SafeConfigParser({'pwd': os.getcwd(),'cwd': os.getcwd()})
config.read(inpath)
outdict={}
for section in config_file_structure:
if config.has_section(section):
for option in config_file_structure[section]:
if config.has_option(section,option if isinstance(option,str) else option[0]):
if isinstance(option,str):
dsetset(outdict,(section,option), config.get(section,option))
elif option[1]==bool:
dsetset(outdict,(section,option[0]), config.getboolean(section,option[0]))
elif option[1]==int:
dsetset(outdict,(section,option[0]), config.getint(section,option[0]))
elif option[1]=='commasep':
dsetset(outdict,(section,option[0]), config.get(section,option[0]).split(','))
return(outdict)
################################################################################################
################################################################################################
def dsetset(adict,keys,avalue):
############################################################################################
############################################################################################
"""
July 2011: making the converse of dgetget... but I think rather more efficient!
This sets the value of a nested dict, ensuring that the sublevels exist.
adict must exist and be a dict, of course.
"""
if len(keys)>1:
if keys[0] not in adict:
adict[keys[0]]={}
dsetset(adict[keys[0]],keys[1:],avalue)
else:
adict[keys[0]]=avalue
return
################################################################################################
################################################################################################
def dgetget(adict,keys,defaultvalue,*args):
############################################################################################
############################################################################################
"""
July 2011: rewriting degetget, using recursion, and conforming only to the newer format in which a list of the keys is passed.
Much more efficient than the old version which took nargs!
adict must exist and be a dict, of course.
*args is vestigial, for backwards compatibility. It should not be used.
"""
# Backwards compatibility: Ancient dgetgetold(adict, key1, key2, key3=None,key4=None,key5=None,key6=None,keyn=None):
if not isinstance(keys,list):
keylist=[keys,defaultvalue]+list(args)
#keylist=keylist[:min([ii for ii in range(len(keylist)) if keylist[ii] is None])]
keylist, defaultvalue= keylist[:-1] ,keylist[-1]
return( dgetget(adict,keylist,defaultvalue))
#
return( dgetgetOLD(adict,keys,defaultvalue,key3=key3,key4=key4,key5=key5,key6=key6,keyn=keyn))
# New, recursive algorithm, which takes a list of keys as second argument:
if keys[0] not in adict:
return(defaultvalue)
if len(keys)==1:
return(adict[keys[0]])
return(dgetget(adict[keys[0]],keys[1:],defaultvalue))
def merge_dictionaries(default,update, verboseSource=False, allow_new_keys=True):
"""Given two dictionaries, this deep copies 'default' but updates it with any
matching keys from 'update'.
allow_new_keys = False ensures that only keys in the default are taken (updated) from the update.
If not False, verboseSource must be a string, which denotes the updating source file description
"""
result=copy.deepcopy(default)
for key in update:
if key not in default:
if allow_new_keys:
result[key]=update[key]
else:
print("WARNING: configuration merge_dictionaries got an update, but\
that key doesn't exist in the default config settings. key=%s"%key)
continue
if type(update[key])==dict:
result[key]=merge_dictionaries(result[key],update[key], verboseSource=verboseSource)
else:
result[key]=update[key]
if verboseSource:
print(' Using '+verboseSource+' config value for: '+key)
if 0:
print('-------')
print default
print update
print result
return result
def _co_read_hierarchy_of_config_files(files):
"""
There is a more general version of this in configtools, which is used by other modules. But I can't use that one because these modules would be cross-dependent
"""
configDict={}
for ff in files:
if os.path.exists(ff):
newConfigDict=readConfigFile(ff)
configDict=merge_dictionaries(configDict,newConfigDict, verboseSource=False) #bool(configDict))
if not configDict:
raise Exception("Cannot find config[-template].cfg file in "+', '.join(files))
return configDict
def read_hierarchy_of_config_files(files,config_file_structure, verbose=True):
"""
Reads a sequence of config files, successively updating a dict of config settings.
Returns the dict.
if verbose is True, it also reports file was the last to set each setting.
Note that there is also a verboseSource feature in merge_dictionaries, which reports updating as it goes, but this is less useful than the verbose behaviour given here.
"""
configDict={}
configDictOrigins={}
def setOrigin(filename,adict):
for kk in adict:
if isinstance(adict[kk],dict):
setOrigin(filename,adict[kk])
else:
adict[kk]=filename
def reportOrigin(adict, vdict):
for kk in adict:
if isinstance(adict[kk],dict):
reportOrigin(adict[kk], vdict[kk])
else:
print(kk+'\t = \t'+str(vdict[kk])+' :\t (from '+adict[kk]+ ')')
for ff in files:
if os.path.exists(ff):
newConfigDict=readConfigFile(ff,config_file_structure)
if verbose:
newConfigOrigins=copy.deepcopy(newConfigDict)
setOrigin(ff,newConfigOrigins)
configDictOrigins=merge_dictionaries(configDictOrigins,newConfigOrigins)
configDict=merge_dictionaries(configDict,newConfigDict, verboseSource=False) #False if not verbose else ff) #bool(configDict))
if not configDict:
raise Exception("Cannot find config[-template].cfg file in "+', '.join(files))
if verbose:
reportOrigin(configDictOrigins, configDict)
return configDict
def main():
"""
"""
localConfigFile=os.getcwd()+'/config.cfg'
localConfigTemplateFile=os.getcwd()+'/config-template.cfg'
repoPath=os.path.abspath(os.path.dirname(__file__ if __file__ is not None else '.'))
if 0:
# Change directory to the bin folder, ie location of this module. That way, we always have the config.cfg file as local, which means other utlilities using config.cfg will find the right one.
path = os.path.abspath(__file__)
dir_path = os.path.dirname(path)
if 'cpblUtilities' not in os.getcwd():
os.chdir(dir_path)
repoFile=(repoPath if repoPath else '.')+'/config.cfg'
repoTemplateFile=(repoPath if repoPath else '.')+'/config-template.cfg'
print('cpblUtilities setting defaults:')
merged_dictionary=read_hierarchy_of_config_files([
repoTemplateFile,
repoFile,
localConfigTemplateFile,
localConfigFile,
], UTILS_config_file_structure)
# Now impose our structure
defaults=dict([[kk,vv] for kk,vv in merged_dictionary.items() if kk in ['rdc','mode']])
defaults.update(dict(paths=merged_dictionary['paths'],
))
defaults['stata']={'paths':copy.deepcopy(defaults['paths'])}
return(defaults)
defaults=main()
paths=defaults['paths']
if 'python_utils_path' in paths:
sys.path.append(paths['python_utils_path'])