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