def migrate_attribute(self, property, metabuilder, instance): """ Method migrate an attribute from the metabuilder instance to the instance created by itself. :param property: object to set the code given in the parameters of this method :type property: <type 'property'> :param metabuilder: instance of a metabuilder where to extract the methods and validators of the attribute to migrate :type metabuilder: <type 'Metabuilder'> :param instance: instance created that will hold the property and the validators :type instance: <type 'instance'> """ for method in [ getattr(metabuilder, m) for m in getMethodsByName(metabuilder, property) ]: if metabuilder._prefix in method.__name__: setattr(instance, method.__name__ + getMeta_attr_name(property), MethodType(unbind(method), metabuilder)) else: setattr(instance, method.__name__, getattr(metabuilder, method.__name__)) getter = getattr(instance, 'get{0}'.format(property)) setter = getattr(instance, 'set{0}'.format(property)) self.set_property(instance, property, getter, setter, getattr(metabuilder, getMeta_attr_name(property)))
def migrate_attribute(self, property, metabuilder, instance): """ Method migrate an attribute from the metabuilder instance to the instance created by itself. :param property: object to set the code given in the parameters of this method :type property: <type 'property'> :param metabuilder: instance of a metabuilder where to extract the methods and validators of the attribute to migrate :type metabuilder: <type 'Metabuilder'> :param instance: instance created that will hold the property and the validators :type instance: <type 'instance'> """ for method in [getattr(metabuilder, m) for m in getMethodsByName(metabuilder, property)]: if metabuilder._prefix in method.__name__: setattr(instance, method.__name__ + getMeta_attr_name(property), MethodType(unbind(method), metabuilder)) else: setattr(instance, method.__name__, getattr(metabuilder, method.__name__)) getter = getattr(instance, 'get{0}'.format(property)) setter = getattr(instance, 'set{0}'.format(property)) self.set_property(instance, property, getter, setter, getattr(metabuilder, getMeta_attr_name(property)))
def set_property(self, obj, attributeName, getter, setter, defaultValue=None): """ """ setattr(obj.__class__, attributeName, property(fget=getter, fset=setter)) setattr(obj, getMeta_attr_name(attributeName), defaultValue)
def build_getter(self, instance, propertyName): """ Method that given an instance and a propertyName, builds a getter dynamically and associate this propertyName to the instance. :param instance: The instance to associate the getter method :type instance: <type 'instance'> :param propertyName: The property that the getter will return on calling :type propertyName: str """ getter = "def get{0}(self):\n" \ " return self.{1}".format(propertyName, getMeta_attr_name(propertyName)) return self.create_function(instance, getter)
def build_setter(self, instance, propertyName, callbacks=None): """ Method that given an instance and a propertyName, builds a setter dynamically and associate this propertyName to the instance. Will append validators if any callback passed :param instance: The instance to associate the getter method :type instance: <type 'instance'> :param propertyName: The property that the getter will return on calling :type propertyName: str """ callback = '' if callbacks is None else '\n'.join('\t'+callback.generate_validator().strip() for callback in callbacks) setter = "def set{0}(self,value):\n" \ "{1}\n" \ "\tself.{2}=value".format(propertyName, callback, getMeta_attr_name(propertyName)) return self.create_function(instance, setter)
def build_setter(self, instance, propertyName, callbacks=None): """ Method that given an instance and a propertyName, builds a setter dynamically and associate this propertyName to the instance. Will append validators if any callback passed :param instance: The instance to associate the getter method :type instance: <type 'instance'> :param propertyName: The property that the getter will return on calling :type propertyName: str """ callback = '' if callbacks is None else '\n'.join( '\t' + callback.generate_validator().strip() for callback in callbacks) setter = "def set{0}(self,value):\n" \ "{1}\n" \ "\tself.{2}=value".format(propertyName, callback, getMeta_attr_name(propertyName)) return self.create_function(instance, setter)
def __init__(self, attribute, validator, validateArg=None): self.validator = validator self.validatorArguments = self.process_validator_arg(validateArg) self.validatorName = validator.__name__ + getMeta_attr_name(attribute)