The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
Configuration.py
Go to the documentation of this file.
13
14import logging
15
16# Make these available in our namespace
17from Gaudi.CommonGaudiConfigurables import * # noqa: F401 F403
18from GaudiKernel.Configurable import * # noqa: F401 F403
19from GaudiKernel.Configurable import Configurable
20from GaudiKernel.ConfigurableDb import cfgDb, loadConfigurableDb
21from GaudiKernel.ConfigurableDb import getConfigurable as confDbGetConfigurable
22from GaudiKernel.Constants import * # noqa: F401 F403
24 InstallRootLoggingHandler as _InstallRootLoggingHandler,
25)
26from GaudiKernel.ProcessJobOptions import importOptions, importUnits # noqa: F401
27
28log = logging.getLogger(__name__)
29# Ensure that a root logging handler is always present.
30_InstallRootLoggingHandler()
31
32allConfigurables = Configurable.allConfigurables
33
34
36 nFiles = loadConfigurableDb()
37 log = logging.getLogger("PropertyProxy")
38 log.debug(
39 "Read module info for %d configurables from %d genConfDb files",
40 len(cfgDb),
41 nFiles,
42 )
43 if len(cfgDb.duplicates()) > 0:
44 log.warning(
45 "Found %d duplicates among the %d genConfDb files :",
46 len(cfgDb.duplicates()),
47 nFiles,
48 )
49 log.warning("--------------------------------------------------")
50 log.warning(
51 " -%s: %s - %s", "<component name>", "<module>", "[ <duplicates> ]"
52 )
53 log.warning("--------------------------------------------------")
54 dups = cfgDb.duplicates()
55 for cfgName in dups.keys():
56 log.warning(
57 " -%s: %s - %s",
58 cfgName,
59 cfgDb[cfgName]["module"],
60 str([d["module"] for d in dups[cfgName]]),
61 )
62 pass
63 del dups
64 pass
65 else:
66 log.debug("No duplicates have been found: that's good !")
67 pass
68 return
69
70
71# fill the configurable dictionary at module load
73
74
75def importConfiguration(conf, local=locals()):
76 local[conf] = confDbGetConfigurable(conf)
77
78
79def configurationDict(all=False):
80 """Return a dictionary representing the configuration.
81 The dictionary contains one entry per configurable which is a dictionary
82 with one entry per property.
83 The optional argument "all" is used to decide if to include only values
84 different from the default or all of them.
85 """
86 from GaudiKernel.Proxy.Configurable import getNeededConfigurables
87
88 catalog = allConfigurables
89 keys = getNeededConfigurables() # use only interesting configurables
90 conf_dict = {}
91 if all:
92 for n in keys:
93 if n not in conf_dict:
94 conf_dict[n] = {}
95 for p, v in catalog[n].getDefaultProperties().items():
96 conf_dict[n][p] = v
97
98 for n in keys:
99 if n not in conf_dict:
100 conf_dict[n] = {}
101 for p, v in catalog[n].getValuedProperties().items():
102 conf_dict[n][p] = v
103 # purge empty configurables
104 keys = conf_dict.keys()
105 ret_dict = {}
106 for n in keys:
107 if conf_dict[n]:
108 ret_dict[n] = conf_dict[n]
109 return ret_dict
110
111
112def getConfigurable(name, defaultType=None):
113 """Helper function to get a configurable with the given name regardless
114 for the type.
115 If defaultType can be a class derived from configurable or a string. If not
116 specified, the tool name is used as type."""
117 if name in allConfigurables:
118 return allConfigurables[name]
119 else:
120 # if the configurable is not found, we need to instantiate it
121 if defaultType is None:
122 # try to use the name of the configurable as default type
123 defaultType = name
124 if isinstance(defaultType, str):
125 # we need to convert from string to actual class
126 if defaultType in globals():
127 # We the type is defined in the global namespace
128 defaultType = globals()[defaultType]
129 else:
130 # otherwise we try to get it from the Configurables database
131 import Configurables
132
133 defaultType = getattr(Configurables, defaultType)
134 return defaultType(name)
importConfiguration(conf, local=locals())
configurationDict(all=False)
getConfigurable(name, defaultType=None)