Changeset 269
- Timestamp:
- 09/15/2010 10:14:15 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
SHX/trunk/src/SeismicHandler/modules/runtime.py
r268 r269 25 25 import os 26 26 import ConfigParser 27 import warnings 27 28 from SeismicHandler.basics import Property, Singleton, AttribDict 28 29 … … 78 79 79 80 def __init__(self): 80 self.readConfig() 81 if not self.__configuration: 82 self.readConfig() 81 83 82 84 def readConfig(self): … … 88 90 2. /etc/ 89 91 3. $HOME/.shx/ 90 """ 91 92 dirs = { 93 "SHX": os.path.abspath(__file__).rsplit(os.path.sep, 2)[0], 94 "ETC": "/etc/SeismicHandler", 95 } 92 All configurations files found are processed in this order! 93 94 Finally the variable "conf" contains the full parsed configuration and 95 "fullconfig" contains the merged configuration with replacements. 96 """ 97 98 # helper function 99 def __handlePlaceholdersVars(values, replacevars=False): 100 """ 101 Replace placeholders in config file. 102 """ 103 104 # split values 105 if type(values) != list: 106 values = map(str.strip, values.split(",")) 107 108 ret = [] 109 for v in values: 110 if v.startswith("["): 111 # replacement requested 112 for d in replace.keys(): 113 if v.startswith("[%s]" % d): 114 # do replacement 115 ret.append(v.replace("[%s]" % d, replace[d])) 116 elif replacevars and v.startswith("$"): 117 # variable substitution 118 try: 119 ret.extend(vars[v.lower()]) 120 except KeyError: 121 warnings.warn("Variable '%s' not found!" % v, SyntaxWarning) 122 else: 123 ret.append(v) 124 125 return ret 126 127 # "main" function 128 dirs_check = [ 129 ["SHX", os.path.abspath(__file__).rsplit(os.path.sep, 2)[0]], 130 ["ETC", "/etc/SeismicHandler"], # XXX not portable! 131 ] 96 132 97 133 dir_home = os.getenv("HOME", None) 98 134 99 135 if dir_home: 100 dirs["PRIVATE"] = os.path.join(dir_home, ".shx") 101 102 for d in dirs.values(): 103 src = os.path.join(d, "shx.conf") 136 dirs_check.append(["PRIVATE", os.path.join(dir_home, ".shx")]) 137 # XXX not portable! 138 139 dirs = [] 140 replace = {} 141 for d in dirs_check: 142 src = os.path.join(d[1], "shx.conf") 143 replace[d[0]] = d[1] 104 144 if os.path.exists(src): 105 break 106 107 config = ConfigParser.SafeConfigParser() 108 if not config.read(src): 109 raise Exception("configuration file not found!") 145 dirs.append(src) 110 146 111 147 conf = AttribDict() 112 for s in config.sections(): 113 conf[s] = AttribDict() 114 for i in config.items(s): 115 conf[s][i[0]] = self.__handlePlaceholders(map(str.strip, \ 116 i[1].split(",")), dirs) 117 118 # check for include statement 119 if "include" in conf[s]: 120 121 # include sub configfile if requested 122 extra = ConfigParser.SafeConfigParser() 123 for e in conf[s]["include"]: 124 extra.read(e) 125 126 # only include current section 127 if not extra.has_section(s): 128 break 129 130 for ie in extra.items(s): 131 conf[s][ie[0]] = self.__handlePlaceholders(ie[1].split(","), dirs) 132 133 print conf 134 135 def __handlePlaceholders(self, values, dirs): 136 """ 137 Replace placeholders in config file. 138 """ 139 140 ret = [] 141 for v in values: 142 if v.startswith("["): 143 # replacement requested 144 for d in dirs.keys(): 145 if v.startswith("[%s]" % d): 146 # do replacement 147 ret.append(v.replace("[%s]" % d, dirs[d])) 148 149 else: 150 ret.append(v) 151 152 return ret 148 vars = AttribDict() 149 150 # includes merged configuration without replacements 151 fullconfig = ConfigParser.SafeConfigParser() 152 153 for src in dirs: 154 config = ConfigParser.SafeConfigParser() 155 config.read(src) 156 157 for s in config.sections(): 158 if not fullconfig.has_section(s): 159 fullconfig.add_section(s) 160 161 if s not in conf.keys(): 162 conf[s] = AttribDict() 163 164 for i in config.items(s): 165 fullconfig.set(s, *i) 166 167 # check for variable definition 168 if i[0].startswith("$"): 169 vars[i[0].lower()] = __handlePlaceholdersVars(i[1]) 170 else: 171 conf[s][i[0]] = __handlePlaceholdersVars(i[1]) 172 173 # check for include statement 174 if "include" in conf[s]: 175 176 # include sub configfile if requested 177 extra = ConfigParser.SafeConfigParser() 178 for e in conf[s]["include"]: 179 extra.read(e) 180 181 # only include current section 182 if not extra.has_section(s): 183 break 184 185 for ie in extra.items(s): 186 fullconfig.set(s, *ie) 187 conf[s][ie[0]] = __handlePlaceholdersVars(ie[1]) 188 189 del conf[s]["include"] 190 fullconfig.remove_option(s, "include") 191 192 # since configparser doesn't preserve order of entries, variables will 193 # be processed last. 194 for s in conf: 195 for i in conf[s]: 196 conf[s][i] = __handlePlaceholdersVars(conf[s][i], True) 197 198 self.__configuration = conf 199 self.__fullconfig = fullconfig 153 200 154 201 def __getattr__(self, name): … … 156 203 return getattr(self, "_Switches")[name[2:]] 157 204 else: 158 # return self.__class__.__dict__[name]159 205 return object.__getattribute__(self, name) 160 206
Note: See TracChangeset
for help on using the changeset viewer.