The Gaudi Framework  master (f5098d57)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
__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 
19 from GaudiConfig2._configurables import ( # noqa: F401
20  Configurable,
21  Property,
22  all_options,
23  useGlobalInstances,
24 )
25 
26 # Regular expression to check if any of the options is a Python callable,
27 # in the form of a string like `package.sub_package.module:callable` or
28 # `path/to/file.py:callable`
29 CALLABLE_FORMAT = re.compile(
30  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_]*)$"
31 )
32 
33 
34 def _makeConfigDict(iterable):
35  try: # pragma no cover
36  from collections.abc import Mapping
37  except ImportError: # pragma no cover
38  from collections import Mapping
39 
40  if iterable is None:
41  return {}
42  if not isinstance(iterable, Mapping):
43  return {c.name: c for c in iterable}
44  return iterable
45 
46 
47 def mergeConfigs(*configs):
48  """
49  Merge configuration dictionaries ({'name': Configurable('name'), ...}) or
50  lists ([Configurable('name'), ...]) into one configuration dictionary.
51 
52  **warning** the configurable instances passed are not cloned during the
53  merging, so the arguments to this function cannot be used afterwards
54  """
55  result = {}
56  for config in configs:
57  config = _makeConfigDict(config)
58  for name in config:
59  if name in result:
60  result[name].merge(config[name])
61  else:
62  result[name] = config[name]
63  return result
64 
65 
66 def invokeConfig(func, *args, **kwargs):
67  from importlib import import_module
68 
69  if not callable(func):
70  if isinstance(func, str):
71  m = CALLABLE_FORMAT.match(func)
72  if m and m.group("module"):
73  func = getattr(import_module(m.group("module")), m.group("callable"))
74  elif m and m.group("path"):
75  globals = {"__file__": m.group("path")}
76  exec(
77  compile(
78  open(m.group("path"), "rb").read(), m.group("path"), "exec"
79  ),
80  globals,
81  )
82  func = globals[m.group("callable")]
83  else:
84  raise ValueError("invalid callable id %r" % func)
85  else:
86  raise TypeError("expected either a callable or a string as first argument")
87  return _makeConfigDict(func(*args, **kwargs))
GaudiConfig2._makeConfigDict
def _makeConfigDict(iterable)
Definition: __init__.py:34
GaudiConfig2.invokeConfig
def invokeConfig(func, *args, **kwargs)
Definition: __init__.py:66
cpluginsvc.func
func
Definition: cpluginsvc.py:235
GaudiConfig2.mergeConfigs
def mergeConfigs(*configs)
Definition: __init__.py:47
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:93
GaudiConfig2._db
Definition: _db.py:1