5 _log = logging.getLogger(__name__)
8 def __init__(self, fmt=None, datefmt=None, prefix = "# ", with_time = False):
9 logging.Formatter.__init__(self, fmt, datefmt)
13 fmsg = logging.Formatter.format(self, record)
16 prefix +=
'%f ' % time.time()
17 if record.levelno >= logging.WARNING:
18 prefix += record.levelname +
": " 19 s =
"\n".join([ prefix + line
20 for line
in fmsg.splitlines() ])
25 logging.Filter.__init__(self, name)
31 def printOn(self, step = 1, force = False):
33 Decrease the printing_level of 'step' units. ( >0 means no print) 34 The level cannot go below 0, unless the force flag is set to True. 35 A negative value of the threshold disables subsequent "PrintOff"s. 46 Increase the printing_level of 'step' units. ( >0 means no print) 49 def disable(self, allowed = logging.WARNING):
52 def enable(self, allowed = logging.WARNING):
57 def __init__(self, stream = None, prefix = None, with_time = False):
60 logging.StreamHandler.__init__(self, stream)
68 self._formatter.prefix = prefix
69 def printOn(self, step = 1, force = False):
71 Decrease the printing_level of 'step' units. ( >0 means no print) 72 The level cannot go below 0, unless the force flag is set to True. 73 A negative value of the threshold disables subsequent "PrintOff"s. 75 self._filter.printOn(step, force)
78 Increase the printing_level of 'step' units. ( >0 means no print) 80 self._filter.printOff(step)
81 def disable(self, allowed = logging.WARNING):
82 self._filter.disable(allowed)
83 def enable(self, allowed = logging.WARNING):
84 self._filter.enable(allowed)
86 _consoleHandler =
None 88 global _consoleHandler
89 if _consoleHandler
is None:
90 _consoleHandler =
ConsoleHandler(prefix = prefix, stream = stream, with_time = with_time)
91 elif prefix
is not None:
92 _consoleHandler.setPrefix(prefix)
93 return _consoleHandler
96 root_logger = logging.getLogger()
97 if not root_logger.handlers:
99 root_logger.setLevel(logging.WARNING)
100 if level
is not None:
101 root_logger.setLevel(level)
113 f = os.path.expandvars(f)
114 if os.path.isfile(f):
115 return os.path.realpath(f)
117 path = os.environ.get(
'JOBOPTSEARCHPATH',
'').split(os.pathsep)
119 candidates = [d
for d
in path
if os.path.isfile(os.path.join(d,f))]
121 raise ParserError(
"Cannot find '%s' in %s" % (f,path))
122 return os.path.realpath(os.path.join(candidates[0],f))
124 _included_files = set()
126 if f
in _included_files:
127 _log.warning(
"file '%s' already included, ignored.", f)
129 _included_files.add(f)
133 comment = re.compile(
r'(//.*)$')
136 comment_in_string = re.compile(
r'(["\']).*//.*\1')
137 directive = re.compile(
r'^\s*#\s*([\w!]+)\s*(.*)\s*$')
138 comment_ml = ( re.compile(
r'/\*'), re.compile(
r'\*/') )
140 reference = re.compile(
r'^@([\w.]*)$')
146 if sys.platform !=
'win32':
152 _log.info(
"--> Including file '%s'", file)
154 _log.info(
"<-- End of file '%s'", file)
161 ifdef_skipping =
False 162 ifdef_skipping_level = 0
168 if l.startswith(
"#!"):
177 m = self.comment.search(l)
180 m2 = self.comment_in_string.search(l)
183 if not ( m2
and m2.start() < m.start() ):
186 l = l[:m.start()]+l[m.end():]
188 m = self.directive.search(l)
190 directive_name = m.group(1)
191 directive_arg = m.group(2).strip()
192 if directive_name ==
"include":
193 included_file = directive_arg.strip(
"'\"")
195 elif directive_name ==
"units":
196 units_file = directive_arg.strip(
"'\"")
198 elif directive_name
in [
"ifdef",
"ifndef"]:
199 ifdef_skipping_level = ifdef_level
201 if directive_arg
in self.
defines:
202 ifdef_skipping = directive_name ==
"ifndef" 204 ifdef_skipping = directive_name ==
"ifdef" 205 elif directive_name ==
"else":
206 ifdef_skipping =
not ifdef_skipping
207 elif directive_name ==
"endif":
209 if ifdef_skipping
and ifdef_skipping_level == ifdef_level:
210 ifdef_skipping =
False 211 elif directive_name ==
"pragma":
212 if not directive_arg:
215 pragma = directive_arg.split()
216 if pragma[0] ==
"print":
218 if pragma[1].upper()
in [
"ON",
"TRUE",
"1" ]:
223 _log.warning(
"unknown directive '%s'", directive_name)
234 l,l1 = l[:m.start()],l[m.end():]
242 raise ParserError(
"End Of File reached before end of multi-line comment")
248 string_end = l.find(
'"')
250 statement += l[:string_end+1]
257 string_start = l.find(
'"')
258 if string_start >= 0:
259 string_end = l.find(
'"', string_start + 1)
262 statement += l[:string_end+1]
278 if statement.lstrip().startswith(
"//"):
286 for line
in open(file):
288 line = line[:line.index(
'//')]
292 nunit, value = line.split(
'=')
293 factor, unit = nunit.split()
294 value = eval(value)/eval(factor)
295 self.
units[unit] = value
298 from GaudiKernel.Proxy.Configurable
import (ConfigurableGeneric,
304 property,value = statement.split(
"=",1)
307 if property[-1]
in [
"+",
"-" ]:
309 property = property[:-1]
311 property = property.strip()
312 value = value.strip()
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]
334 cfg = ConfigurableGeneric(component)
337 value = value.replace(
'true',
'True').replace(
'false',
'False')
340 if ':' in value
and not ( value[:value.index(
':')].count(
'"')%2
or value[:value.index(
':')].count(
"'")%2 ) :
342 value =
'{'+value[1:-1].replace(
'{',
'[').replace(
'}',
']')+
'}' 344 value = value.replace(
'{',
'[').replace(
'}',
']')
347 value = value.replace(
'\\',
'\\\\')
349 value = (value.replace(
r"\\n",
r"\n")
350 .replace(
r"\\t",
r"\t")
351 .replace(
r'\\"',
r'\"'))
353 value =
'"'.join([(v
if i % 2
else re.sub(
r'\\[nt]',
' ', v))
354 for i, v
in enumerate(value.split(
'"'))])
357 m = self.reference.match(value)
360 value = PropertyReference(m.group(1))
362 value = eval(value,self.
units)
367 if property
not in cfg.__slots__
and not hasattr(cfg,property):
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)
378 if hasattr(cfg,property):
379 prop = getattr(cfg,property)
380 if type(prop) == dict:
386 setattr(cfg,property,value)
388 if hasattr(cfg,property):
389 prop = getattr(cfg,property)
390 if type(prop)
is dict:
395 _log.warning(
"key '%s' not in %s.%s", k, cfg.name(), property)
401 _log.warning(
"value '%s' not in %s.%s", k, cfg.name(), property)
403 setattr(cfg,property,value)
419 input = open(file,
'rb')
420 catalog = pickle.load(input)
421 _log.info(
'Unpickled %d configurables', len(catalog))
426 _import_function_mapping = {
427 ".py" : _import_python,
428 ".pkl" : _import_pickle,
429 ".opts" : _import_opts,
434 optsfile = os.path.expandvars(optsfile)
436 dummy, ext = os.path.splitext(optsfile)
437 if ext
in _import_function_mapping:
441 _log.info(
"--> Including file '%s'", optsfile)
443 _import_function_mapping[ext](optsfile)
444 _log.info(
"<-- End of file '%s'", optsfile)
446 raise ParserError(
"Unknown file type '%s' ('%s')" % (ext,optsfile))
455 unitsfile = os.path.expandvars(unitsfile)
458 _parser._include(unitsfile, _parser._parse_units)
def disable(self, allowed=logging.WARNING)
def __init__(self, stream=None, prefix=None, with_time=False)
def _parse_units(self, file)
def GetConsoleHandler(prefix=None, stream=None, with_time=False)
def _eval_statement(self, statement)
def enable(self, allowed=logging.WARNING)
def __init__(self, new_path)
def printOn(self, step=1, force=False)
def InstallRootLoggingHandler(prefix=None, level=None, stream=None, with_time=False)
def printOff(self, step=1)
def __init__(self, name="")
def _include(self, file, function)
def importOptions(optsfile)
def printOn(self, step=1, force=False)
def setPrefix(self, prefix)
def PrintOn(step=1, force=False)
def printOff(self, step=1)
def enable(self, allowed=logging.WARNING)
def disable(self, allowed=logging.WARNING)
def importUnits(unitsfile)
Import a file containing declaration of units.