Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
15  """
16  Exception raised if the persistency type is not known to the module.
17  """
18 
19  def __init__(self, type_):
20  super(UnknownPersistency,
21  self).__init__("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,
50  fun=None):
51  '''
52  Class to hold/manipulate the file description.
53 
54  @param filename: name of the file
55  @param opt: option (READ/CREATE/RECREATE/WRITE)
56  @param svc: conversion service (or selector)
57  @param sel: selection expression
58  @param collection: collection
59  @param fun: selection class
60  '''
61  self.filename = filename
62  self.opt = opt
63  self.svc = svc
64  self.sel = sel
65  self.collection = collection
66  self.fun = fun
67 
68  def __data__(self):
69  '''
70  Return a list of pairs describing the instance.
71  '''
72  return [("DATAFILE", self.filename), ("OPT", self.opt),
73  ("SVC", self.svc), ("SEL", self.sel),
74  ("COLLECTION", self.collection), ("FUN", self.fun)]
75 
76  def __str__(self):
77  """
78  Return the string representation of the file description to be passed
79  to the application.
80  """
81  return " ".join(["%s='%s'" % (k, v) for k, v in self.__data__() if v])
82 
83 
84 class PersistencyHelper(object):
85  """
86  Base class for extensions to persistency configuration in GaudiPython.
87  """
88 
89  def __init__(self, types):
90  """
91  Define the type of persistencies supported by the instance.
92  """
93  self.types = set(types)
94 
95  def handle(self, typ):
96  """
97  Returns True if the current instance understands the requested
98  persistency type.
99  """
100  return typ in self.types
101 
102 
104  """
105  Implementation of PersistencyHelper based on Gaudi::RootCnvSvc.
106  """
107 
108  def __init__(self):
109  """
110  Constructor.
111 
112  Declare the type of supported persistencies to the base class.
113  """
114  super(RootPersistency, self).__init__(
115  ["ROOT", "POOL_ROOT", "RootCnvSvc", "Gaudi::RootCnvSvc"])
116  self.configured = False
117 
118  def configure(self, appMgr):
119  """
120  Basic configuration.
121  """
122  if not self.configured:
123  # instantiate the required services
124  appMgr.service('Gaudi::RootCnvSvc/RootCnvSvc')
125  eps = appMgr.service('EventPersistencySvc')
126  eps.CnvServices += ['RootCnvSvc']
127  self.configured = True
128 
129  def formatInput(self, filenames, **kwargs):
130  '''
131  Translate a list of file names in a list of input descriptions.
132 
133  The optional parameters 'collection', 'sel' and 'fun' should be used to
134  configure Event Tag Collection inputs.
135 
136  @param filenames: the list of files
137  '''
138  if not self.configured:
139  raise PersistencyError("Persistency not configured")
140  if type(filenames) is str:
141  filenames = [filenames]
142  fileargs = {}
143  # check if we are accessing a collection
144  fileargs = dict([(k, kwargs[k]) for k in ["collection", "sel", "fun"]
145  if k in kwargs])
146  if fileargs:
147  # is a collection
148  svc = 'Gaudi::RootCnvSvc'
149  else:
150  svc = 'Gaudi::RootEvtSelector'
151  return map(
152  str,
153  [FileDescription(f, 'READ', svc, **fileargs) for f in filenames])
154 
155  def formatOutput(self, filename, **kwargs):
156  '''
157  Translate a filename in an output description.
158 
159  @param filenames: the list of files
160  @param lun: Logical Unit for Event Tag Collection outputs (optional)
161  '''
162  if not self.configured:
163  raise PersistencyError("Persistency not configured")
164  retval = str(
165  FileDescription(filename, 'RECREATE', 'Gaudi::RootCnvSvc'))
166  if 'lun' in kwargs:
167  retval = "%s %s" % (kwargs['lun'], retval)
168  return retval
169 
170 
171 # Adding the know instances to the list of helpers
def formatOutput(self, filename, kwargs)
Definition: Persistency.py:155
def formatInput(self, filenames, kwargs)
Definition: Persistency.py:129
def __init__(self, filename, opt, svc, sel=None, collection=None, fun=None)
Definition: Persistency.py:50
struct GAUDI_API map
Parametrisation class for map-like implementation.