Configuration.py
Go to the documentation of this file.
1 # File: Gaudi/python/Gaudi/Configuration.py
2 # Author: Pere Mato (pere.mato@cern.ch)
3 
4 from GaudiKernel.Constants import *
5 from GaudiKernel.Configurable import *
7 from GaudiKernel.ConfigurableDb import loadConfigurableDb, cfgDb
8 from GaudiKernel.ConfigurableDb import getConfigurable as confDbGetConfigurable
9 from CommonGaudiConfigurables import *
10 from GaudiKernel.ProcessJobOptions import importOptions, importUnits
11 from GaudiKernel.ProcessJobOptions import InstallRootLoggingHandler as _InstallRootLoggingHandler
12 
13 import logging
14 log = logging.getLogger(__name__)
15 # Ensure that a root logging handler is always present.
16 _InstallRootLoggingHandler()
17 
18 allConfigurables = Configurable.allConfigurables
19 
20 def _fillConfDict():
21  nFiles = loadConfigurableDb()
22  log = logging.getLogger( 'PropertyProxy' )
23  log.debug( "Read module info for %d configurables from %d genConfDb files",
24  len(cfgDb), nFiles )
25  if len(cfgDb.duplicates()) > 0:
26  log.warning( "Found %d duplicates among the %d genConfDb files :",
27  len(cfgDb.duplicates()), nFiles )
28  log.warning( "--------------------------------------------------" )
29  log.warning( " -%s: %s - %s",
30  "<component name>", "<module>", "[ <duplicates> ]" )
31  log.warning( "--------------------------------------------------" )
32  dups = cfgDb.duplicates()
33  for cfgName in dups.keys():
34  log.warning( " -%s: %s - %s",
35  cfgName,
36  cfgDb[cfgName]['module'],
37  str([ d['module'] for d in dups[cfgName]]) )
38  pass
39  del dups
40  log.warning( "Fix your cmt/requirements file !!" )
41  pass
42  else:
43  log.debug( "No duplicates have been found: that's good !" )
44  pass
45  return
46 
47 # fill the configurable dictionary at module load
49 
50 import os, sys
51 
52 def importConfiguration(conf, local=locals()) :
53  local[conf] = confDbGetConfigurable(conf)
54 
55 def configurationDict(all=False):
56  """Return a dictionary representing the configuration.
57  The dictionary contains one entry per configurable which is a dictionary
58  with one entry per property.
59  The optional argument "all" is used to decide if to include only values
60  different from the default or all of them.
61  """
62  from GaudiKernel.Proxy.Configurable import getNeededConfigurables
63 
64  catalog = allConfigurables
65  keys = getNeededConfigurables() # use only interesting configurables
66  conf_dict = {}
67  if all:
68  for n in keys :
69  if n not in conf_dict:
70  conf_dict[n] = {}
71  for p, v in catalog[n].getDefaultProperties().items() :
72  conf_dict[n][p] = v
73 
74  for n in keys :
75  if n not in conf_dict:
76  conf_dict[n] = {}
77  for p, v in catalog[n].getValuedProperties().items() :
78  conf_dict[n][p] = v
79  # purge empty configurables
80  keys = conf_dict.keys()
81  for n in keys:
82  if not conf_dict[n]:
83  del conf_dict[n]
84  return conf_dict
85 
86 def getConfigurable(name, defaultType = None):
87  """Helper function to get a configurable with the given name regardless
88  for the type.
89  If defaultType can be a class derived from configurable or a string. If not
90  specified, the tool name is used as type."""
91  if name in allConfigurables:
92  return allConfigurables[name]
93  else:
94  # if the configurable is not found, we need to instantiate it
95  if defaultType is None:
96  # try to use the name of the configurable as default type
97  defaultType = name
98  if type(defaultType) is str:
99  # we need to convert from string to actual class
100  if defaultType in globals():
101  # We the type is defined in the global namespace
102  defaultType = globals()[defaultType]
103  else:
104  # otherwise we try to get it from the Configurables database
105  import Configurables
106  defaultType = getattr(Configurables, defaultType)
107  return defaultType(name)
108 
109 def setCustomMainLoop(runner):
110  '''
111  Replace the default main execution loop with the specified callable object.
112 
113  @param runner: a callable that accepts an initialized instance of GaudiPython.AppMgr
114  and the number of events to process and returns a StatusCode or a boolean
115  (True means success)
116  '''
117  # change the mainLoop function
118  from Gaudi.Main import gaudimain
119  gaudimain.mainLoop = lambda _self, app, nevt: runner(app, nevt)
120 
121 
122 class GaudiPersistency(ConfigurableUser):
123  """Configurable to enable ROOT-based persistency.
124 
125  Note: it requires Gaudi::RootCnvSvc (package RootCnv).
126  """
127  __slots__ = {}
128  def __apply_configuration__(self):
129  """Apply low-level configuration"""
130  from Configurables import (ApplicationMgr,
131  PersistencySvc,
132  FileRecordDataSvc,
133  EventPersistencySvc,
134  )
135  # aliased names
136  from Configurables import (RootCnvSvc,
137  RootEvtSelector,
138  IODataManager,
139  FileCatalog,
140  )
141  cnvSvcs = [ RootCnvSvc() ]
142  EventPersistencySvc().CnvServices += cnvSvcs
143  PersistencySvc("FileRecordPersistencySvc").CnvServices += cnvSvcs
144  app = ApplicationMgr()
145  app.SvcOptMapping += [ FileCatalog(), IODataManager(),
146  RootCnvSvc() ]
147  app.ExtSvc += [ FileRecordDataSvc() ]
def setCustomMainLoop(runner)
def loadConfigurableDb()
Helper function to load all ConfigurableDb files holding informations.
PersistencySvc class implementation definition.
A FileRecordDataSvc is the base class for event services.
The Application Manager class.
string type
Definition: gaudirun.py:151