4 _log = logging.getLogger(__name__)
7 def __init__(self, fmt=None, datefmt=None, prefix = "# "):
8 logging.Formatter.__init__(self, fmt, datefmt)
11 fmsg = logging.Formatter.format(self, record)
13 if record.levelno >= logging.WARNING:
14 prefix += record.levelname +
": "
15 s =
"\n".join([ prefix + line
16 for line
in fmsg.splitlines() ])
21 logging.Filter.__init__(self, name)
27 def printOn(self, step = 1, force = False):
29 Decrease the printing_level of 'step' units. ( >0 means no print)
30 The level cannot go below 0, unless the force flag is set to True.
31 A negative value of the threshold disables subsequent "PrintOff"s.
42 Increase the printing_level of 'step' units. ( >0 means no print)
45 def disable(self, allowed = logging.WARNING):
48 def enable(self, allowed = logging.WARNING):
53 def __init__(self, stream = None, prefix = None):
56 logging.StreamHandler.__init__(self, stream)
64 self._formatter.prefix = prefix
65 def printOn(self, step = 1, force = False):
67 Decrease the printing_level of 'step' units. ( >0 means no print)
68 The level cannot go below 0, unless the force flag is set to True.
69 A negative value of the threshold disables subsequent "PrintOff"s.
71 self._filter.printOn(step, force)
74 Increase the printing_level of 'step' units. ( >0 means no print)
76 self._filter.printOff(step)
77 def disable(self, allowed = logging.WARNING):
78 self._filter.disable(allowed)
79 def enable(self, allowed = logging.WARNING):
80 self._filter.enable(allowed)
82 _consoleHandler =
None
84 global _consoleHandler
85 if _consoleHandler
is None:
87 elif prefix
is not None:
88 _consoleHandler.setPrefix(prefix)
89 return _consoleHandler
92 root_logger = logging.getLogger()
93 if not root_logger.handlers:
95 root_logger.setLevel(logging.WARNING)
97 root_logger.setLevel(level)
109 f = os.path.expandvars(f)
110 if os.path.isfile(f):
111 return os.path.realpath(f)
113 path = os.environ.get(
'JOBOPTSEARCHPATH',
'').split(os.pathsep)
115 candidates = [d
for d
in path
if os.path.isfile(os.path.join(d,f))]
117 raise ParserError(
"Cannot find '%s' in %s" % (f,path))
118 return os.path.realpath(os.path.join(candidates[0],f))
120 _included_files = set()
122 if f
in _included_files:
123 _log.warning(
"file '%s' already included, ignored.", f)
125 _included_files.add(f)
129 comment = re.compile(
r'(//.*)$')
132 comment_in_string = re.compile(
r'(["\']).*//.*\1')
133 directive = re.compile(
r'^\s*#\s*([\w!]+)\s*(.*)\s*$')
134 comment_ml = ( re.compile(
r'/\*'), re.compile(
r'\*/') )
136 reference = re.compile(
r'^@([\w.]*)$')
142 if sys.platform !=
'win32':
143 self.defines[
"WIN32" ] =
True
145 def _include(self,file,function):
148 _log.info(
"--> Including file '%s'", file)
150 _log.info(
"<-- End of file '%s'", file)
152 def parse(self,file):
157 ifdef_skipping =
False
158 ifdef_skipping_level = 0
162 if l.startswith(
"#!"):
171 m = self.comment.search(l)
174 m2 = self.comment_in_string.search(l)
177 if not ( m2
and m2.start() < m.start() ):
180 l = l[:m.start()]+l[m.end():]
182 m = self.directive.search(l)
184 directive_name = m.group(1)
185 directive_arg = m.group(2).strip()
186 if directive_name ==
"include":
187 included_file = directive_arg.strip(
"'\"")
189 elif directive_name ==
"units":
190 units_file = directive_arg.strip(
"'\"")
191 self._include(units_file,self._parse_units)
192 elif directive_name
in [
"ifdef",
"ifndef"]:
193 ifdef_skipping_level = ifdef_level
195 if directive_arg
in self.defines:
196 ifdef_skipping = directive_name ==
"ifndef"
198 ifdef_skipping = directive_name ==
"ifdef"
199 elif directive_name ==
"else":
200 ifdef_skipping =
not ifdef_skipping
201 elif directive_name ==
"endif":
203 if ifdef_skipping
and ifdef_skipping_level == ifdef_level:
204 ifdef_skipping =
False
205 elif directive_name ==
"pragma":
206 if not directive_arg:
209 pragma = directive_arg.split()
210 if pragma[0] ==
"print":
212 if pragma[1].upper()
in [
"ON",
"TRUE",
"1" ]:
217 _log.warning(
"unknown directive '%s'", directive_name)
226 m = self.comment_ml[0].search(l)
228 l,l1 = l[:m.start()],l[m.end():]
229 m = self.comment_ml[1].search(l1)
234 m = self.comment_ml[1].search(l1)
236 raise ParserError(
"End Of File reached before end of multi-line comment")
239 if self.statement_sep
in l:
240 i = l.index(self.statement_sep)
242 self._eval_statement(statement.replace(
"\n",
"").strip())
246 if statement.lstrip().startswith(
"//"):
253 def _parse_units(self,file):
254 for line
in open(file):
256 line = line[:line.index(
'//')]
260 nunit, value = line.split(
'=')
261 factor, unit = nunit.split()
262 value = eval(value)/eval(factor)
263 self.units[unit] = value
265 def _eval_statement(self,statement):
266 from GaudiKernel.Proxy.Configurable
import (ConfigurableGeneric,
270 _log.info(
"%s%s", statement, self.statement_sep)
272 property,value = statement.split(
"=",1)
275 if property[-1]
in [
"+",
"-" ]:
277 property = property[:-1]
279 property = property.strip()
280 value = value.strip()
297 property =
'.'.join([w.strip()
for w
in property.split(
'.')])
298 component, property = property.rsplit(
'.',1)
299 if component
in Configurable.allConfigurables:
300 cfg = Configurable.allConfigurables[component]
302 cfg = ConfigurableGeneric(component)
305 value = value.replace(
'true',
'True').replace(
'false',
'False')
308 if ':' in value
and not ( value[:value.index(
':')].count(
'"')%2
or value[:value.index(
':')].count(
"'")%2 ) :
310 value =
'{'+value[1:-1].replace(
'{',
'[').replace(
'}',
']')+
'}'
312 value = value.replace(
'{',
'[').replace(
'}',
']')
315 value = value.replace(
'\\',
'\\\\')
317 value = value.replace(
"\\n",
" ").replace(
"\\t",
" ")
320 m = self.reference.match(value)
323 value = PropertyReference(m.group(1))
325 value = eval(value,self.units)
330 if property
not in cfg.__slots__
and not hasattr(cfg,property):
332 lprop = property.lower()
333 for p
in cfg.__slots__:
334 if lprop == p.lower():
335 _log.warning(
"property '%s' was requested for %s, but the correct spelling is '%s'", property, cfg.name(), p)
341 if hasattr(cfg,property):
342 prop = getattr(cfg,property)
343 if type(prop) == dict:
349 setattr(cfg,property,value)
351 if hasattr(cfg,property):
352 prop = getattr(cfg,property)
353 if type(prop)
is dict:
358 _log.warning(
"key '%s' not in %s.%s", k, cfg.name(), property)
364 _log.warning(
"value '%s' not in %s.%s", k, cfg.name(), property)
366 setattr(cfg,property,value)
382 input = open(file,
'rb')
383 catalog = pickle.load(input)
384 _log.info(
'Unpickled %d configurables', len(catalog))
389 _import_function_mapping = {
390 ".py" : _import_python,
391 ".pkl" : _import_pickle,
392 ".opts" : _import_opts,
397 optsfile = os.path.expandvars(optsfile)
399 dummy, ext = os.path.splitext(optsfile)
400 if ext
in _import_function_mapping:
404 _log.info(
"--> Including file '%s'", optsfile)
406 _import_function_mapping[ext](optsfile)
407 _log.info(
"<-- End of file '%s'", optsfile)
409 raise ParserError(
"Unknown file type '%s' ('%s')" % (ext,optsfile))
418 unitsfile = os.path.expandvars(unitsfile)
421 _parser._include(unitsfile, _parser._parse_units)
def importUnits
Import a file containing declaration of units.
StatusCode parse(GaudiUtils::HashMap< K, V > &result, const std::string &input)
Basic parser for the types of HashMap used in DODBasicMapper.
def InstallRootLoggingHandler