| The Gaudi Framework v29r0 (ff2e7097)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

Persistency.py
Go to the documentation of this file.
1 """
2 Module to configure the persistency type in GaudiPython.
3 """
4 __author__ = "Marco Clemencic <marco.clemencic@cern.ch>"
5
6
7 class PersistencyError(RuntimeError):
8 """
9 Base class for exceptions in PersistencyHelper.
10 """
11 pass
12
13
14 class UnknownPersistency(PersistencyError):
15 """
16 Exception raised if the persistency type is not known to the module.
17 """
18
19 def __init__(self, type_):
20 super(UnknownPersistency, self).__init__(
21 "Unknown persistency type %r" % type_)
22 self.type = type_
23
24
25 # internal storage for the persistency helpers
26 _implementations = []
27
28
29 def get(type_):
30 """
31 Return the PersistencyHerper implementing the given persistency type.
32 """
33 for i in _implementations:
34 if i.handle(type_):
35 return i
36 raise UnknownPersistency(type_)
37
38
39 def add(instance):
40 """
41 Function to extend the list of known helpers.
42
43 New helpers are added to the top of the list.
44 """
45 _implementations.insert(0, instance)
46
47
48 class FileDescription(object):
49 def __init__(self, filename, opt, svc, sel=None, collection=None, fun=None):
50 '''
51 Class to hold/manipulate the file description.
52
53 @param filename: name of the file
54 @param opt: option (READ/CREATE/RECREATE/WRITE)
55 @param svc: conversion service (or selector)
56 @param sel: selection expression
57 @param collection: collection
58 @param fun: selection class
59 '''
60 self.filename = filename
61 self.opt = opt
62 self.svc = svc
63 self.sel = sel
64 self.collection = collection
65 self.fun = fun
66
67 def __data__(self):
68 '''
69 Return a list of pairs describing the instance.
70 '''
71 return [("DATAFILE", self.filename),
72 ("OPT", self.opt),
73 ("SVC", self.svc),
74 ("SEL", self.sel),
75 ("COLLECTION", self.collection),
76 ("FUN", self.fun)]
77
78 def __str__(self):
79 """
80 Return the string representation of the file description to be passed
81 to the application.
82 """
83 return " ".join(["%s='%s'" % (k, v) for k, v in self.__data__() if v])
84
85
86 class PersistencyHelper(object):
87 """
88 Base class for extensions to persistency configuration in GaudiPython.
89 """
90
91 def __init__(self, types):
92 """
93 Define the type of persistencies supported by the instance.
94 """
95 self.types = set(types)
96
97 def handle(self, typ):
98 """
99 Returns True if the current instance understands the requested
100 persistency type.
101 """
102 return typ in self.types
103
104
105 class RootPersistency(PersistencyHelper):
106 """
107 Implementation of PersistencyHelper based on Gaudi::RootCnvSvc.
108 """
109
110 def __init__(self):
111 """
112 Constructor.
113
114 Declare the type of supported persistencies to the base class.
115 """
116 super(RootPersistency, self).__init__(["ROOT", "POOL_ROOT",
117 "RootCnvSvc", "Gaudi::RootCnvSvc"])
118 self.configured = False
119
120 def configure(self, appMgr):
121 """
122 Basic configuration.
123 """
124 if not self.configured:
125 # instantiate the required services
126 appMgr.service('Gaudi::RootCnvSvc/RootCnvSvc')
127 eps = appMgr.service('EventPersistencySvc')
128 eps.CnvServices += ['RootCnvSvc']
129 self.configured = True
130
131 def formatInput(self, filenames, **kwargs):
132 '''
133 Translate a list of file names in a list of input descriptions.
134
135 The optional parameters 'collection', 'sel' and 'fun' should be used to
136 configure Event Tag Collection inputs.
137
138 @param filenames: the list of files
139 '''
140 if not self.configured:
141 raise PersistencyError("Persistency not configured")
142 if type(filenames) is str:
143 filenames = [filenames]
144 fileargs = {}
145 # check if we are accessing a collection
146 fileargs = dict([(k, kwargs[k])
147 for k in ["collection", "sel", "fun"]
148 if k in kwargs])
149 if fileargs:
150 # is a collection
151 svc = 'Gaudi::RootCnvSvc'
152 else:
153 svc = 'Gaudi::RootEvtSelector'
154 return map(str,
155 [FileDescription(f, 'READ', svc, **fileargs)
156 for f in filenames])
157
158 def formatOutput(self, filename, **kwargs):
159 '''
160 Translate a filename in an output description.
161
162 @param filenames: the list of files
163 @param lun: Logical Unit for Event Tag Collection outputs (optional)
164 '''
165 if not self.configured:
166 raise PersistencyError("Persistency not configured")
167 retval = str(FileDescription(
168 filename, 'RECREATE', 'Gaudi::RootCnvSvc'))
169 if 'lun' in kwargs:
170 retval = "%s %s" % (kwargs['lun'], retval)
171 return retval
172
173
174 # Adding the know instances to the list of helpers
175 add(RootPersistency())
GaudiPython.Persistency.PersistencyHelper.handle
def handle(self, typ)
Definition: Persistency.py:97
GaudiPython.Persistency.FileDescription.collection
collection
Definition: Persistency.py:64
GaudiPython.Persistency.UnknownPersistency.type
type
Definition: Persistency.py:22
GaudiPython.Persistency.FileDescription.__data__
def __data__(self)
Definition: Persistency.py:67
GaudiPython.Persistency.PersistencyHelper.__init__
def __init__(self, types)
Definition: Persistency.py:91
GaudiPython.Persistency.FileDescription.filename
filename
Definition: Persistency.py:60
GaudiPython.Persistency.FileDescription.__str__
def __str__(self)
Definition: Persistency.py:78
GaudiPython.Persistency.PersistencyHelper.types
types
Definition: Persistency.py:95
GaudiPython.Persistency.RootPersistency.formatOutput
def formatOutput(self, filename, kwargs)
Definition: Persistency.py:158
GaudiPython.Persistency.UnknownPersistency.__init__
def __init__(self, type_)
Definition: Persistency.py:19
GaudiPython.Persistency.RootPersistency.formatInput
def formatInput(self, filenames, kwargs)
Definition: Persistency.py:131
GaudiPython.Persistency.FileDescription.svc
svc
Definition: Persistency.py:62
GaudiPython.Persistency.add
def add(instance)
Definition: Persistency.py:39
GaudiPython.Persistency.PersistencyHelper
Definition: Persistency.py:86
GaudiPython.Persistency.FileDescription.__init__
def __init__(self, filename, opt, svc, sel=None, collection=None, fun=None)
Definition: Persistency.py:49
Containers::map
struct GAUDI_API map
Parametrisation class for map-like implementation.
Definition: KeyedObjectManager.h:26
GaudiPython.Persistency.RootPersistency.configure
def configure(self, appMgr)
Definition: Persistency.py:120
gaudirun.type
type
Definition: gaudirun.py:133
GaudiPython.Persistency.UnknownPersistency
Definition: Persistency.py:14
GaudiPython.Persistency.RootPersistency
Definition: Persistency.py:105
GaudiPython.Persistency.RootPersistency.configured
configured
Definition: Persistency.py:118
GaudiPython.Persistency.get
def get(type_)
Definition: Persistency.py:29
GaudiPython.Persistency.FileDescription.opt
opt
Definition: Persistency.py:61
GaudiPython.Persistency.FileDescription.sel
sel
Definition: Persistency.py:63
GaudiPython.Persistency.PersistencyError
Definition: Persistency.py:7
GaudiPython.Persistency.FileDescription
Definition: Persistency.py:48
GaudiPython.Persistency.FileDescription.fun
fun
Definition: Persistency.py:65
GaudiPython.Persistency.RootPersistency.__init__
def __init__(self)
Definition: Persistency.py:110