The Gaudi Framework  master (37c0b60a)
__init__.py
Go to the documentation of this file.
1 
12 from GaudiConfig2._db import ConfigurablesDB
13 
14 Configurables = ConfigurablesDB(__name__ + ".Configurables")
15 del ConfigurablesDB # no need to use this class after this point
16 
17 import re
18 from sys import version_info
19 
20 from GaudiConfig2._configurables import ( # noqa: F401
21  Configurable,
22  Property,
23  all_options,
24  useGlobalInstances,
25 )
26 
27 if version_info >= (3,): # pragma no cover
28  basestring = str
29 
30 # Regular expression to check if any of the options is a Python callable,
31 # in the form of a string like `package.sub_package.module:callable` or
32 # `path/to/file.py:callable`
33 CALLABLE_FORMAT = re.compile(
34  r"^(?:(?P<module>[a-zA-Z_][a-zA-Z0-9_]*(?:\.[a-zA-Z_][a-zA-Z0-9_]*)*)|(?P<path>[^\0]+\.py)):(?P<callable>[a-zA-Z_][a-zA-Z0-9_]*)$"
35 )
36 
37 
38 def _makeConfigDict(iterable):
39  try: # pragma no cover
40  from collections.abc import Mapping
41  except ImportError: # pragma no cover
42  from collections import Mapping
43 
44  if iterable is None:
45  return {}
46  if not isinstance(iterable, Mapping):
47  return {c.name: c for c in iterable}
48  return iterable
49 
50 
51 def mergeConfigs(*configs):
52  """
53  Merge configuration dictionaries ({'name': Configurable('name'), ...}) or
54  lists ([Configurable('name'), ...]) into one configuration dictionary.
55 
56  **warning** the configurable instances passed are not cloned during the
57  merging, so the arguments to this function cannot be used afterwards
58  """
59  result = {}
60  for config in configs:
61  config = _makeConfigDict(config)
62  for name in config:
63  if name in result:
64  result[name].merge(config[name])
65  else:
66  result[name] = config[name]
67  return result
68 
69 
70 def invokeConfig(func, *args, **kwargs):
71  from importlib import import_module
72 
73  if not callable(func):
74  if isinstance(func, basestring):
75  m = CALLABLE_FORMAT.match(func)
76  if m and m.group("module"):
77  func = getattr(import_module(m.group("module")), m.group("callable"))
78  elif m and m.group("path"):
79  globals = {"__file__": m.group("path")}
80  exec(
81  compile(
82  open(m.group("path"), "rb").read(), m.group("path"), "exec"
83  ),
84  globals,
85  )
86  func = globals[m.group("callable")]
87  else:
88  raise ValueError("invalid callable id %r" % func)
89  else:
90  raise TypeError("expected either a callable or a string as first argument")
91  return _makeConfigDict(func(*args, **kwargs))
GaudiConfig2._makeConfigDict
def _makeConfigDict(iterable)
Definition: __init__.py:38
GaudiConfig2.invokeConfig
def invokeConfig(func, *args, **kwargs)
Definition: __init__.py:70
cpluginsvc.func
func
Definition: cpluginsvc.py:235
GaudiConfig2.mergeConfigs
def mergeConfigs(*configs)
Definition: __init__.py:51
hivetimeline.read
def read(f, regex=".*", skipevents=0)
Definition: hivetimeline.py:32
GaudiConfig2._configurables
Definition: _configurables.py:1
merge
int merge(const char *target, const char *source, bool fixup=false, bool dbg=true)
Definition: merge.C:430
GaudiConfig2._db.ConfigurablesDB
Definition: _db.py:88
GaudiConfig2._db
Definition: _db.py:1