The Gaudi Framework  v36r3 (83a1ddab)
__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 (
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`
32 CALLABLE_FORMAT = re.compile(
33  r"^(?P<module>[a-zA-Z_][a-zA-Z0-9_]*(?:\.[a-zA-Z_][a-zA-Z0-9_]*)*):(?P<callable>[a-zA-Z_][a-zA-Z0-9_]*)$"
34 )
35 
36 
37 def _makeConfigDict(iterable):
38  try: # pragma no cover
39  from collections.abc import Mapping
40  except ImportError: # pragma no cover
41  from collections import Mapping
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, basestring):
71  m = CALLABLE_FORMAT.match(func)
72  if m:
73  func = getattr(import_module(m.group("module")), m.group("callable"))
74  else:
75  raise ValueError("invalid callable id %r" % func)
76  else:
77  raise TypeError("expected either a callable or a string as first argument")
78  return _makeConfigDict(func(*args, **kwargs))
GaudiConfig2._makeConfigDict
def _makeConfigDict(iterable)
Definition: __init__.py:37
GaudiConfig2.invokeConfig
def invokeConfig(func, *args, **kwargs)
Definition: __init__.py:66
GaudiPluginService.cpluginsvc.func
func
Definition: cpluginsvc.py:236
GaudiConfig2.mergeConfigs
def mergeConfigs(*configs)
Definition: __init__.py:47
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:102
GaudiConfig2._db
Definition: _db.py:1