| The Gaudi Framework v29r0 (ff2e7097)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | List of all members
GaudiKernel.ProcessJobOptions.JobOptsParser Class Reference
Collaboration diagram for GaudiKernel.ProcessJobOptions.JobOptsParser:
[legend]

## Public Member Functions
def | __init__ (self)
def | parse (self, file)

## Public Attributes
| units
| defines

## Static Public Attributes
| comment = re.compile(r'(//.*)$')
| comment_in_string = re.compile(r'(["\']).*//.*\1')
| directive = re.compile(r'^\s*#\s*([\w!]+)\s*(.*)\s*$')
tuple | comment_ml = (re.compile(r'/\*'), re.compile(r'\*/'))
string | statement_sep = ";"
| reference = re.compile(r'^@([\w.]*)$')

## Private Member Functions
def | _include (self, file, function)
def | _parse_units (self, file)
def | _eval_statement (self, statement)

## Detailed Description

Definition at line 162 of file ProcessJobOptions.py.

## Constructor & Destructor Documentation

def GaudiKernel.ProcessJobOptions.JobOptsParser.__init__ | ( |  | self | ) |

Definition at line 172 of file ProcessJobOptions.py.

172 def __init__(self):
173 # parser level states
174 self.units = {}
175 self.defines = {}
176 if sys.platform != 'win32':
177 self.defines["WIN32"] = True
178
GaudiKernel.ProcessJobOptions.JobOptsParser.__init__
def __init__(self)
Definition: ProcessJobOptions.py:172
GaudiKernel.ProcessJobOptions.JobOptsParser.defines
defines
Definition: ProcessJobOptions.py:175
GaudiKernel.ProcessJobOptions.JobOptsParser.units
units
Definition: ProcessJobOptions.py:174

## Member Function Documentation

def GaudiKernel.ProcessJobOptions.JobOptsParser._eval_statement | ( |  | self,
|  |  | statement
| ) |  |
| private

Definition at line 328 of file ProcessJobOptions.py.

328 def _eval_statement(self, statement):
329 from GaudiKernel.Proxy.Configurable import (ConfigurableGeneric,
330 Configurable,
331 PropertyReference)
332 #statement = statement.replace("\n","").strip()
333 _log.info("%s%s", statement, self.statement_sep)
334
335 property, value = statement.split("=", 1)
336
337 inc = None
338 if property[-1] in ["+", "-"]:
339 inc = property[-1]
340 property = property[:-1]
341
342 property = property.strip()
343 value = value.strip()
344
345 # find the configurable to apply the property to
346 #parent_cfg = None
347 # while '.' in property:
348 # component, property = property.split('.',1)
349 # if parent_cfg:
350 # if hasattr(parent_cfg,component):
351 # cfg = getattr(parent_cfg,component)
352 # else:
353 # cfg = ConfigurableGeneric(component)
354 # setattr(parent_cfg,component,cfg)
355 # else:
356 # cfg = ConfigurableGeneric(component)
357 # parent_cfg = cfg
358
359 # remove spaces around dots
360 property = '.'.join([w.strip() for w in property.split('.')])
361 component, property = property.rsplit('.', 1)
362 if component in Configurable.allConfigurables:
363 cfg = Configurable.allConfigurables[component]
364 else:
365 cfg = ConfigurableGeneric(component)
366
367 #value = os.path.expandvars(value)
368 value = value.replace('true', 'True').replace('false', 'False')
369 if value[0] == '{':
370 # Try to guess if the values looks like a dictionary
371 if ':' in value and not (value[:value.index(':')].count('"') % 2 or value[:value.index(':')].count("'") % 2):
372 # for dictionaries, keep the surrounding {}
373 value = '{' + \
374 value[1:-1].replace('{', '[').replace('}', ']') + '}'
375 else: # otherwise replace all {} with []
376 value = value.replace('{', '[').replace('}', ']')
377
378 # We must escape '\' because eval tends to interpret them
379 value = value.replace('\\', '\\\\')
380 # Restore special cases ('\n', '\t' and '\"') (see GAUDI-1001)
381 value = (value.replace(r"\\n", r"\n")
382 .replace(r"\\t", r"\t")
383 .replace(r'\\"', r'\"'))
384 # replace r'\n' and r'\t' that are outside double quoted strings
385 value = '"'.join([(v if i % 2 else re.sub(r'\\[nt]', ' ', v))
386 for i, v in enumerate(value.split('"'))])
387
388 # interprete the @ operator
389 m = self.reference.match(value)
390 if m:
391 # this allows late binding of references
392 value = PropertyReference(m.group(1))
393 else:
394 value = eval(value, self.units)
395
396 #if type(value) is str : value = os.path.expandvars(value)
397 # elif type(value) is list : value = [ type(item) is str and os.path.expandvars(item) or item for item in value ]
398
399 if property not in cfg.__slots__ and not hasattr(cfg, property):
400 # check if the case of the property is wrong (old options are case insensitive)
401 lprop = property.lower()
402 for p in cfg.__slots__:
403 if lprop == p.lower():
404 _log.warning(
405 "property '%s' was requested for %s, but the correct spelling is '%s'", property, cfg.name(), p)
406 property = p
407 break
408
409 # consider the += and -=
410 if inc == "+":
411 if hasattr(cfg, property):
412 prop = getattr(cfg, property)
413 if type(prop) == dict:
414 for k in value:
415 prop[k] = value[k]
416 else:
417 prop += value
418 else:
419 setattr(cfg, property, value)
420 elif inc == "-":
421 if hasattr(cfg, property):
422 prop = getattr(cfg, property)
423 if type(prop) is dict:
424 for k in value:
425 if k in prop:
426 del prop[k]
427 else:
428 _log.warning("key '%s' not in %s.%s",
429 k, cfg.name(), property)
430 else:
431 for k in value:
432 if k in prop:
433 prop.remove(k)
434 else:
435 _log.warning("value '%s' not in %s.%s",
436 k, cfg.name(), property)
437 else:
438 setattr(cfg, property, value)
439
440
GaudiKernel.ProcessJobOptions.JobOptsParser._eval_statement
def _eval_statement(self, statement)
Definition: ProcessJobOptions.py:328
GaudiKernel.ProcessJobOptions.JobOptsParser.statement_sep
string statement_sep
Definition: ProcessJobOptions.py:169
gaudirun.type
type
Definition: gaudirun.py:133
GaudiKernel.ProcessJobOptions.JobOptsParser.units
units
Definition: ProcessJobOptions.py:174

def GaudiKernel.ProcessJobOptions.JobOptsParser._include | ( |  | self,
|  |  | file,
|  |  | function
| ) |  |
| private

Definition at line 179 of file ProcessJobOptions.py.

179 def _include(self, file, function):
180 file = _find_file(file)
181 if _to_be_included(file):
182 _log.info("--> Including file '%s'", file)
183 function(file)
184 _log.info("<-- End of file '%s'", file)
185
GaudiKernel.ProcessJobOptions._to_be_included
def _to_be_included(f)
Definition: ProcessJobOptions.py:154
GaudiKernel.ProcessJobOptions._find_file
def _find_file(f)
Definition: ProcessJobOptions.py:137
GaudiKernel.ProcessJobOptions.JobOptsParser._include
def _include(self, file, function)
Definition: ProcessJobOptions.py:179

def GaudiKernel.ProcessJobOptions.JobOptsParser._parse_units | ( |  | self,
|  |  | file
| ) |  |
| private

Definition at line 316 of file ProcessJobOptions.py.

316 def _parse_units(self, file):
317 for line in open(file):
318 if '//' in line:
319 line = line[:line.index('//')]
320 line = line.strip()
321 if not line:
322 continue
323 nunit, value = line.split('=')
324 factor, unit = nunit.split()
325 value = eval(value) / eval(factor)
326 self.units[unit] = value
327
GaudiKernel.ProcessJobOptions.JobOptsParser._parse_units
def _parse_units(self, file)
Definition: ProcessJobOptions.py:316
GaudiKernel.ProcessJobOptions.JobOptsParser.units
units
Definition: ProcessJobOptions.py:174

def GaudiKernel.ProcessJobOptions.JobOptsParser.parse | ( |  | self,
|  |  | file
| ) |  |

Definition at line 186 of file ProcessJobOptions.py.

186 def parse(self, file):
187 # states for the "translation unit"
188 statement = ""
189
190 ifdef_level = 0
191 ifdef_skipping = False
192 ifdef_skipping_level = 0
193
194 in_string = False
195
196 f = open(_find_file(file))
197 l = f.readline()
198 if l.startswith("#!"):
199 # Skip the first line if it starts with "#!".
200 # It allows to use options files as scripts.
201 l = f.readline()
202
203 while l:
204 l = l.rstrip() + '\n' # normalize EOL chars (to avoid problems with DOS new-line on Unix)
205
206 # single line comment
207 m = self.comment.search(l)
208 if m:
209 # check if the '//' is part of a string
210 m2 = self.comment_in_string.search(l)
211 # the '//' is part of a string if we find the quotes around it
212 # and they are not part of the comment itself
213 if not (m2 and m2.start() < m.start()):
214 # if it is not the case, we can remove the comment from the
215 # statement
216 l = l[:m.start()] + l[m.end():]
217 # process directives
218 m = self.directive.search(l)
219 if m:
220 directive_name = m.group(1)
221 directive_arg = m.group(2).strip()
222 if directive_name == "include":
223 included_file = directive_arg.strip("'\"")
224 importOptions(included_file)
225 elif directive_name == "units":
226 units_file = directive_arg.strip("'\"")
227 self._include(units_file, self._parse_units)
228 elif directive_name in ["ifdef", "ifndef"]:
229 ifdef_skipping_level = ifdef_level
230 ifdef_level += 1
231 if directive_arg in self.defines:
232 ifdef_skipping = directive_name == "ifndef"
233 else:
234 ifdef_skipping = directive_name == "ifdef"
235 elif directive_name == "else":
236 ifdef_skipping = not ifdef_skipping
237 elif directive_name == "endif":
238 ifdef_level -= 1
239 if ifdef_skipping and ifdef_skipping_level == ifdef_level:
240 ifdef_skipping = False
241 elif directive_name == "pragma":
242 if not directive_arg:
243 l = f.readline()
244 continue
245 pragma = directive_arg.split()
246 if pragma[0] == "print":
247 if len(pragma) > 1:
248 if pragma[1].upper() in ["ON", "TRUE", "1"]:
249 PrintOn()
250 else:
251 PrintOff()
252 else:
253 _log.warning("unknown directive '%s'", directive_name)
254 l = f.readline()
255 continue
256
257 if ifdef_skipping:
258 l = f.readline()
259 continue
260
261 # multi-line comment
262 m = self.comment_ml[0].search(l)
263 if m:
264 l, l1 = l[:m.start()], l[m.end():]
265 m = self.comment_ml[1].search(l1)
266 while not m:
267 l1 = f.readline()
268 if not l1:
269 break # EOF
270 m = self.comment_ml[1].search(l1)
271 if not l1 and not m:
272 raise ParserError(
273 "End Of File reached before end of multi-line comment")
274 l += l1[m.end():]
275
276 # if we are in a multiline string, we add to the statement
277 # everything until the next '"'
278 if in_string:
279 string_end = l.find('"')
280 if string_end >= 0:
281 statement += l[:string_end + 1]
282 l = l[string_end + 1:]
283 in_string = False # the string ends here
284 else:
285 statement += l
286 l = ''
287 else: # check if we have a string
288 string_start = l.find('"')
289 if string_start >= 0:
290 string_end = l.find('"', string_start + 1)
291 if string_end >= 0:
292 # the string is opened and closed
293 statement += l[:string_end + 1]
294 l = l[string_end + 1:]
295 else:
296 # the string is only opened
297 statement += l
298 in_string = True
299 l = f.readline()
300 continue
301
302 if self.statement_sep in l:
303 i = l.index(self.statement_sep)
304 statement += l[:i]
305 self._eval_statement(statement.strip().replace("\n", "\\n"))
306 statement = l[i + 1:]
307 # it may happen (bug #37479) that the rest of the statement
308 # contains a comment.
309 if statement.lstrip().startswith("//"):
310 statement = ""
311 else:
312 statement += l
313
314 l = f.readline()
315
GaudiKernel.ProcessJobOptions.PrintOff
def PrintOff(step=1)
Definition: ProcessJobOptions.py:129
GaudiKernel.ProcessJobOptions.ParserError
Definition: ProcessJobOptions.py:133
GaudiKernel.ProcessJobOptions.JobOptsParser._parse_units
def _parse_units(self, file)
Definition: ProcessJobOptions.py:316
GaudiKernel.ProcessJobOptions._find_file
def _find_file(f)
Definition: ProcessJobOptions.py:137
GaudiKernel.ProcessJobOptions.JobOptsParser._eval_statement
def _eval_statement(self, statement)
Definition: ProcessJobOptions.py:328
GaudiKernel.ProcessJobOptions.JobOptsParser.statement_sep
string statement_sep
Definition: ProcessJobOptions.py:169
GaudiKernel.ProcessJobOptions.JobOptsParser.comment_ml
tuple comment_ml
Definition: ProcessJobOptions.py:168
GaudiKernel.ProcessJobOptions.JobOptsParser.defines
defines
Definition: ProcessJobOptions.py:175
GaudiKernel.ProcessJobOptions.JobOptsParser._include
def _include(self, file, function)
Definition: ProcessJobOptions.py:179
GaudiKernel.ProcessJobOptions.importOptions
def importOptions(optsfile)
Definition: ProcessJobOptions.py:475
GaudiKernel.ProcessJobOptions.PrintOn
def PrintOn(step=1, force=False)
Definition: ProcessJobOptions.py:125
GaudiKernel.ProcessJobOptions.JobOptsParser.parse
def parse(self, file)
Definition: ProcessJobOptions.py:186

## Member Data Documentation

GaudiKernel.ProcessJobOptions.JobOptsParser.comment = re.compile(r'(//.*)$')
| static

Definition at line 163 of file ProcessJobOptions.py.

GaudiKernel.ProcessJobOptions.JobOptsParser.comment_in_string = re.compile(r'(["\']).*//.*\1')
| static

Definition at line 166 of file ProcessJobOptions.py.

tuple GaudiKernel.ProcessJobOptions.JobOptsParser.comment_ml = (re.compile(r'/\*'), re.compile(r'\*/'))
| static

Definition at line 168 of file ProcessJobOptions.py.

GaudiKernel.ProcessJobOptions.JobOptsParser.defines

Definition at line 175 of file ProcessJobOptions.py.

GaudiKernel.ProcessJobOptions.JobOptsParser.directive = re.compile(r'^\s*#\s*([\w!]+)\s*(.*)\s*$')
| static

Definition at line 167 of file ProcessJobOptions.py.

GaudiKernel.ProcessJobOptions.JobOptsParser.reference = re.compile(r'^@([\w.]*)$')
| static

Definition at line 170 of file ProcessJobOptions.py.

string GaudiKernel.ProcessJobOptions.JobOptsParser.statement_sep = ";"
| static

Definition at line 169 of file ProcessJobOptions.py.

GaudiKernel.ProcessJobOptions.JobOptsParser.units

Definition at line 174 of file ProcessJobOptions.py.

The documentation for this class was generated from the following file:

- GaudiKernel/python/GaudiKernel/ProcessJobOptions.py