Gaudi Framework, version v20r4

Generated: 8 Jan 2009

GaudiHandles.py

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

Generated at Thu Jan 8 17:44:21 2009 for Gaudi Framework, version v20r4 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004