The Gaudi Framework  master (37c0b60a)
Persistency.py
Go to the documentation of this file.
1 
11 """
12 Module to configure the persistency type in GaudiPython.
13 """
14 
15 __author__ = "Marco Clemencic <marco.clemencic@cern.ch>"
16 
17 
18 class 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 
40 def 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 
50 def 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 
59 class 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 
99 class 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
GaudiPython.Persistency.UnknownPersistency.type
type
Definition: Persistency.py:33
GaudiPython.Persistency.RootPersistency
Definition: Persistency.py:118
GaudiPython.Persistency.FileDescription.svc
svc
Definition: Persistency.py:73
GaudiPython.Persistency.RootPersistency.configure
def configure(self, appMgr)
Definition: Persistency.py:134
GaudiPython.Persistency.FileDescription.__init__
def __init__(self, filename, opt, svc, sel=None, collection=None, fun=None)
Definition: Persistency.py:60
GaudiPython.Persistency.RootPersistency.__init__
def __init__(self)
Definition: Persistency.py:123
GaudiPython.Persistency.UnknownPersistency.__init__
def __init__(self, type_)
Definition: Persistency.py:31
GaudiPython.Persistency.RootPersistency.configured
configured
Definition: Persistency.py:132
GaudiPython.Persistency.RootPersistency.formatInput
def formatInput(self, filenames, **kwargs)
Definition: Persistency.py:145
GaudiPython.Persistency.FileDescription.fun
fun
Definition: Persistency.py:76
GaudiPython.Persistency.RootPersistency.formatOutput
def formatOutput(self, filename, **kwargs)
Definition: Persistency.py:170
GaudiPython.Persistency.FileDescription.sel
sel
Definition: Persistency.py:74
GaudiPython.Persistency.FileDescription.__str__
def __str__(self)
Definition: Persistency.py:91
GaudiPython.Persistency.PersistencyError
Definition: Persistency.py:18
GaudiPython.Persistency.UnknownPersistency
Definition: Persistency.py:26
GaudiPython.Persistency.FileDescription.filename
filename
Definition: Persistency.py:71
GaudiPython.Persistency.FileDescription.collection
collection
Definition: Persistency.py:75
GaudiPython.Persistency.PersistencyHelper.handle
def handle(self, typ)
Definition: Persistency.py:110
GaudiPython.Persistency.FileDescription.opt
opt
Definition: Persistency.py:72
GaudiPython.Persistency.PersistencyHelper.__init__
def __init__(self, types)
Definition: Persistency.py:104
GaudiPython.Persistency.get
def get(type_)
Definition: Persistency.py:40
GaudiPython.Persistency.FileDescription
Definition: Persistency.py:59
GaudiPython.Persistency.PersistencyHelper.types
types
Definition: Persistency.py:108
GaudiPython.Persistency.PersistencyHelper
Definition: Persistency.py:99
GaudiPython.Persistency.add
def add(instance)
Definition: Persistency.py:50
GaudiPython.Persistency.FileDescription.__data__
def __data__(self)
Definition: Persistency.py:78