| The Gaudi Framework v29r1 (5cd95fa2)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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