7 _log = logging.getLogger(__name__)
11 def __init__(self, fmt=None, datefmt=None, prefix="# ", with_time=False):
12 logging.Formatter.__init__(self, fmt, datefmt)
17 fmsg = logging.Formatter.format(self, record)
20 prefix +=
'%f ' % time.time()
21 if record.levelno >= logging.WARNING:
22 prefix += record.levelname +
": " 23 s =
"\n".join([prefix + line
24 for line
in fmsg.splitlines()])
30 logging.Filter.__init__(self, name)
40 Decrease the printing_level of 'step' units. ( >0 means no print) 41 The level cannot go below 0, unless the force flag is set to True. 42 A negative value of the threshold disables subsequent "PrintOff"s. 54 Increase the printing_level of 'step' units. ( >0 means no print) 58 def disable(self, allowed=logging.WARNING):
62 def enable(self, allowed=logging.WARNING):
68 def __init__(self, stream=None, prefix=None, with_time=False):
71 logging.StreamHandler.__init__(self, stream)
80 self._formatter.prefix = prefix
84 Decrease the printing_level of 'step' units. ( >0 means no print) 85 The level cannot go below 0, unless the force flag is set to True. 86 A negative value of the threshold disables subsequent "PrintOff"s. 88 self._filter.printOn(step, force)
92 Increase the printing_level of 'step' units. ( >0 means no print) 94 self._filter.printOff(step)
96 def disable(self, allowed=logging.WARNING):
97 self._filter.disable(allowed)
99 def enable(self, allowed=logging.WARNING):
100 self._filter.enable(allowed)
103 _consoleHandler =
None 107 global _consoleHandler
108 if _consoleHandler
is None:
110 prefix=prefix, stream=stream, with_time=with_time)
111 elif prefix
is not None:
112 _consoleHandler.setPrefix(prefix)
113 return _consoleHandler
117 root_logger = logging.getLogger()
118 if not root_logger.handlers:
120 root_logger.setLevel(logging.WARNING)
121 if level
is not None:
122 root_logger.setLevel(level)
139 f = os.path.expandvars(f)
140 if os.path.isfile(f):
141 return os.path.realpath(f)
143 path = os.environ.get(
'JOBOPTSEARCHPATH',
'').split(os.pathsep)
145 candidates = [d
for d
in path
if os.path.isfile(os.path.join(d, f))]
147 raise ParserError(
"Cannot find '%s' in %s" % (f, path))
148 return os.path.realpath(os.path.join(candidates[0], f))
151 _included_files = set()
155 if f
in _included_files:
156 _log.warning(
"file '%s' already included, ignored.", f)
158 _included_files.add(f)
163 comment = re.compile(
r'(//.*)$')
166 comment_in_string = re.compile(
r'(["\']).*//.*\1')
167 directive = re.compile(
r'^\s*#\s*([\w!]+)\s*(.*)\s*$')
168 comment_ml = (re.compile(
r'/\*'), re.compile(
r'\*/'))
170 reference = re.compile(
r'^@([\w.]*)$')
176 if sys.platform !=
'win32':
182 _log.info(
"--> Including file '%s'", file)
184 _log.info(
"<-- End of file '%s'", file)
191 ifdef_skipping =
False 192 ifdef_skipping_level = 0
198 if l.startswith(
"#!"):
204 l = l.rstrip() +
'\n' 207 m = self.comment.search(l)
210 m2 = self.comment_in_string.search(l)
213 if not (m2
and m2.start() < m.start()):
216 l = l[:m.start()] + l[m.end():]
218 m = self.directive.search(l)
220 directive_name = m.group(1)
221 directive_arg = m.group(2).strip()
222 if directive_name ==
"include":
223 included_file = directive_arg.strip(
"'\"")
225 elif directive_name ==
"units":
226 units_file = directive_arg.strip(
"'\"")
228 elif directive_name
in [
"ifdef",
"ifndef"]:
229 ifdef_skipping_level = ifdef_level
231 if directive_arg
in self.
defines:
232 ifdef_skipping = directive_name ==
"ifndef" 234 ifdef_skipping = directive_name ==
"ifdef" 235 elif directive_name ==
"else":
236 ifdef_skipping =
not ifdef_skipping
237 elif directive_name ==
"endif":
239 if ifdef_skipping
and ifdef_skipping_level == ifdef_level:
240 ifdef_skipping =
False 241 elif directive_name ==
"pragma":
242 if not directive_arg:
245 pragma = directive_arg.split()
246 if pragma[0] ==
"print":
248 if pragma[1].upper()
in [
"ON",
"TRUE",
"1"]:
253 _log.warning(
"unknown directive '%s'", directive_name)
264 l, l1 = l[:m.start()], l[m.end():]
273 "End Of File reached before end of multi-line comment")
279 string_end = l.find(
'"')
281 statement += l[:string_end + 1]
282 l = l[string_end + 1:]
288 string_start = l.find(
'"')
289 if string_start >= 0:
290 string_end = l.find(
'"', string_start + 1)
293 statement += l[:string_end + 1]
294 l = l[string_end + 1:]
306 statement = l[i + 1:]
309 if statement.lstrip().startswith(
"//"):
317 for line
in open(file):
319 line = line[:line.index(
'//')]
323 nunit, value = line.split(
'=')
324 factor, unit = nunit.split()
325 value = eval(value) / eval(factor)
326 self.
units[unit] = value
335 property, value = statement.split(
"=", 1)
338 if property[-1]
in [
"+",
"-"]:
340 property = property[:-1]
342 property = property.strip()
343 value = value.strip()
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]
365 cfg = ConfigurableGeneric(component)
368 value = value.replace(
'true',
'True').replace(
'false',
'False')
371 if ':' in value
and not (value[:value.index(
':')].count(
'"') % 2
or value[:value.index(
':')].count(
"'") % 2):
374 value[1:-1].replace(
'{',
'[').replace(
'}',
']') +
'}' 376 value = value.replace(
'{',
'[').replace(
'}',
']')
379 value = value.replace(
'\\',
'\\\\')
381 value = (value.replace(
r"\\n",
r"\n")
382 .replace(
r"\\t",
r"\t")
383 .replace(
r'\\"',
r'\"'))
385 value =
'"'.join([(v
if i % 2
else re.sub(
r'\\[nt]',
' ', v))
386 for i, v
in enumerate(value.split(
'"'))])
389 m = self.reference.match(value)
392 value = PropertyReference(m.group(1))
394 value = eval(value, self.
units)
399 if property
not in cfg.__slots__
and not hasattr(cfg, property):
401 lprop = property.lower()
402 for p
in cfg.__slots__:
403 if lprop == p.lower():
405 "property '%s' was requested for %s, but the correct spelling is '%s'", property, cfg.name(), p)
411 if hasattr(cfg, property):
412 prop = getattr(cfg, property)
413 if type(prop) == dict:
419 setattr(cfg, property, value)
421 if hasattr(cfg, property):
422 prop = getattr(cfg, property)
423 if type(prop)
is dict:
428 _log.warning(
"key '%s' not in %s.%s",
429 k, cfg.name(), property)
435 _log.warning(
"value '%s' not in %s.%s",
436 k, cfg.name(), property)
438 setattr(cfg, property, value)
459 input = open(file,
'rb')
460 catalog = pickle.load(input)
461 _log.info(
'Unpickled %d configurables', len(catalog))
468 _import_function_mapping = {
469 ".py": _import_python,
470 ".pkl": _import_pickle,
471 ".opts": _import_opts,
477 optsfile = os.path.expandvars(optsfile)
479 dummy, ext = os.path.splitext(optsfile)
480 if ext
in _import_function_mapping:
484 _log.info(
"--> Including file '%s'", optsfile)
486 _import_function_mapping[ext](optsfile)
487 _log.info(
"<-- End of file '%s'", optsfile)
489 raise ParserError(
"Unknown file type '%s' ('%s')" % (ext, optsfile))
500 unitsfile = os.path.expandvars(unitsfile)
503 _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)