def _checkValue(self, variableName, value): inValue = value try: value = int(value) except: raise TransformConfigError('%s value %s is not an int' % (variableName, value)) if value != float(inValue): raise TransformConfigError('%s value %s is not an int' % (variableName, value)) # check the value against list of possible values return Descriptor._checkValue(self, variableName, value)
def __init__(self, name, bases, dict): ## print "JobConfigMetaClass( self=%r, name=%r, bases=%r, dict=%r )" % (self,name,bases,dict) if '__slots__' not in dict: raise TransformConfigError( 'Class %s does not have member __slots__. Please add __slots__ = ()' ' to the class definition' % (name, )) # # add list of properties # # first add it to __slots__ slots = self.__slots__ ## print "%s.__slots__ before: = %r" % (self.__name__,slots) descrName = '__properties' if descrName not in slots: # add variable slots += (descrName, ) # synchronise dict dict['__slots__'] = slots ## print "%s.__slots__ after: = %r" % (self.__name__,slots) # then add the list itself setattr(self, descrName, []) descrList = getattr(self, descrName) # set names of properties and add them to the list of properties for n, attrib in dict.items(): # attrib = getattr(self,n) if isinstance(attrib, Descriptor): ## print "Setting name of %s.%s" % (self.__name__,n) setattr(attrib, '_Descriptor__name', n) ## print "Adding %s.%s to %s.%s" % (self.__name__,n,self.__name__,descrName) descrList.append(attrib) type.__init__(self, name, bases, dict)
def getProperty(self, name): """Get the property (the descriptor, not the value) named <name>. It raises an TransformConfigError if the class does not have proporty <name>.""" for d in self.properties(): if d.name() == name: return d raise TransformConfigError('class %s does not have property %s' % (self.__class__.__name__, name))
def _checkValue(self, variableName, value): valType = type(value).__name__ if valType != 'str': raise TransformConfigError( '%s should be a string. Got %s instead.' % (variableName, valType)) # check the value against list of possible values return Descriptor._checkValue(self, variableName, value)
def _checkValue(self, variableName, value): try: value = float(value) except Exception: raise TransformConfigError('%s value %s is not a float' % (variableName, value)) # check the value against list of possible values return Descriptor._checkValue(self, variableName, value)
def _checkType(self, variableName, value): """Add check that all entries are strings""" # check that <value> is a list or tuple value = UniqueList._checkType(self, variableName, value) # check that all entries are strings for v in value: if not isinstance(v, str): raise TransformConfigError("Entry %r in %s is not a string (but an %s)" % \ ( v, variableName, type( v ).__name__ ) ) return value
def __init__(self, name=None, metaData=None): """name is used in printout. The default name is derived from the filename of the python file where constructor is called""" JobConfig.__init__(self, name) self.__metadata = [] if metaData is not None: propertyNames = [d.name() for d in self.properties()] for m in metaData: if m not in propertyNames: raise TransformConfigError( 'Requested metadata %s is not an attribute of %s' % (m, self.name())) self.__metadata.append(m)
def _checkValue(self, variableName, value): # check that entries are allowed allowed = self.allowedValues() if allowed: for v in value: if v not in allowed: raise TransformConfigError( '%s value %r is not one of %s' % \ (variableName, value, allowed) ) # make entries unique newValue = set() #[] for v in value: newValue.add(v) return list(newValue)
def _checkValue(self, variableName, value): """Private helper function to check the value of <value>. This function is called after calling _checkType. <value> can therefore be considered to be of the correct type. This implementation checks that the value is one of the allowed values (if defined). This function can be overridden in derived class to do type & additional value checking. It has to return the value (adapted if needed) if all is OK. It has to raise a TransformConfigError exception in case of problems. <variableName> is the name of the variable that is being set and is typically only used for error messages.""" if self.__allowed and value not in self.__allowed: raise TransformConfigError( '%s value %r is not in %s' % \ (variableName, value, self.__allowed) ) return value
def _checkType(self, variableName, value): """Check that <value> is of type list of tuple, and convert to a list if it is a tuple.""" # # check types # valType = type(value).__name__ # if valType != 'list' and valType != 'tuple': # raise TransformConfigError( '%s should be a list or tuple. Got %s instead.' % \ # (variableName, valType) ) # if valType == 'tuple': # # convert to a list # value = list(value) # return value try: value.__iter__ return list(value) except: raise TransformConfigError( '%s should be a list or tuple. Got %s instead.' % \ ( variableName, type( value ).__name__ ) )