Gaudi Framework, version v23r4

Home   Generated: Mon Sep 17 2012

Persistency.py

Go to the documentation of this file.
00001 """
00002 Module to configure the persistency type in GaudiPython.
00003 """
00004 __author__ = "Marco Clemencic <marco.clemencic@cern.ch>"
00005 
00006 class PersistencyError(RuntimeError):
00007     """
00008     Base class for exceptions in PersistencyHelper.
00009     """
00010     pass
00011 
00012 class UnknownPersistency(PersistencyError):
00013     """
00014     Exception raised if the persistency type is not known to the module.
00015     """
00016     def __init__(self, type_):
00017         super(UnknownPersistency, self).__init__("Unknown persistency type %r" % type_)
00018         self.type = type_
00019 
00020 # internal storage for the persistency helpers
00021 _implementations = []
00022 
00023 def get(type_):
00024     """
00025     Return the PersistencyHerper implementing the given persistency type.
00026     """
00027     for i in _implementations:
00028         if i.handle(type_):
00029             return i
00030     raise UnknownPersistency(type_)
00031 
00032 def add(instance):
00033     """
00034     Function to extend the list of known helpers.
00035 
00036     New helpers are added to the top of the list.
00037     """
00038     _implementations.insert(0, instance)
00039 
00040 class FileDescription(object):
00041     def __init__(self, filename, opt, svc, sel=None, collection=None, fun=None):
00042         '''
00043         Class to hold/manipulate the file description.
00044 
00045         @param filename: name of the file
00046         @param opt: option (READ/CREATE/RECREATE/WRITE)
00047         @param svc: conversion service (or selector)
00048         @param sel: selection expression
00049         @param collection: collection
00050         @param fun: selection class
00051         '''
00052         self.filename = filename
00053         self.opt = opt
00054         self.svc = svc
00055         self.sel = sel
00056         self.collection = collection
00057         self.fun = fun
00058     def __data__(self):
00059         '''
00060         Return a list of pairs describing the instance.
00061         '''
00062         return [("DATAFILE", self.filename),
00063                 ("OPT", self.opt),
00064                 ("SVC", self.svc),
00065                 ("SEL", self.sel),
00066                 ("COLLECTION", self.collection),
00067                 ("FUN", self.fun)]
00068     def __str__(self):
00069         """
00070         Return the string representation of the file description to be passed
00071         to the application.
00072         """
00073         return " ".join(["%s='%s'" % (k, v) for k, v in self.__data__() if v])
00074 
00075 class PersistencyHelper(object):
00076     """
00077     Base class for extensions to persistency configuration in GaudiPython.
00078     """
00079     def __init__(self, types):
00080         """
00081         Define the type of persistencies supported by the instance.
00082         """
00083         self.types = set(types)
00084     def handle(self, typ):
00085         """
00086         Returns True if the current instance understands the requested
00087         persistency type.
00088         """
00089         return typ in self.types
00090 
00091 class RootPersistency(PersistencyHelper):
00092     """
00093     Implementation of PersistencyHelper based on Gaudi::RootCnvSvc.
00094     """
00095     def __init__(self):
00096         """
00097         Constructor.
00098 
00099         Declare the type of supported persistencies to the base class.
00100         """
00101         super(RootPersistency, self).__init__(["ROOT", "POOL_ROOT",
00102                                                "RootCnvSvc", "Gaudi::RootCnvSvc"])
00103         self.configured = False
00104 
00105     def configure(self, appMgr):
00106         """
00107         Basic configuration.
00108         """
00109         if not self.configured:
00110             # instantiate the required services
00111             appMgr.service('Gaudi::RootCnvSvc/RootCnvSvc')
00112             eps = appMgr.service ( 'EventPersistencySvc' )
00113             eps.CnvServices += ['RootCnvSvc']
00114             self.configured = True
00115 
00116     def formatInput(self, filenames, **kwargs):
00117         '''
00118         Translate a list of file names in a list of input descriptions.
00119 
00120         The optional parameters 'collection', 'sel' and 'fun' should be used to
00121         configure Event Tag Collection inputs.
00122 
00123         @param filenames: the list of files
00124         '''
00125         if not self.configured:
00126             raise PersistencyError("Persistency not configured")
00127         if type(filenames) is str:
00128             filenames = [filenames]
00129         fileargs = {}
00130         # check if we are accessing a collection
00131         fileargs = dict([(k, kwargs[k])
00132                          for k in ["collection", "sel", "fun"]
00133                          if k in kwargs])
00134         if fileargs:
00135             # is a collection
00136             svc = 'Gaudi::RootCnvSvc'
00137         else:
00138             svc = 'Gaudi::RootEvtSelector'
00139         return map(str,
00140                    [FileDescription(f, 'READ', svc, **fileargs)
00141                     for f in filenames])
00142 
00143     def formatOutput(self, filename, **kwargs):
00144         '''
00145         Translate a filename in an output description.
00146 
00147         @param filenames: the list of files
00148         @param lun: Logical Unit for Event Tag Collection outputs (optional)
00149         '''
00150         if not self.configured:
00151             raise PersistencyError("Persistency not configured")
00152         retval = str(FileDescription(filename, 'RECREATE', 'Gaudi::RootCnvSvc'))
00153         if 'lun' in kwargs:
00154             retval = "%s %s" % (kwargs['lun'], retval)
00155         return retval
00156 
00157 # Adding the know instances to the list of helpers
00158 add(RootPersistency())
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Mon Sep 17 2012 13:49:35 for Gaudi Framework, version v23r4 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004