Public Member Functions | |
def | __init__ |
def | parse |
Public Attributes | |
units | |
defines | |
Static Public Attributes | |
tuple | comment = re.compile(r'(//.*)$') |
tuple | comment_in_string = re.compile(r'(["\']).*//.*\1') |
tuple | directive = re.compile(r'^\s*#\s*([\w!]+)\s*(.*)\s*$') |
tuple | comment_ml = ( re.compile(r'/\*'), re.compile(r'\*/') ) |
string | statement_sep = ";" |
tuple | reference = re.compile(r'^@([\w.]*)$') |
Private Member Functions | |
def | _include |
def | _parse_units |
def | _eval_statement |
Definition at line 131 of file ProcessJobOptions.py.
def ProcessJobOptions::JobOptsParser::__init__ | ( | self | ) |
def ProcessJobOptions::JobOptsParser::_include | ( | self, | ||
file, | ||||
function | ||||
) | [private] |
Definition at line 148 of file ProcessJobOptions.py.
00148 : 00149 file = _find_file(file) 00150 if _to_be_included(file): 00151 _log.info("--> Including file '%s'", file) 00152 function(file) 00153 _log.info("<-- End of file '%s'", file) 00154 def parse(self,file):
def ProcessJobOptions::JobOptsParser::parse | ( | self, | ||
file | ||||
) |
Definition at line 155 of file ProcessJobOptions.py.
00155 : 00156 # states for the "translation unit" 00157 statement = "" 00158 00159 ifdef_level = 0 00160 ifdef_skipping = False 00161 ifdef_skipping_level = 0 00162 00163 f = open(_find_file(file)) 00164 l = f.readline() 00165 if l.startswith("#!"): 00166 # Skip the first line if it starts with "#!". 00167 # It allows to use options files as scripts. 00168 l = f.readline() 00169 00170 while l: 00171 l = l.rstrip()+'\n' # normalize EOL chars (to avoid problems with DOS new-line on Unix) 00172 00173 # single line comment 00174 m = self.comment.search(l) 00175 if m: 00176 # check if the '//' is part of a string 00177 m2 = self.comment_in_string.search(l) 00178 # the '//' is part of a string if we find the quotes around it 00179 # and they are not part of the comment itself 00180 if not ( m2 and m2.start() < m.start() ): 00181 # if it is not the case, we can remove the comment from the 00182 # statement 00183 l = l[:m.start()]+l[m.end():] 00184 # process directives 00185 m = self.directive.search(l) 00186 if m: 00187 directive_name = m.group(1) 00188 directive_arg = m.group(2).strip() 00189 if directive_name == "include": 00190 included_file = directive_arg.strip("'\"") 00191 importOptions(included_file) 00192 elif directive_name == "units": 00193 units_file = directive_arg.strip("'\"") 00194 self._include(units_file,self._parse_units) 00195 elif directive_name in [ "ifdef", "ifndef"]: 00196 ifdef_skipping_level = ifdef_level 00197 ifdef_level += 1 00198 if directive_arg in self.defines: 00199 ifdef_skipping = directive_name == "ifndef" 00200 else: 00201 ifdef_skipping = directive_name == "ifdef" 00202 elif directive_name == "else": 00203 ifdef_skipping = not ifdef_skipping 00204 elif directive_name == "endif": 00205 ifdef_level -= 1 00206 if ifdef_skipping and ifdef_skipping_level == ifdef_level: 00207 ifdef_skipping = False 00208 elif directive_name == "pragma": 00209 if not directive_arg: 00210 l = f.readline() 00211 continue 00212 pragma = directive_arg.split() 00213 if pragma[0] == "print": 00214 if len(pragma) > 1: 00215 if pragma[1].upper() in [ "ON", "TRUE", "1" ]: 00216 PrintOn() 00217 else: 00218 PrintOff() 00219 else: 00220 _log.warning("unknown directive '%s'", directive_name) 00221 l = f.readline() 00222 continue 00223 00224 if ifdef_skipping: 00225 l = f.readline() 00226 continue 00227 00228 # multi-line comment 00229 m = self.comment_ml[0].search(l) 00230 if m: 00231 l,l1 = l[:m.start()],l[m.end():] 00232 m = self.comment_ml[1].search(l1) 00233 while not m: 00234 l1 = f.readline() 00235 if not l1: 00236 break # EOF 00237 m = self.comment_ml[1].search(l1) 00238 if not l1 and not m: 00239 raise ParserError("End Of File reached before end of multi-line comment") 00240 l += l1[m.end():] 00241 00242 if self.statement_sep in l: 00243 i = l.index(self.statement_sep) 00244 statement += l[:i] 00245 self._eval_statement(statement.replace("\n","").strip()) 00246 statement = l[i+1:] 00247 # it may happen (bug #37479) that the rest of the statement 00248 # contains a comment. 00249 if statement.lstrip().startswith("//"): 00250 statement = "" 00251 else: 00252 statement += l 00253 00254 l = f.readline() 00255 def _parse_units(self,file):
def ProcessJobOptions::JobOptsParser::_parse_units | ( | self, | ||
file | ||||
) | [private] |
Definition at line 256 of file ProcessJobOptions.py.
00256 : 00257 for line in open(file): 00258 if '//' in line: 00259 line = line[:line.index('//')] 00260 line = line.strip() 00261 if not line: 00262 continue 00263 nunit, value = line.split('=') 00264 factor, unit = nunit.split() 00265 value = eval(value)/eval(factor) 00266 self.units[unit] = value 00267 def _eval_statement(self,statement):
def ProcessJobOptions::JobOptsParser::_eval_statement | ( | self, | ||
statement | ||||
) | [private] |
Definition at line 268 of file ProcessJobOptions.py.
00268 : 00269 from GaudiKernel.Proxy.Configurable import (ConfigurableGeneric, 00270 Configurable, 00271 PropertyReference) 00272 #statement = statement.replace("\n","").strip() 00273 _log.info("%s%s", statement, self.statement_sep) 00274 00275 property,value = statement.split("=",1) 00276 00277 inc = None 00278 if property[-1] in [ "+", "-" ]: 00279 inc = property[-1] 00280 property = property[:-1] 00281 00282 property = property.strip() 00283 value = value.strip() 00284 00285 ## find the configurable to apply the property to 00286 #parent_cfg = None 00287 #while '.' in property: 00288 # component, property = property.split('.',1) 00289 # if parent_cfg: 00290 # if hasattr(parent_cfg,component): 00291 # cfg = getattr(parent_cfg,component) 00292 # else: 00293 # cfg = ConfigurableGeneric(component) 00294 # setattr(parent_cfg,component,cfg) 00295 # else: 00296 # cfg = ConfigurableGeneric(component) 00297 # parent_cfg = cfg 00298 00299 # remove spaces around dots 00300 property = '.'.join([w.strip() for w in property.split('.')]) 00301 component, property = property.rsplit('.',1) 00302 if component in Configurable.allConfigurables: 00303 cfg = Configurable.allConfigurables[component] 00304 else: 00305 cfg = ConfigurableGeneric(component) 00306 00307 #value = os.path.expandvars(value) 00308 value = value.replace('true','True').replace('false','False') 00309 if value[0] == '{' : 00310 # Try to guess if the values looks like a dictionary 00311 if ':' in value and not ( value[:value.index(':')].count('"')%2 or value[:value.index(':')].count("'")%2 ) : 00312 # for dictionaries, keep the surrounding {} 00313 value = '{'+value[1:-1].replace('{','[').replace('}',']')+'}' 00314 else : # otherwise replace all {} with [] 00315 value = value.replace('{','[').replace('}',']') 00316 00317 # We must escape '\' because eval tends to interpret them 00318 value = value.replace('\\','\\\\') 00319 00320 # interprete the @ operator 00321 m = self.reference.match(value) 00322 if m: 00323 # this allows late binding of references 00324 value = PropertyReference(m.group(1)) 00325 else: 00326 value = eval(value,self.units) 00327 00328 #if type(value) is str : value = os.path.expandvars(value) 00329 #elif type(value) is list : value = [ type(item) is str and os.path.expandvars(item) or item for item in value ] 00330 00331 if property not in cfg.__slots__ and not hasattr(cfg,property): 00332 # check if the case of the property is wrong (old options are case insensitive) 00333 lprop = property.lower() 00334 for p in cfg.__slots__: 00335 if lprop == p.lower(): 00336 _log.warning("property '%s' was requested for %s, but the correct spelling is '%s'", property, cfg.name(), p) 00337 property = p 00338 break 00339 00340 # consider the += and -= 00341 if inc == "+": 00342 if hasattr(cfg,property): 00343 prop = getattr(cfg,property) 00344 if type(prop) == dict: 00345 for k in value: 00346 prop[k] = value[k] 00347 else: 00348 prop += value 00349 else: 00350 setattr(cfg,property,value) 00351 elif inc == "-": 00352 if hasattr(cfg,property): 00353 prop = getattr(cfg,property) 00354 if type(prop) is dict: 00355 for k in value: 00356 if k in prop: 00357 del prop[k] 00358 else: 00359 _log.warning("key '%s' not in %s.%s", k, cfg.name(), property) 00360 else: 00361 for k in value: 00362 if k in prop: 00363 prop.remove(k) 00364 else: 00365 _log.warning("value '%s' not in %s.%s", k, cfg.name(), property) 00366 else: 00367 setattr(cfg,property,value) 00368 class _TempSysPath:
tuple ProcessJobOptions::JobOptsParser::comment = re.compile(r'(//.*)$') [static] |
Definition at line 132 of file ProcessJobOptions.py.
tuple ProcessJobOptions::JobOptsParser::comment_in_string = re.compile(r'(["\']).*//.*\1') [static] |
Definition at line 135 of file ProcessJobOptions.py.
tuple ProcessJobOptions::JobOptsParser::directive = re.compile(r'^\s*#\s*([\w!]+)\s*(.*)\s*$') [static] |
Definition at line 136 of file ProcessJobOptions.py.
tuple ProcessJobOptions::JobOptsParser::comment_ml = ( re.compile(r'/\*'), re.compile(r'\*/') ) [static] |
Definition at line 137 of file ProcessJobOptions.py.
string ProcessJobOptions::JobOptsParser::statement_sep = ";" [static] |
Definition at line 138 of file ProcessJobOptions.py.
tuple ProcessJobOptions::JobOptsParser::reference = re.compile(r'^@([\w.]*)$') [static] |
Definition at line 139 of file ProcessJobOptions.py.
Definition at line 143 of file ProcessJobOptions.py.
Definition at line 144 of file ProcessJobOptions.py.