| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

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         return self.toStringProperty()
00129 
00130     def __getitem__(self,index):
00131         if type(index) == str:
00132             # seach by instance name
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             # convert string to handle
00158             value = self.__class__.handleType(value)
00159         elif type(value) == self.__class__.handleType:
00160             pass # leave handle as-is
00161         elif isinstance( value, GaudiHandle ):
00162             # do not allow different type of handles
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             # assume it is a configurable: allow only correct types
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             # check public vs private if applicable for this configurable
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         # check that an instance name appears only once in the list
00176         try:
00177             oldValue = self.__getitem__( value.getName() )
00178         except IndexError:
00179             # not yet there, so add it
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     # Member functions which are the same as Configurables
00191     #
00192     def toStringProperty(self):
00193         rep = '['
00194         # add entries
00195         for v in self:
00196             rep += repr( v.toStringProperty() ) + ','
00197         # remove last comma
00198         if rep[-1] == ',': rep = rep[:-1]
00199         return rep + ']'
00200 
00201     # pickle support
00202     def __getstate__ (self):
00203         return { 'typesAndNames' : self.typesAndNames }
00204 
00205     def __setstate__ ( self, dict ):
00206         self.typesAndNames = dict[ 'typesAndNames' ]
00207 
00208 
00209 class PublicToolHandleArray(GaudiHandleArray):
00210     __slots__ = ()
00211     handleType = PublicToolHandle
00212     
00213     def __init__(self, toolTypesAndNames=None):
00214         GaudiHandleArray.__init__( self, toolTypesAndNames )
00215 
00216 
00217 class PrivateToolHandleArray(GaudiHandleArray):
00218     __slots__ = ()
00219     handleType = PrivateToolHandle
00220         
00221     def __init__(self, toolTypesAndNames=None):
00222         GaudiHandleArray.__init__( self, toolTypesAndNames )
00223 
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 19:56:58 2011 for GaudiKernel by doxygen 1.4.7