| The Gaudi Framework v32r1 (f65d50dc)

GaudiHandles.py
Go to the documentation of this file.
1 # explicit list for wildcard imports
2 __all__ = [
3 "GaudiHandle",
4 "PublicToolHandle",
5 "PrivateToolHandle",
6 "ServiceHandle",
7 "GaudiHandleArray",
8 "ServiceHandleArray",
9 "PublicToolHandleArray",
10 "PrivateToolHandleArray",
11 ]
12 __doc__ = """The python module holding python bindings to XyzHandles"""
13
14 from os import linesep
15
16
17 class GaudiHandle(object):
18 componentType = "Unspecified" # must be overridden by derived class
19 isPublic = True # can be overridden by derived class
20
21 def __init__(self, typeAndName):
22 object.__init__(self)
23 if hasattr(typeAndName, "toStringProperty"):
24 # this is a GaudiHandle or equivalent
25 typeAndName = typeAndName.toStringProperty()
26 if type(typeAndName) != str:
27 raise TypeError(
28 "Argument to %s must be a string. Got a %s instead" %
29 (self.__class__.__name__, type(typeAndName).__name__))
30 self.typeAndName = typeAndName
31
32 def __repr__(self):
33 return "%s(%r)" % (self.__class__.__name__, self.toStringProperty())
34
35 def __str__(self):
36 # FIXME: (Patch #1668) this creates problem with 2.5
37 # return "%s:%s" % (self.__class__.__name__, self.toStringProperty())
38 return self.toStringProperty()
39
40 #
41 # Several member functions which are the same as Configurables
42 #
43 def toStringProperty(self):
44 return self.typeAndName
45
46 def getType(self):
47 """Get the 'type' part of the \"type/name\" string."""
48 slash = self.typeAndName.find('/')
49 if slash != -1:
50 # includes name. Return part before /
51 return self.typeAndName[0:slash]
52 else:
53 # only type is given, return full string
54 return self.typeAndName
55
56 def getName(self):
57 """Get the 'name' part of the \"type/name\" string.
58 If only a type is given, this will be returned as the name.
59 If the \"type/name\" string is empty, it will return an emtpy string."""
60 slash = self.typeAndName.find('/')
61 if slash != -1:
62 # explicit name. Return part after the /
63 return self.typeAndName[slash + 1:]
64 else:
65 # only type is given. return type as default name
66 return self.typeAndName
67
68 def getGaudiHandle(self):
69 return self
70
71 def getFullName(self):
72 return self.toStringProperty()
73
74
75 class PublicToolHandle(GaudiHandle):
76 __slots__ = ()
77 componentType = "AlgTool"
78 isPublic = True
79
80 def __init__(self, toolTypeAndName=''):
81 GaudiHandle.__init__(self, toolTypeAndName)
82
83
84 class PrivateToolHandle(GaudiHandle):
85 __slots__ = ()
86 componentType = "AlgTool"
87 isPublic = False
88
89 def __init__(self, toolTypeAndName=''):
90 GaudiHandle.__init__(self, toolTypeAndName)
91
92
93 class ServiceHandle(GaudiHandle):
94 __slots__ = ()
95 componentType = "Service"
96 isPublic = True
97
98 def __init__(self, serviceName=''):
99 GaudiHandle.__init__(self, serviceName)
100
101
102 #
103 # The HandleArrays
104 #
105
106
107 class GaudiHandleArray(list):
108 """A list of GaudiHandles. Only handles of one type are allowed, as specified by self.__class__.handleType
109 """
110 __slots__ = ('typesAndNames')
111 handleType = None # must be set by derived class to the handle type
112
113 def __init__(self, typesAndNames=None):
114 if typesAndNames is None:
115 typesAndNames = []
116 list.__init__(self)
117 # check the type
118 if type(typesAndNames) != list:
119 raise TypeError(
120 "Argument to %s must be a list. Got a %s instead" %
121 (self.__class__.__name__, type(typesAndNames).__name__))
122 # add entries to list
123 for tn in typesAndNames:
124 self.append(tn)
125
126 def __repr__(self):
127 """Return class name with list of type/name strings as argument"""
128 rep = self.__class__.__name__ + '(['
129 for h in self:
130 rep += repr(h.toStringProperty()) + ','
131 # remove last comma
132 if rep[-1] == ',':
133 rep = rep[:-1]
134 return rep + '])'
135
136 def __str__(self):
137 """Print entries, one per line"""
138 shortName = self.__class__.__name__
139 return "%s:%s" % (shortName,
140 linesep + linesep.join([str(s) for s in self]))
141
142 def __getitem__(self, index):
143 if type(index) == str:
144 # seach by instance name
145 for h in self:
146 if h.getName() == index:
147 return h
148 raise IndexError("%s does not have a %s with instance name %s" % (
149 self.__class__.__name__, self.handleType.componentType, index))
150 else:
151 return list.__getitem__(self, index)
152
153 def __delitem__(self, key):
154 super(GaudiHandleArray, self).__delitem__(self.index(self[key]))
155
156 def __iadd__(self, array):
157 arrayType = type(array)
158 if arrayType == list or arrayType == type(self):
159 for v in array:
160 self.append(v)
161 else:
162 raise TypeError("Can not add a %s to a %s" %
163 (arrayType.__name__, self.__class__.__name__))
164
165 return self
166
167 def append(self, value):
168 """Only allow appending compatible types. It accepts a string, a handle or a configurable."""
169 if type(value) == str:
170 # convert string to handle
171 value = self.__class__.handleType(value)
172 elif type(value) == self.__class__.handleType:
173 pass # leave handle as-is
174 elif isinstance(value, GaudiHandle):
175 # do not allow different type of handles
176 raise TypeError(
177 "Can not add a %s to a %s" % (value.__class__.__name__,
178 self.__class__.__name__))
179 elif value.getGaudiType() != self.__class__.handleType.componentType:
180 # assume it is a configurable: allow only correct types
181 raise TypeError("Can not append %s (%s) to a %s" %
182 (value.__class__.__name__, value.getGaudiType(),
183 self.__class__.__name__))
184 elif hasattr(value, 'isPublic'):
185 # check public vs private if applicable for this configurable
186 pop = value.isPublic() and 'Public' or 'Private'
187 if value.isPublic() != self.__class__.handleType.isPublic:
188 raise TypeError(
189 "Can not append %s (%s %s) to a %s" %
190 (value.__class__.__name__, pop, value.getGaudiType(),
191 self.__class__.__name__))
192
193 # check that an instance name appears only once in the list
194 try:
195 oldValue = self.__getitem__(value.getName())
196 except IndexError:
197 # not yet there, so add it
198 list.append(self, value)
199 else:
200 print "%s WARNING %r with instance name %r already in list. Not adding %r" % \
201 (self.__class__.__name__, oldValue, oldValue.getName(), value)
202
203 def isPublic(self):
204 return self.__class__.handleType.isPublic
205
206 #
207 # Member functions which are the same as Configurables
208 #
209 def toStringProperty(self):
210 rep = '['
211 # add entries
212 for v in self:
213 rep += repr(v.toStringProperty()) + ','
214 # remove last comma
215 if rep[-1] == ',':
216 rep = rep[:-1]
217 return rep + ']'
218
219 # pickle support
220 def __getstate__(self):
221 return {'typesAndNames': self.typesAndNames}
222
223 def __setstate__(self, dict):
224 self.typesAndNames = dict['typesAndNames']
225
226
227 class ServiceHandleArray(GaudiHandleArray):
228 __slots__ = ()
229 handleType = ServiceHandle
230
231 def __init__(self, serviceTypesAndNames=None):
232 GaudiHandleArray.__init__(self, serviceTypesAndNames)
233
234
235 class PublicToolHandleArray(GaudiHandleArray):
236 __slots__ = ()
237 handleType = PublicToolHandle
238
239 def __init__(self, toolTypesAndNames=None):
240 GaudiHandleArray.__init__(self, toolTypesAndNames)
241
242
243 class PrivateToolHandleArray(GaudiHandleArray):
244 __slots__ = ()
245 handleType = PrivateToolHandle
246
247 def __init__(self, toolTypesAndNames=None):
248 GaudiHandleArray.__init__(self, toolTypesAndNames)
GaudiKernel.GaudiHandles.PublicToolHandleArray
Definition: GaudiHandles.py:235
GaudiKernel.GaudiHandles.GaudiHandle.getType
def getType(self)
Definition: GaudiHandles.py:46
GaudiKernel.GaudiHandles.GaudiHandle.__repr__
def __repr__(self)
Definition: GaudiHandles.py:32
GaudiKernel.GaudiHandles.GaudiHandle
Definition: GaudiHandles.py:17
GaudiKernel.GaudiHandles.PrivateToolHandle.__init__
def __init__(self, toolTypeAndName='')
Definition: GaudiHandles.py:89
GaudiKernel.GaudiHandles.ServiceHandleArray.__init__
def __init__(self, serviceTypesAndNames=None)
Definition: GaudiHandles.py:231
GaudiKernel.GaudiHandles.PrivateToolHandle
Definition: GaudiHandles.py:84
GaudiKernel.GaudiHandles.GaudiHandleArray.__repr__
def __repr__(self)
Definition: GaudiHandles.py:126
GaudiKernel.GaudiHandles.PublicToolHandleArray.__init__
def __init__(self, toolTypesAndNames=None)
Definition: GaudiHandles.py:239
GaudiKernel.GaudiHandles.PublicToolHandle.__init__
def __init__(self, toolTypeAndName='')
Definition: GaudiHandles.py:80
GaudiKernel.GaudiHandles.ServiceHandleArray
Definition: GaudiHandles.py:227
GaudiKernel.GaudiHandles.GaudiHandleArray.toStringProperty
def toStringProperty(self)
Definition: GaudiHandles.py:209
GaudiKernel.GaudiHandles.GaudiHandleArray.__setstate__
def __setstate__(self, dict)
Definition: GaudiHandles.py:223
GaudiKernel.GaudiHandles.GaudiHandle.toStringProperty
def toStringProperty(self)
Definition: GaudiHandles.py:43
GaudiKernel.GaudiHandles.GaudiHandleArray
Definition: GaudiHandles.py:107
GaudiKernel.GaudiHandles.PrivateToolHandleArray.__init__
def __init__(self, toolTypesAndNames=None)
Definition: GaudiHandles.py:247
GaudiKernel.GaudiHandles.GaudiHandleArray.typesAndNames
typesAndNames
Definition: GaudiHandles.py:224
gaudirun.type
type
Definition: gaudirun.py:142
GaudiKernel.GaudiHandles.GaudiHandleArray.isPublic
def isPublic(self)
Definition: GaudiHandles.py:203
GaudiKernel.GaudiHandles.GaudiHandleArray.__init__
def __init__(self, typesAndNames=None)
Definition: GaudiHandles.py:113
GaudiKernel.GaudiHandles.GaudiHandleArray.__iadd__
def __iadd__(self, array)
Definition: GaudiHandles.py:156
GaudiKernel.GaudiHandles.PublicToolHandle
Definition: GaudiHandles.py:75
GaudiKernel.GaudiHandles.GaudiHandle.typeAndName
typeAndName
Definition: GaudiHandles.py:30
GaudiKernel.GaudiHandles.GaudiHandleArray.__getitem__
def __getitem__(self, index)
Definition: GaudiHandles.py:142
GaudiKernel.GaudiHandles.PrivateToolHandleArray
Definition: GaudiHandles.py:243
GaudiKernel.GaudiHandles.ServiceHandle
Definition: GaudiHandles.py:93
GaudiKernel.GaudiHandles.GaudiHandle.getGaudiHandle
def getGaudiHandle(self)
Definition: GaudiHandles.py:68
GaudiKernel.GaudiHandles.GaudiHandle.__str__
def __str__(self)
Definition: GaudiHandles.py:35
GaudiKernel.GaudiHandles.GaudiHandle.getName
def getName(self)
Definition: GaudiHandles.py:56
GaudiKernel.GaudiHandles.GaudiHandle.__init__
def __init__(self, typeAndName)
Definition: GaudiHandles.py:21
GaudiKernel.GaudiHandles.GaudiHandleArray.__str__
def __str__(self)
Definition: GaudiHandles.py:136
GaudiKernel.GaudiHandles.GaudiHandle.getFullName
def getFullName(self)
Definition: GaudiHandles.py:71
GaudiKernel.GaudiHandles.GaudiHandleArray.append
def append(self, value)
Definition: GaudiHandles.py:167
GaudiKernel.GaudiHandles.GaudiHandleArray.handleType
handleType
Definition: GaudiHandles.py:111
GaudiKernel.GaudiHandles.ServiceHandle.__init__
def __init__(self, serviceName='')
Definition: GaudiHandles.py:98
GaudiKernel.GaudiHandles.GaudiHandleArray.__delitem__
def __delitem__(self, key)
Definition: GaudiHandles.py:153
GaudiKernel.GaudiHandles.GaudiHandleArray.__getstate__
def __getstate__(self)
Definition: GaudiHandles.py:220