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