| # Gaudi Framework, version v21r10p1 |

Home |  | Generated: 29 Jul 2010

# 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, importUnits
    00010 from GaudiKernel.ProcessJobOptions import InstallRootLoggingHandler as _InstallRootLoggingHandler
    00011
    00012 import logging
    00013 log = logging.getLogger(__name__)
    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 def configurationDict(all=False):
    00055     """Return a dictionary representing the configuration.
    00056     The dictionary contains one entry per configurable which is a dictionary
    00057     with one entry per property.
    00058     The optional argument "all" is used to decide if to inluce only values
    00059     different from the default or all of them.
    00060     """
    00061     from GaudiKernel.Proxy.Configurable import getNeededConfigurables
    00062
    00063     catalog = allConfigurables
    00064     keys = getNeededConfigurables() # use only interesting configurables
    00065     conf_dict = {}
    00066     if all:
    00067         for n in keys :
    00068             if n not in conf_dict:
    00069                 conf_dict[n] = {}
    00070             for p, v in  catalog[n].getDefaultProperties().items() :
    00071                 conf_dict[n][p] = v
    00072
    00073     for n in keys :
    00074         if n not in conf_dict:
    00075             conf_dict[n] = {}
    00076         for p, v in catalog[n].getValuedProperties().items() :
    00077             conf_dict[n][p] = v
    00078     # purge empty configurables
    00079     keys = conf_dict.keys()
    00080     for n in keys:
    00081         if not conf_dict[n]:
    00082             del conf_dict[n]
    00083     return conf_dict
    00084
    00085 def getConfigurable(name, defaultType = None):
    00086     """Helper function to get a configurable with the given name regardless
    00087     for the type.
    00088     If defaultType can be a class derived from configurable or a string. If not
    00089     specified, the tool name is used as type."""
    00090     if name in allConfigurables:
    00091         return allConfigurables[name]
    00092     else:
    00093         # if the configurable is not found, we need to instantiate it
    00094         if defaultType is None:
    00095             # try to use the name of the configurable as default type
    00096             defaultType = name
    00097         if type(defaultType) is str:
    00098             # we need to convert from string to actual class
    00099             if defaultType in globals():
    00100                 # We the type is defined in the global namespace
    00101                 defaultType = globals()[defaultType]
    00102             else:
    00103                 # otherwise we try to get it from the Configurables database
    00104                 import Configurables
    00105                 defaultType = getattr(Configurables, defaultType)
    00106         return defaultType(name)