The Gaudi Framework  master (37c0b60a)
ConfigurableDb.py
Go to the documentation of this file.
1 
13 """A singleton holding informations on the whereabouts of all the automatically
14 generated Configurables.
15 This repository of (informations on) Configurables is used by the PropertyProxy
16 class to locate Configurables and feed the JobOptionsSvc. It could also be used
17 to feed the AthenaEmacs module..."""
18 
19 __all__ = ["CfgDb", "cfgDb", "loadConfigurableDb", "getConfigurable"]
20 
21 import logging
22 
23 _transtable = str.maketrans("<>&*,: ().", "__rp__s___")
24 
25 log = logging.getLogger("ConfigurableDb")
26 
27 
28 class _CfgDb(dict):
29  """
30  A singleton class holding informations about automatically generated
31  Configurables.
32  --> package holding that Configurable
33  --> library holding the components related to that Configurable
34  --> python module holding the Configurable
35  --> a dictionary of duplicates
36  """
37 
38  __slots__ = {
39  "_duplicates": {},
40  }
41 
42  def __init__(self):
43  object.__init__(self)
44  self._duplicates = {}
45 
46  def add(self, configurable, package, module, lib):
47  """Method to populate the Db.
48  It is called from the auto-generated Xyz_confDb.py files (genconf.cpp)
49  @param configurable: the name of the configurable being added
50  @param package: the name of the package which holds this Configurable
51  @param module: the name of the python module holding the Configurable
52  @param lib: the name of the library holding the component(s) (ie: the
53  C++ Gaudi component which is mapped by the Configurable)
54  """
55  cfg = {"package": package, "module": module, "lib": lib}
56  if configurable in self:
57  # check if it comes from the same library...
58  if cfg["lib"] != self[configurable]["lib"]:
59  log.debug(
60  "dup!! [%s] p=%s m=%s lib=%s", configurable, package, module, lib
61  )
62  if configurable in self._duplicates:
63  self._duplicates[configurable] += [cfg]
64  else:
65  self._duplicates[configurable] = [cfg]
66  else:
67  log.debug("added [%s] p=%s m=%s lib=%s", configurable, package, module, lib)
68  self[configurable] = cfg
69 
70  def duplicates(self):
71  return self._duplicates
72 
73  def _loadModule(self, fname):
74  f = open(fname)
75  for i, ll in enumerate(f):
76  l = ll.strip()
77  if l.startswith("#") or len(l) <= 0:
78  continue
79  try:
80  line = l.split()
81  cname = line[2]
82  pkg = line[0].split(".")[0]
83  module = line[0]
84  lib = line[1]
85  self.add(cname, pkg, module, lib)
86  except IndexError:
87  f.close()
88  raise Exception("invalid line format: %s:%d: %r" % (fname, i + 1, ll))
89  f.close()
90 
91 
92 class _Singleton(object):
93  # the object this singleton is holding
94  # No other object will be created...
95  __obj = _CfgDb()
96 
97  def __call__(self):
98  return self.__obj
99 
100 
101 CfgDb = _Singleton()
102 
103 # clean-up
104 del _Singleton
105 del _CfgDb
106 
107 # default name for CfgDb instance
108 cfgDb = CfgDb()
109 
110 # Helper function to load all ConfigurableDb files holding informations
111 
112 
114  """Helper function to load all ConfigurableDb files (modules) holding
115  informations about Configurables
116  """
117  import os
118  from os.path import join as path_join
119 
120  log.debug("loading confDb files...")
121  nFiles = 0 # counter of imported files
122  pathlist = os.getenv("LD_LIBRARY_PATH", "").split(os.pathsep)
123  ignored_files = set(os.environ.get("CONFIGURABLE_DB_IGNORE", "").split(","))
124  for path in pathlist:
125  if not os.path.isdir(path):
126  continue
127  log.debug("walking in [%s]...", path)
128  confDbFiles = [
129  f
130  for f in [
131  path_join(path, f) for f in os.listdir(path) if f.endswith(".confdb")
132  ]
133  if os.path.isfile(f) and f not in ignored_files
134  ]
135  # check if we use "*_merged.confdb"
136  mergedConfDbFiles = [f for f in confDbFiles if f.endswith("_merged.confdb")]
137  if mergedConfDbFiles:
138  # use only the merged ones
139  confDbFiles = mergedConfDbFiles
140 
141  for confDb in confDbFiles:
142  log.debug("\t-loading [%s]...", confDb)
143  try:
144  cfgDb._loadModule(confDb)
145  except Exception as err:
146  log.warning("Could not load file [%s] !", confDb)
147  log.warning("Reason: %s", err)
148  nFiles += 1
149  log.debug("loading confDb files... [DONE]")
150  nPkgs = len(set([k["package"] for k in cfgDb.values()]))
151  log.debug("loaded %i confDb packages", nPkgs)
152  return nFiles
153 
154 
155 def getConfigurable(className, requester="", assumeCxxClass=True):
156  confClass = className
157  if assumeCxxClass:
158  # assume className is C++: --> translate to python
159  confClass = str.translate(confClass, _transtable)
160 
161  # see if I have it in my dictionary
162  confClassInfo = cfgDb.get(confClass)
163  if not confClassInfo:
164  confClassInfo = cfgDb.get(confClass)
165  # get the python module
166  confMod = confClassInfo and confClassInfo.get("module")
167  if not confMod:
168  log.warning("%s: Class %s not in database", requester, className)
169  return None
170  # load the module
171  try:
172  mod = __import__(confMod, globals(), locals(), confClass)
173  except ImportError:
174  log.warning(
175  "%s: Module %s not found (needed for configurable %s)",
176  requester,
177  confMod,
178  className,
179  )
180  return None
181  # get the class
182  try:
183  confClass = getattr(mod, confClass)
184  except AttributeError:
185  log.warning(
186  "%s: Configurable %s not found in module %s", requester, confClass, confMod
187  )
188  return None
189  # Got it!
190  log.debug("%s: Found configurable %s in module %s", requester, confClass, confMod)
191 
192  return confClass
GaudiKernel.ConfigurableDb._CfgDb.duplicates
def duplicates(self)
Definition: ConfigurableDb.py:70
GaudiKernel.ConfigurableDb.getConfigurable
def getConfigurable(className, requester="", assumeCxxClass=True)
Definition: ConfigurableDb.py:155
GaudiKernel.ConfigurableDb._Singleton.__obj
__obj
Definition: ConfigurableDb.py:95
GaudiKernel.ConfigurableDb._CfgDb.add
def add(self, configurable, package, module, lib)
Definition: ConfigurableDb.py:46
GaudiKernel.ConfigurableDb._CfgDb.__init__
def __init__(self)
Definition: ConfigurableDb.py:42
GaudiKernel.ConfigurableDb.CfgDb
CfgDb
Definition: ConfigurableDb.py:101
GaudiKernel.ConfigurableDb._Singleton
Definition: ConfigurableDb.py:92
GaudiKernel.ConfigurableDb.loadConfigurableDb
def loadConfigurableDb()
Definition: ConfigurableDb.py:113
GaudiKernel.ConfigurableDb._CfgDb._loadModule
def _loadModule(self, fname)
Definition: ConfigurableDb.py:73
GaudiKernel.ConfigurableDb._CfgDb
Definition: ConfigurableDb.py:28
GaudiKernel.ConfigurableDb._CfgDb._duplicates
_duplicates
Definition: ConfigurableDb.py:44
GaudiKernel.ConfigurableDb._Singleton.__call__
def __call__(self)
Definition: ConfigurableDb.py:97