17 _log = logging.getLogger(__name__)
21 def __init__(self, fmt=None, datefmt=None, prefix="# ", with_time=False):
22 logging.Formatter.__init__(self, fmt, datefmt)
27 fmsg = logging.Formatter.format(self, record)
30 prefix +=
'%f ' % time.time()
31 if record.levelno >= logging.WARNING:
32 prefix += record.levelname +
": "
33 s =
"\n".join([prefix + line
for line
in fmsg.splitlines()])
39 logging.Filter.__init__(self, name)
50 Decrease the printing_level of 'step' units. ( >0 means no print)
51 The level cannot go below 0, unless the force flag is set to True.
52 A negative value of the threshold disables subsequent "PrintOff"s.
64 Increase the printing_level of 'step' units. ( >0 means no print)
68 def disable(self, allowed=logging.WARNING):
72 def enable(self, allowed=logging.WARNING):
78 def __init__(self, stream=None, prefix=None, with_time=False):
81 logging.StreamHandler.__init__(self, stream)
94 Decrease the printing_level of 'step' units. ( >0 means no print)
95 The level cannot go below 0, unless the force flag is set to True.
96 A negative value of the threshold disables subsequent "PrintOff"s.
102 Increase the printing_level of 'step' units. ( >0 means no print)
109 def enable(self, allowed=logging.WARNING):
113 _consoleHandler =
None
117 global _consoleHandler
118 if _consoleHandler
is None:
120 prefix=prefix, stream=stream, with_time=with_time)
121 elif prefix
is not None:
122 _consoleHandler.setPrefix(prefix)
123 return _consoleHandler
130 root_logger = logging.getLogger()
131 if not root_logger.handlers:
133 root_logger.setLevel(logging.WARNING)
134 if level
is not None:
135 root_logger.setLevel(level)
152 f = os.path.expandvars(f)
153 if os.path.isfile(f):
154 return os.path.realpath(f)
156 path = os.environ.get(
'JOBOPTSEARCHPATH',
'').split(os.pathsep)
158 candidates = [d
for d
in path
if os.path.isfile(os.path.join(d, f))]
160 raise ParserError(
"Cannot find '%s' in %s" % (f, path))
161 return os.path.realpath(os.path.join(candidates[0], f))
164 _included_files = set()
168 if f
in _included_files:
169 _log.warning(
"file '%s' already included, ignored.", f)
171 _included_files.add(f)
176 comment = re.compile(
r'(//.*)$')
179 comment_in_string = re.compile(
r'(["\']).*//.*\1')
180 directive = re.compile(
r'^\s*#\s*([\w!]+)\s*(.*)\s*$')
181 comment_ml = (re.compile(
r'/\*'), re.compile(
r'\*/'))
183 reference = re.compile(
r'^@([\w.]*)$')
189 if sys.platform !=
'win32':
195 _log.info(
"--> Including file '%s'", file)
197 _log.info(
"<-- End of file '%s'", file)
204 ifdef_skipping =
False
205 ifdef_skipping_level = 0
211 if l.startswith(
"#!"):
227 if not (m2
and m2.start() < m.start()):
230 l = l[:m.start()] + l[m.end():]
234 directive_name = m.group(1)
235 directive_arg = m.group(2).strip()
236 if directive_name ==
"include":
237 included_file = directive_arg.strip(
"'\"")
239 elif directive_name ==
"units":
240 units_file = directive_arg.strip(
"'\"")
242 elif directive_name
in [
"ifdef",
"ifndef"]:
243 ifdef_skipping_level = ifdef_level
245 if directive_arg
in self.
defines:
246 ifdef_skipping = directive_name ==
"ifndef"
248 ifdef_skipping = directive_name ==
"ifdef"
249 elif directive_name ==
"else":
250 ifdef_skipping =
not ifdef_skipping
251 elif directive_name ==
"endif":
253 if ifdef_skipping
and ifdef_skipping_level == ifdef_level:
254 ifdef_skipping =
False
255 elif directive_name ==
"pragma":
256 if not directive_arg:
259 pragma = directive_arg.split()
260 if pragma[0] ==
"print":
262 if pragma[1].upper()
in [
"ON",
"TRUE",
"1"]:
267 _log.warning(
"unknown directive '%s'", directive_name)
278 l, l1 = l[:m.start()], l[m.end():]
287 "End Of File reached before end of multi-line comment")
293 string_end = l.find(
'"')
295 statement += l[:string_end + 1]
296 l = l[string_end + 1:]
302 string_start = l.find(
'"')
303 if string_start >= 0:
304 string_end = l.find(
'"', string_start + 1)
307 statement += l[:string_end + 1]
308 l = l[string_end + 1:]
320 statement = l[i + 1:]
323 if statement.lstrip().startswith(
"//"):
331 for line
in open(file):
333 line = line[:line.index(
'//')]
337 nunit, value = line.split(
'=')
338 factor, unit = nunit.split()
339 value = eval(value) / eval(factor)
340 self.
units[unit] = value
343 from GaudiKernel.Proxy.Configurable
import (
344 ConfigurableGeneric, Configurable, PropertyReference)
348 property, value = statement.split(
"=", 1)
351 if property[-1]
in [
"+",
"-"]:
353 property = property[:-1]
355 property = property.strip()
356 value = value.strip()
373 property =
'.'.join([w.strip()
for w
in property.split(
'.')])
374 component, property = property.rsplit(
'.', 1)
375 if component
in Configurable.allConfigurables:
376 cfg = Configurable.allConfigurables[component]
378 cfg = ConfigurableGeneric(component)
381 value = value.replace(
'true',
'True').replace(
'false',
'False')
384 if ':' in value
and not (value[:value.index(
':')].count(
'"') % 2
or
385 value[:value.index(
':')].count(
"'") % 2):
388 value[1:-1].replace(
'{',
'[').replace(
'}',
']') +
'}'
390 value = value.replace(
'{',
'[').replace(
'}',
']')
393 value = value.replace(
'\\',
'\\\\')
395 value = (value.replace(
r"\\n",
r"\n").replace(
r"\\t",
r"\t").replace(
398 value =
'"'.join([(v
if i % 2
else re.sub(
r'\\[nt]',
' ', v))
399 for i, v
in enumerate(value.split(
'"'))])
405 value = PropertyReference(m.group(1))
407 value = eval(value, self.
units)
412 if property
not in cfg.__slots__
and not hasattr(cfg, property):
414 lprop = property.lower()
415 for p
in cfg.__slots__:
416 if lprop == p.lower():
418 "property '%s' was requested for %s, but the correct spelling is '%s'",
419 property, cfg.name(), p)
425 if hasattr(cfg, property):
426 prop = getattr(cfg, property)
427 if type(prop) == dict:
433 setattr(cfg, property, value)
435 if hasattr(cfg, property):
436 prop = getattr(cfg, property)
437 if type(prop)
is dict:
442 _log.warning(
"key '%s' not in %s.%s", k,
443 cfg.name(), property)
449 _log.warning(
"value '%s' not in %s.%s", k,
450 cfg.name(), property)
452 setattr(cfg, property, value)
468 with open(file)
as f:
469 code = compile(f.read(), file,
'exec')
475 input = open(file,
'rb')
476 catalog = pickle.load(input)
477 _log.info(
'Unpickled %d configurables', len(catalog))
484 _import_function_mapping = {
485 ".py": _import_python,
486 ".pkl": _import_pickle,
487 ".opts": _import_opts,
493 optsfile = os.path.expandvars(optsfile)
495 dummy, ext = os.path.splitext(optsfile)
496 if ext
in _import_function_mapping:
500 _log.info(
"--> Including file '%s'", optsfile)
502 _import_function_mapping[ext](optsfile)
503 _log.info(
"<-- End of file '%s'", optsfile)
505 raise ParserError(
"Unknown file type '%s' ('%s')" % (ext, optsfile))
517 unitsfile = os.path.expandvars(unitsfile)
520 _parser._include(unitsfile, _parser._parse_units)