| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

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

- File List
- File Members

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