00001
00002 __all__ = [
00003 "GaudiHandle",
00004 "PublicToolHandle", "PrivateToolHandle",
00005 "ServiceHandle",
00006 "GaudiHandleArray",
00007 "ServiceHandleArray",
00008 "PublicToolHandleArray", "PrivateToolHandleArray",
00009 ]
00010 __version__ = "$Revision: 1.6 $"
00011 __doc__ = """The python module holding python bindings to XyzHandles"""
00012
00013 from os import linesep
00014
00015 class GaudiHandle(object):
00016 componentType = "Unspecified"
00017 isPublic = True
00018
00019 def __init__(self,typeAndName):
00020 object.__init__(self)
00021 if hasattr(typeAndName,"toStringProperty"):
00022
00023 typeAndName = typeAndName.toStringProperty()
00024 if type(typeAndName) != str:
00025 raise TypeError("Argument to %s must be a string. Got a %s instead" % \
00026 ( self.__class__.__name__, type(typeAndName).__name__) )
00027 self.typeAndName = typeAndName
00028
00029 def __repr__(self):
00030 return "%s(%r)" % (self.__class__.__name__,self.toStringProperty())
00031
00032 def __str__(self):
00033
00034
00035 return self.toStringProperty()
00036
00037
00038
00039
00040 def toStringProperty(self):
00041 return self.typeAndName
00042
00043 def getType(self):
00044 """Get the 'type' part of the \"type/name\" string."""
00045 slash = self.typeAndName.find('/')
00046 if slash != -1:
00047
00048 return self.typeAndName[0:slash]
00049 else:
00050
00051 return self.typeAndName
00052
00053 def getName(self):
00054 """Get the 'name' part of the \"type/name\" string.
00055 If only a type is given, this will be returned as the name.
00056 If the \"type/name\" string is empty, it will return an emtpy string."""
00057 slash = self.typeAndName.find('/')
00058 if slash != -1:
00059
00060 return self.typeAndName[slash+1:]
00061 else:
00062
00063 return self.typeAndName
00064
00065 def getGaudiHandle(self):
00066 return self
00067
00068 def getFullName(self):
00069 return self.toStringProperty()
00070
00071 class PublicToolHandle(GaudiHandle):
00072 __slots__ = ()
00073 componentType = "AlgTool"
00074 isPublic = True
00075
00076 def __init__(self, toolTypeAndName=''):
00077 GaudiHandle.__init__( self, toolTypeAndName )
00078
00079
00080 class PrivateToolHandle(GaudiHandle):
00081 __slots__ = ()
00082 componentType = "AlgTool"
00083 isPublic = False
00084
00085 def __init__(self, toolTypeAndName=''):
00086 GaudiHandle.__init__( self, toolTypeAndName )
00087
00088
00089 class ServiceHandle(GaudiHandle):
00090 __slots__ = ()
00091 componentType = "Service"
00092 isPublic = True
00093
00094 def __init__(self, serviceName=''):
00095 GaudiHandle.__init__( self, serviceName )
00096
00097
00098
00099
00100 class GaudiHandleArray(list):
00101 """A list of GaudiHandles. Only handles of one type are allowed, as specified by self.__class__.handleType
00102 """
00103 __slots__ = ( 'typesAndNames' )
00104 handleType = None
00105
00106 def __init__(self,typesAndNames=None):
00107 if typesAndNames is None: typesAndNames = []
00108 list.__init__(self)
00109
00110 if type(typesAndNames) != list:
00111 raise TypeError("Argument to %s must be a list. Got a %s instead" % \
00112 ( self.__class__.__name__, type(typesAndNames).__name__) )
00113
00114 for tn in typesAndNames: self.append( tn )
00115
00116 def __repr__(self):
00117 """Return class name with list of type/name strings as argument"""
00118 rep = self.__class__.__name__ + '(['
00119 for h in self:
00120 rep += repr(h.toStringProperty()) + ','
00121
00122 if rep[-1] == ',': rep = rep[:-1]
00123 return rep + '])'
00124
00125 def __str__(self):
00126 """Print entries, one per line"""
00127 shortName = self.__class__.__name__
00128 return "%s:%s" % (shortName, linesep + linesep.join([str(s) for s in self]))
00129
00130 def __getitem__(self,index):
00131 if type(index) == str:
00132
00133 for h in self:
00134 if h.getName() == index:
00135 return h
00136 raise IndexError( "%s does not have a %s with instance name %s" % \
00137 (self.__class__.__name__, self.handleType.componentType, index) )
00138 else:
00139 return list.__getitem__(self,index)
00140
00141 def __delitem__( self, key ):
00142 super( GaudiHandleArray, self ).__delitem__( self.index(self[key]) )
00143
00144 def __iadd__(self,array):
00145 arrayType = type(array)
00146 if arrayType == list or arrayType == type(self):
00147 for v in array:
00148 self.append( v )
00149 else:
00150 raise TypeError( "Can not add a %s to a %s" % (arrayType.__name__, self.__class__.__name__) )
00151
00152 return self
00153
00154 def append( self, value ):
00155 """Only allow appending compatible types. It accepts a string, a handle or a configurable."""
00156 if type(value) == str:
00157
00158 value = self.__class__.handleType(value)
00159 elif type(value) == self.__class__.handleType:
00160 pass
00161 elif isinstance( value, GaudiHandle ):
00162
00163 raise TypeError( "Can not add a %s to a %s" % (value.__class__.__name__, self.__class__.__name__) )
00164 elif value.getGaudiType() != self.__class__.handleType.componentType:
00165
00166 raise TypeError( "Can not append %s (%s) to a %s" % \
00167 (value.__class__.__name__, value.getGaudiType(), self.__class__.__name__) )
00168 elif hasattr(value,'isPublic'):
00169
00170 pop = value.isPublic() and 'Public' or 'Private'
00171 if value.isPublic() != self.__class__.handleType.isPublic:
00172 raise TypeError( "Can not append %s (%s %s) to a %s" % \
00173 (value.__class__.__name__, pop, value.getGaudiType(), self.__class__.__name__) )
00174
00175
00176 try:
00177 oldValue = self.__getitem__( value.getName() )
00178 except IndexError:
00179
00180 list.append( self, value )
00181 else:
00182 print "%s WARNING %r with instance name %r already in list. Not adding %r" % \
00183 (self.__class__.__name__, oldValue, oldValue.getName(), value)
00184
00185
00186 def isPublic(self):
00187 return self.__class__.handleType.isPublic
00188
00189
00190
00191
00192 def toStringProperty(self):
00193 rep = '['
00194
00195 for v in self:
00196 rep += repr( v.toStringProperty() ) + ','
00197
00198 if rep[-1] == ',': rep = rep[:-1]
00199 return rep + ']'
00200
00201
00202 def __getstate__ (self):
00203 return { 'typesAndNames' : self.typesAndNames }
00204
00205 def __setstate__ ( self, dict ):
00206 self.typesAndNames = dict[ 'typesAndNames' ]
00207
00208
00209 class ServiceHandleArray(GaudiHandleArray):
00210 __slots__ = ()
00211 handleType = ServiceHandle
00212
00213 def __init__(self, serviceTypesAndNames=None):
00214 GaudiHandleArray.__init__( self, serviceTypesAndNames )
00215
00216 class PublicToolHandleArray(GaudiHandleArray):
00217 __slots__ = ()
00218 handleType = PublicToolHandle
00219
00220 def __init__(self, toolTypesAndNames=None):
00221 GaudiHandleArray.__init__( self, toolTypesAndNames )
00222
00223
00224 class PrivateToolHandleArray(GaudiHandleArray):
00225 __slots__ = ()
00226 handleType = PrivateToolHandle
00227
00228 def __init__(self, toolTypesAndNames=None):
00229 GaudiHandleArray.__init__( self, toolTypesAndNames )