| The Gaudi Framework v28r2p1 (f1a77ff4)

- 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 132 of file ProcessJobOptions.py.

## Constructor & Destructor Documentation

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

Definition at line 142 of file ProcessJobOptions.py.

142 def __init__(self):
143 # parser level states
144 self.units = {}
145 self.defines = {}
146 if sys.platform != 'win32':
147 self.defines[ "WIN32" ] = True
148
GaudiKernel.ProcessJobOptions.JobOptsParser.__init__
def __init__(self)
Definition: ProcessJobOptions.py:142
GaudiKernel.ProcessJobOptions.JobOptsParser.defines
defines
Definition: ProcessJobOptions.py:145
GaudiKernel.ProcessJobOptions.JobOptsParser.units
units
Definition: ProcessJobOptions.py:144

## Member Function Documentation

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

Definition at line 297 of file ProcessJobOptions.py.

297 def _eval_statement(self,statement):
298 from GaudiKernel.Proxy.Configurable import (ConfigurableGeneric,
299 Configurable,
300 PropertyReference)
301 #statement = statement.replace("\n","").strip()
302 _log.info("%s%s", statement, self.statement_sep)
303
304 property,value = statement.split("=",1)
305
306 inc = None
307 if property[-1] in [ "+", "-" ]:
308 inc = property[-1]
309 property = property[:-1]
310
311 property = property.strip()
312 value = value.strip()
313
314 ## find the configurable to apply the property to
315 #parent_cfg = None
316 #while '.' in property:
317 # component, property = property.split('.',1)
318 # if parent_cfg:
319 # if hasattr(parent_cfg,component):
320 # cfg = getattr(parent_cfg,component)
321 # else:
322 # cfg = ConfigurableGeneric(component)
323 # setattr(parent_cfg,component,cfg)
324 # else:
325 # cfg = ConfigurableGeneric(component)
326 # parent_cfg = cfg
327
328 # remove spaces around dots
329 property = '.'.join([w.strip() for w in property.split('.')])
330 component, property = property.rsplit('.',1)
331 if component in Configurable.allConfigurables:
332 cfg = Configurable.allConfigurables[component]
333 else:
334 cfg = ConfigurableGeneric(component)
335
336 #value = os.path.expandvars(value)
337 value = value.replace('true','True').replace('false','False')
338 if value[0] == '{' :
339 # Try to guess if the values looks like a dictionary
340 if ':' in value and not ( value[:value.index(':')].count('"')%2 or value[:value.index(':')].count("'")%2 ) :
341 # for dictionaries, keep the surrounding {}
342 value = '{'+value[1:-1].replace('{','[').replace('}',']')+'}'
343 else : # otherwise replace all {} with []
344 value = value.replace('{','[').replace('}',']')
345
346 # We must escape '\' because eval tends to interpret them
347 value = value.replace('\\', '\\\\')
348 # Restore special cases ('\n', '\t' and '\"') (see GAUDI-1001)
349 value = (value.replace(r"\\n", r"\n")
350 .replace(r"\\t", r"\t")
351 .replace(r'\\"', r'\"'))
352 # replace r'\n' and r'\t' that are outside double quoted strings
353 value = '"'.join([(v if i % 2 else re.sub(r'\\[nt]', ' ', v))
354 for i, v in enumerate(value.split('"'))])
355
356 # interprete the @ operator
357 m = self.reference.match(value)
358 if m:
359 # this allows late binding of references
360 value = PropertyReference(m.group(1))
361 else:
362 value = eval(value,self.units)
363
364 #if type(value) is str : value = os.path.expandvars(value)
365 #elif type(value) is list : value = [ type(item) is str and os.path.expandvars(item) or item for item in value ]
366
367 if property not in cfg.__slots__ and not hasattr(cfg,property):
368 # check if the case of the property is wrong (old options are case insensitive)
369 lprop = property.lower()
370 for p in cfg.__slots__:
371 if lprop == p.lower():
372 _log.warning("property '%s' was requested for %s, but the correct spelling is '%s'", property, cfg.name(), p)
373 property = p
374 break
375
376 # consider the += and -=
377 if inc == "+":
378 if hasattr(cfg,property):
379 prop = getattr(cfg,property)
380 if type(prop) == dict:
381 for k in value:
382 prop[k] = value[k]
383 else:
384 prop += value
385 else:
386 setattr(cfg,property,value)
387 elif inc == "-":
388 if hasattr(cfg,property):
389 prop = getattr(cfg,property)
390 if type(prop) is dict:
391 for k in value:
392 if k in prop:
393 del prop[k]
394 else:
395 _log.warning("key '%s' not in %s.%s", k, cfg.name(), property)
396 else:
397 for k in value:
398 if k in prop:
399 prop.remove(k)
400 else:
401 _log.warning("value '%s' not in %s.%s", k, cfg.name(), property)
402 else:
403 setattr(cfg,property,value)
404
GaudiKernel.ProcessJobOptions.JobOptsParser._eval_statement
def _eval_statement(self, statement)
Definition: ProcessJobOptions.py:297
GaudiKernel.ProcessJobOptions.JobOptsParser.statement_sep
string statement_sep
Definition: ProcessJobOptions.py:139
gaudirun.type
type
Definition: gaudirun.py:125
GaudiKernel.ProcessJobOptions.JobOptsParser.units
units
Definition: ProcessJobOptions.py:144

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

Definition at line 149 of file ProcessJobOptions.py.

149 def _include(self,file,function):
150 file = _find_file(file)
151 if _to_be_included(file):
152 _log.info("--> Including file '%s'", file)
153 function(file)
154 _log.info("<-- End of file '%s'", file)
155
GaudiKernel.ProcessJobOptions._to_be_included
def _to_be_included(f)
Definition: ProcessJobOptions.py:125
GaudiKernel.ProcessJobOptions._find_file
def _find_file(f)
Definition: ProcessJobOptions.py:111
GaudiKernel.ProcessJobOptions.JobOptsParser._include
def _include(self, file, function)
Definition: ProcessJobOptions.py:149

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

Definition at line 285 of file ProcessJobOptions.py.

285 def _parse_units(self,file):
286 for line in open(file):
287 if '//' in line:
288 line = line[:line.index('//')]
289 line = line.strip()
290 if not line:
291 continue
292 nunit, value = line.split('=')
293 factor, unit = nunit.split()
294 value = eval(value)/eval(factor)
295 self.units[unit] = value
296
GaudiKernel.ProcessJobOptions.JobOptsParser._parse_units
def _parse_units(self, file)
Definition: ProcessJobOptions.py:285
GaudiKernel.ProcessJobOptions.JobOptsParser.units
units
Definition: ProcessJobOptions.py:144

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

Definition at line 156 of file ProcessJobOptions.py.

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

## Member Data Documentation

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

Definition at line 133 of file ProcessJobOptions.py.

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

Definition at line 136 of file ProcessJobOptions.py.

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

Definition at line 138 of file ProcessJobOptions.py.

GaudiKernel.ProcessJobOptions.JobOptsParser.defines

Definition at line 145 of file ProcessJobOptions.py.

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

Definition at line 137 of file ProcessJobOptions.py.

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

Definition at line 140 of file ProcessJobOptions.py.

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

Definition at line 139 of file ProcessJobOptions.py.

GaudiKernel.ProcessJobOptions.JobOptsParser.units

Definition at line 144 of file ProcessJobOptions.py.

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

- GaudiKernel/python/GaudiKernel/ProcessJobOptions.py