The Gaudi Framework  v36r1 (3e2fb5a8)
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  pass
22 
23 
25  """
26  Exception raised if the persistency type is not known to the module.
27  """
28 
29  def __init__(self, type_):
30  super(UnknownPersistency,
31  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,
60  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 [("DATAFILE", self.filename), ("OPT", self.opt),
83  ("SVC", self.svc), ("SEL", self.sel),
84  ("COLLECTION", self.collection), ("FUN", self.fun)]
85 
86  def __str__(self):
87  """
88  Return the string representation of the file description to be passed
89  to the application.
90  """
91  return " ".join(["%s='%s'" % (k, v) for k, v in self.__data__() if v])
92 
93 
94 class PersistencyHelper(object):
95  """
96  Base class for extensions to persistency configuration in GaudiPython.
97  """
98 
99  def __init__(self, types):
100  """
101  Define the type of persistencies supported by the instance.
102  """
103  self.types = set(types)
104 
105  def handle(self, typ):
106  """
107  Returns True if the current instance understands the requested
108  persistency type.
109  """
110  return typ in self.types
111 
112 
114  """
115  Implementation of PersistencyHelper based on Gaudi::RootCnvSvc.
116  """
117 
118  def __init__(self):
119  """
120  Constructor.
121 
122  Declare the type of supported persistencies to the base class.
123  """
124  super(RootPersistency, self).__init__(
125  ["ROOT", "POOL_ROOT", "RootCnvSvc", "Gaudi::RootCnvSvc"])
126  self.configured = False
127 
128  def configure(self, appMgr):
129  """
130  Basic configuration.
131  """
132  if not self.configured:
133  # instantiate the required services
134  appMgr.service('Gaudi::RootCnvSvc/RootCnvSvc')
135  eps = appMgr.service('EventPersistencySvc')
136  eps.CnvServices += ['RootCnvSvc']
137  self.configured = True
138 
139  def formatInput(self, filenames, **kwargs):
140  '''
141  Translate a list of file names in a list of input descriptions.
142 
143  The optional parameters 'collection', 'sel' and 'fun' should be used to
144  configure Event Tag Collection inputs.
145 
146  @param filenames: the list of files
147  '''
148  if not self.configured:
149  raise PersistencyError("Persistency not configured")
150  if type(filenames) is str:
151  filenames = [filenames]
152  fileargs = {}
153  # check if we are accessing a collection
154  fileargs = dict([(k, kwargs[k]) for k in ["collection", "sel", "fun"]
155  if k in kwargs])
156  if fileargs:
157  # is a collection
158  svc = 'Gaudi::RootCnvSvc'
159  else:
160  svc = 'Gaudi::RootEvtSelector'
161  return [
162  str(FileDescription(f, 'READ', svc, **fileargs)) for f in filenames
163  ]
164 
165  def formatOutput(self, filename, **kwargs):
166  '''
167  Translate a filename in an output description.
168 
169  @param filenames: the list of files
170  @param lun: Logical Unit for Event Tag Collection outputs (optional)
171  '''
172  if not self.configured:
173  raise PersistencyError("Persistency not configured")
174  retval = str(
175  FileDescription(filename, 'RECREATE', 'Gaudi::RootCnvSvc'))
176  if 'lun' in kwargs:
177  retval = "%s %s" % (kwargs['lun'], retval)
178  return retval
179 
180 
181 # Adding the know instances to the list of helpers
GaudiPython.Persistency.UnknownPersistency.type
type
Definition: Persistency.py:32
GaudiPython.Persistency.RootPersistency
Definition: Persistency.py:113
GaudiPython.Persistency.FileDescription.svc
svc
Definition: Persistency.py:72
GaudiPython.Persistency.RootPersistency.configure
def configure(self, appMgr)
Definition: Persistency.py:128
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:118
GaudiPython.Persistency.UnknownPersistency.__init__
def __init__(self, type_)
Definition: Persistency.py:29
GaudiPython.Persistency.RootPersistency.configured
configured
Definition: Persistency.py:126
GaudiPython.Persistency.RootPersistency.formatInput
def formatInput(self, filenames, **kwargs)
Definition: Persistency.py:139
GaudiPython.Persistency.FileDescription.fun
fun
Definition: Persistency.py:75
GaudiPython.Persistency.RootPersistency.formatOutput
def formatOutput(self, filename, **kwargs)
Definition: Persistency.py:165
GaudiPython.Persistency.FileDescription.sel
sel
Definition: Persistency.py:73
GaudiPython.Persistency.FileDescription.__str__
def __str__(self)
Definition: Persistency.py:86
GaudiPython.Persistency.PersistencyError
Definition: Persistency.py:17
GaudiPython.Persistency.UnknownPersistency
Definition: Persistency.py:24
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:105
gaudirun.type
type
Definition: gaudirun.py:154
GaudiPython.Persistency.FileDescription.opt
opt
Definition: Persistency.py:71
GaudiPython.Persistency.PersistencyHelper.__init__
def __init__(self, types)
Definition: Persistency.py:99
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:103
GaudiPython.Persistency.PersistencyHelper
Definition: Persistency.py:94
GaudiPython.Persistency.add
def add(instance)
Definition: Persistency.py:49
GaudiPython.Persistency.FileDescription.__data__
def __data__(self)
Definition: Persistency.py:78