| # Gaudi Framework, version v20r4
Generated: 8 Jan 2009

# Configuration.py

Go to the documentation of this file.

    00001 # File: Gaudi/python/Gaudi/Configuration.py
    00002 # Author: Pere Mato (per.mato@cern.ch)
    00003
    00004 from GaudiKernel.Constants import *
    00005 from GaudiKernel.Configurable import *
    00006 from GaudiKernel.ConfigurableDb import loadConfigurableDb, cfgDb
    00007 from GaudiKernel.ConfigurableDb import getConfigurable as confDbGetConfigurable
    00008 from CommonGaudiConfigurables import *
    00009 from GaudiKernel.ProcessJobOptions import importOptions
    00010 from GaudiKernel.ProcessJobOptions import InstallRootLoggingHandler as _InstallRootLoggingHandler
    00011
    00012 import logging
    00013 log = logging.getLogger("Gaudi.Configuration")
    00014 # Ensure that a root logging handler is always present.
    00015 _InstallRootLoggingHandler()
    00016
    00017 allConfigurables = Configurable.allConfigurables
    00018
    00019 def _fillConfDict():
    00020     nFiles = loadConfigurableDb()
    00021     log = logging.getLogger( 'PropertyProxy' )
    00022     log.debug( "Read module info for %d configurables from %d genConfDb files",
    00023               len(cfgDb), nFiles )
    00024     if len(cfgDb.duplicates()) > 0:
    00025         log.warning( "Found %d duplicates among the %d genConfDb files :",
    00026                      len(cfgDb.duplicates()), nFiles )
    00027         log.warning( "--------------------------------------------------" )
    00028         log.warning( "  -%s: %s - %s",
    00029                      "<component name>", "<module>", "[ <duplicates> ]" )
    00030         log.warning( "--------------------------------------------------" )
    00031         dups = cfgDb.duplicates()
    00032         for cfgName in dups.keys():
    00033             log.warning( "  -%s: %s - %s",
    00034                          cfgName,
    00035                          cfgDb[cfgName]['module'],
    00036                          str([ d['module'] for d in dups[cfgName]]) )
    00037             pass
    00038         del dups
    00039         log.warning( "Fix your cmt/requirements file !!" )
    00040         pass
    00041     else:
    00042         log.debug( "No duplicates have been found: that's good !" )
    00043         pass
    00044     return
    00045
    00046 # fill the configurable dictionary at module load
    00047 _fillConfDict()
    00048
    00049 import os, sys
    00050
    00051 def importConfiguration(conf, local=locals()) :
    00052     local[conf] = confDbGetConfigurable(conf)
    00053
    00054 class ConfFacade(object) :
    00055     def __init__(self) :
    00056         pass
    00057     def __getattr__(self, name) :
    00058         if '_' == name[0] : return None
    00059         else:               return confDbGetConfigurable(name)
    00060
    00061 def configurationDict(all=False):
    00062     """Return a dictionary representing the configuration.
    00063     The dictionary contains one entry per configurable which is a dictionary
    00064     with one entry per property.
    00065     The optional argument "all" is used to decide if to inluce only values
    00066     different from the default or all of them.
    00067     """
    00068     from GaudiKernel.Proxy.Configurable import getNeededConfigurables
    00069
    00070     catalog = allConfigurables
    00071     keys = getNeededConfigurables() # use only interesting configurables
    00072     conf_dict = {}
    00073     if all:
    00074         for n in keys :
    00075             if n not in conf_dict:
    00076                 conf_dict[n] = {}
    00077             for p, v in  catalog[n].getDefaultProperties().items() :
    00078                 conf_dict[n][p] = v
    00079
    00080     for n in keys :
    00081         if n not in conf_dict:
    00082             conf_dict[n] = {}
    00083         for p, v in catalog[n].getValuedProperties().items() :
    00084             conf_dict[n][p] = v
    00085     # purge empty configurables
    00086     keys = conf_dict.keys()
    00087     for n in keys:
    00088         if not conf_dict[n]:
    00089             del conf_dict[n]
    00090     return conf_dict
    00091
    00092 def getConfigurable(name, defaultType = None):
    00093     """Helper function to get a configurable with the given name regardless
    00094     for the type.
    00095     If defaultType can be a class derived from configurable or a string. If not
    00096     specified, the tool name is used as type."""
    00097     if name in allConfigurables:
    00098         return allConfigurables[name]
    00099     else:
    00100         # if the configurable is not found, we need to instantiate it
    00101         if defaultType is None:
    00102             # try to use the name of the configurable as default type
    00103             defaultType = name
    00104         if type(defaultType) is str:
    00105             # we need to convert from string to actual class
    00106             if defaultType in globals():
    00107                 # We the type is defined in the global namespace
    00108                 defaultType = globals()[defaultType]
    00109             else:
    00110                 # otherwise we try to get it from the Configurables database
    00111                 exec "from Configurables import %s" % defaultType
    00112                 defaultType = locals()[defaultType]
    00113         return defaultType(name)
    00114
    00115 sys.modules['Configurables'] = ConfFacade()