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