Esempio n. 1
0
 def __contains__(self, ref):
     '''
     Checks if the object contains a value for the property even if that value is None.
     
     @param ref: TypeCriteriaEntry|TypeProperty reference
         The type property to check.
     @return: boolean
         True if a value for the reference is present, false otherwise.
     '''
     typ = typeFor(ref)
     if isinstance(typ, TypeCriteriaEntry):
         assert isinstance(typ, TypeCriteriaEntry)
         if typ.parent.isValid(self):
             descriptor, _clazz = getAttrAndClass(self.__class__, typ.name)
             if isinstance(descriptor, IContained):
                 assert isinstance(descriptor, IContained)
                 return descriptor.__contained__(self)
     elif isinstance(typ, TypeProperty):
         # We do not need to make any recursive checking here since the criteria will only contain primitive properties
         # so there will not be the case of AQuery.ACriteria.AModel.AProperty the maximum is AQuery.ACriteria.AProperty
         try: typCrt = typeFor(ref._ally_ref_parent)
         except AttributeError: pass
         else:
             if isinstance(typCrt, TypeCriteriaEntry):
                 if typCrt.parent.isValid(self):
                     descriptor, _clazz = getAttrAndClass(self.__class__, typCrt.name)
                     if isinstance(descriptor, IContained) and isinstance(descriptor, IGet):
                         assert isinstance(descriptor, IContained)
                         assert isinstance(descriptor, IGet)
                         if descriptor.__contained__(self): return typ in descriptor.__get__(self)
     return False
Esempio n. 2
0
    def __init__(self, name, bases, namespace):
        assert isinstance(namespace, dict), 'Invalid namespace %s' % namespace

        mapped, models = [], []
        for cls in bases:
            typ = typeFor(cls)
            if isinstance(typ, TypeModelMapped): mapped.append(cls)
            elif isinstance(typ, TypeModel): models.append(cls)

        if not mapped and not models:
            assert log.debug(
                'Cannot find any API model class for \'%s\', no merging required',
                name) or True
            DeclarativeMeta.__init__(self, name, bases, namespace)
            return

        if len(mapped) > 1:
            raise MappingError(
                'Cannot inherit more then one mapped class, got %s' % models)

        if len(models) > 1:
            raise MappingError(
                'Cannot merge with more then one API model class, got %s' %
                models)

        if models: typeModel = typeFor(models[0])
        else: typeModel = None
        if mapped:
            if typeModel is None: typeModel = typeFor(mapped[0]).base
        assert isinstance(typeModel, TypeModel)
        typeModel = TypeModelMapped(self, typeModel)

        self._ally_reference = {
            prop: Reference(TypeModelProperty(typeModel, prop, propType))
            for prop, propType in typeModel.container.properties.items()
        }
        self._ally_listeners = {}  # Provides the BindableSupport
        self._ally_type = typeModel  # Provides the TypeSupport

        DeclarativeMeta.__init__(self, name, bases, namespace)

        # TODO: see if required: self.__clause_element__ = lambda: self.__table__

        for prop in typeModel.container.properties:
            if typeFor(getattr(self, prop)) != typeFor(
                    self._ally_reference[prop]):
                value, _class = getAttrAndClass(self, prop)
                setattr(self, prop, value)

        try:
            mappings = self.metadata._ally_mappers
        except AttributeError:
            mappings = self.metadata._ally_mappers = deque()
        mappings.append(self)
Esempio n. 3
0
    def __init__(self, name, bases, namespace):
        assert isinstance(namespace, dict), "Invalid namespace %s" % namespace

        mapped, models = [], []
        for cls in bases:
            typ = typeFor(cls)
            if isinstance(typ, TypeModelMapped):
                mapped.append(cls)
            elif isinstance(typ, TypeModel):
                models.append(cls)

        if not mapped and not models:
            assert log.debug("Cannot find any API model class for '%s', no merging required", name) or True
            DeclarativeMeta.__init__(self, name, bases, namespace)
            return

        if len(mapped) > 1:
            raise MappingError("Cannot inherit more then one mapped class, got %s" % models)

        if len(models) > 1:
            raise MappingError("Cannot merge with more then one API model class, got %s" % models)

        if models:
            typeModel = typeFor(models[0])
        else:
            typeModel = None
        if mapped:
            if typeModel is None:
                typeModel = typeFor(mapped[0]).base
        assert isinstance(typeModel, TypeModel)
        typeModel = TypeModelMapped(self, typeModel)

        self._ally_reference = {
            prop: Reference(TypeModelProperty(typeModel, prop, propType))
            for prop, propType in typeModel.container.properties.items()
        }
        self._ally_listeners = {}  # Provides the BindableSupport
        self._ally_type = typeModel  # Provides the TypeSupport

        DeclarativeMeta.__init__(self, name, bases, namespace)

        # TODO: see if required: self.__clause_element__ = lambda: self.__table__

        for prop in typeModel.container.properties:
            if typeFor(getattr(self, prop)) != typeFor(self._ally_reference[prop]):
                value, _class = getAttrAndClass(self, prop)
                setattr(self, prop, value)

        try:
            mappings = self.metadata._ally_mappers
        except AttributeError:
            mappings = self.metadata._ally_mappers = deque()
        mappings.append(self)
Esempio n. 4
0
    def validations(self, mapped):
        '''
        Provides the mapped class validations that can be performed based on the SQL alchemy mapping.
        
        @param mapped: class
            The mapped model class.
        @return: list[Validation, DeclarativeMetaModel]
            The list of validations obtained.
        '''
        assert isclass(mapped), 'Invalid class %s' % mapped
        assert isinstance(mapped.metadata, MetaData), 'Invalid mapped class %s' % mapped
        mapper, model = mappingFor(mapped), typeFor(mapped)
        assert isinstance(mapper, Mapper), 'Invalid mapped class %s' % mapped
        assert isinstance(model, TypeModel), 'Invalid model class %s' % mapped

        validations = []
        mvalidations = validationsFor(mapped)
        if mvalidations:
            for validation, target in mvalidations:
                assert isinstance(validation, IValidation), 'Invalid created validation %s' % validation
                validations.append((validation, target))
        
        for name, prop in model.properties.items():
            descriptor, dclazz = getAttrAndClass(mapped, name)
            dvalidations = validationsFor(descriptor)
            if dvalidations:
                for creator, target in dvalidations:
                    validation = creator(prop)
                    assert isinstance(validation, IValidation), 'Invalid created validation %s' % validation
                    if target == descriptor: target = dclazz
                    validations.append((validation, target))
                continue
            
            if isinstance(descriptor, hybrid_property):
                assert isinstance(descriptor, hybrid_property)
                if descriptor.fset is None: validations.append((ReadOnly(prop), dclazz))
                continue
            
            column = getattr(mapper.c, name, None)
            if column is None or not isinstance(column, Column): continue
    
            if column.primary_key:
                if column.autoincrement: validations.append((AutoId(prop), mapped))
                else: validations.append((Mandatory(prop), mapped))
            elif not column.nullable: validations.append((Mandatory(prop), mapped))
    
            if isinstance(column.type, String) and column.type.length:
                validations.append((MaxLen(prop, column.type.length), mapped))
            
            if isinstance(prop, TypePropertyContainer): validations.append((Relation(prop), mapped))
            
        return validations        
Esempio n. 5
0
 def __contains__(self, ref):
     '''
     Checks if the object contains a value for the property even if that value is None.
     
     @param ref: TypeCriteriaEntry|TypeProperty reference
         The type property to check.
     @return: boolean
         True if a value for the reference is present, false otherwise.
     '''
     typ = typeFor(ref)
     if isinstance(typ, TypeCriteriaEntry):
         assert isinstance(typ, TypeCriteriaEntry)
         if typ.parent.isValid(self):
             descriptor, _clazz = getAttrAndClass(self.__class__, typ.name)
             if isinstance(descriptor, IContained):
                 assert isinstance(descriptor, IContained)
                 return descriptor.__contained__(self)
     elif isinstance(typ, TypeProperty):
         # We do not need to make any recursive checking here since the criteria will only contain primitive properties
         # so there will not be the case of AQuery.ACriteria.AModel.AProperty the maximum is AQuery.ACriteria.AProperty
         try:
             typCrt = typeFor(ref._ally_ref_parent)
         except AttributeError:
             pass
         else:
             if isinstance(typCrt, TypeCriteriaEntry):
                 if typCrt.parent.isValid(self):
                     descriptor, _clazz = getAttrAndClass(
                         self.__class__, typCrt.name)
                     if isinstance(descriptor, IContained) and isinstance(
                             descriptor, IGet):
                         assert isinstance(descriptor, IContained)
                         assert isinstance(descriptor, IGet)
                         if descriptor.__contained__(self):
                             return typ in descriptor.__get__(self)
     return False
Esempio n. 6
0
    def __init__(self, name, clazz):
        '''
        Constructs the info function based on the provided function.
        
        @param name: string
            The class function name.
        @param clazz: class
            The class of the function.
        '''
        assert isinstance(name, str), 'Invalid function name %s' % name
        assert isclass(clazz), 'Invalid class %s' % clazz

        method, clazzDefiner = getAttrAndClass(clazz, name)
        super().__init__(name, method.__code__.co_filename, method.__code__.co_firstlineno, getdoc(method))

        self.clazz = clazz
        self.clazzDefiner = clazzDefiner
Esempio n. 7
0
 def __contains__(self, ref):
     '''
     Checks if the object contains a value for the property even if that value is None.
     
     @param ref: TypeProperty reference
         The type property to check.
     @return: boolean
         True if a value for the reference is present, false otherwise.
     '''
     typ = typeFor(ref)
     if not isinstance(typ, TypeProperty): return False
     assert isinstance(typ, TypeProperty)
     if typ.parent.isValid(self):
         descriptor, _clazz = getAttrAndClass(self.__class__, typ.property)
         if isinstance(descriptor, IContained):
             assert isinstance(descriptor, IContained)
             return descriptor.__contained__(self)
     return False
Esempio n. 8
0
 def __contains__(self, ref):
     '''
     Checks if the object contains a value for the property even if that value is None.
     
     @param ref: TypeProperty reference
         The type property to check.
     @return: boolean
         True if a value for the reference is present, false otherwise.
     '''
     typ = typeFor(ref)
     if not isinstance(typ, TypeProperty): return False
     assert isinstance(typ, TypeProperty)
     if typ.parent.isValid(self):
         descriptor, _clazz = getAttrAndClass(self.__class__, typ.property)
         if isinstance(descriptor, IContained):
             assert isinstance(descriptor, IContained)
             return descriptor.__contained__(self)
     return False
Esempio n. 9
0
    def __init__(self, name, clazz):
        '''
        Constructs the info function based on the provided function.
        
        @param name: string
            The class function name.
        @param clazz: class
            The class of the function.
        '''
        assert isinstance(name, str), 'Invalid function name %s' % name
        assert isclass(clazz), 'Invalid class %s' % clazz

        method, clazzDefiner = getAttrAndClass(clazz, name)
        super().__init__(name, method.__code__.co_filename,
                         method.__code__.co_firstlineno, getdoc(method))

        self.clazz = clazz
        self.clazzDefiner = clazzDefiner
Esempio n. 10
0
 def __contains__(self, ref):
     '''
     Checks if the object contains a value for the property even if that value is None.
     
     @param ref: TypeProperty reference
         The type property to check.
     @return: boolean
         True if a value for the reference is present, false otherwise.
     '''
     prop = typeFor(ref)
     if not isinstance(prop, TypeProperty): return False
     assert isinstance(prop, TypeProperty)
     assert isinstance(prop.parent, TypeContainer), 'Invalid parent for %s' % prop
     if prop.parent.isValid(self):
         descriptor, _clazz = getAttrAndClass(self.__class__, prop.name)
         if not isinstance(descriptor, IContained): return True
         # In case the descriptor has no contained method we consider that the property should always be present.
         assert isinstance(descriptor, IContained)
         return descriptor.__contained__(self)
     return False
Esempio n. 11
0
 def __contains__(self, ref):
     '''
     Checks if the object contains a value for the property even if that value is None.
     
     @param ref: TypeProperty reference
         The type property to check.
     @return: boolean
         True if a value for the reference is present, false otherwise.
     '''
     prop = typeFor(ref)
     if not isinstance(prop, TypeProperty): return False
     assert isinstance(prop, TypeProperty)
     assert isinstance(prop.parent,
                       TypeContainer), 'Invalid parent for %s' % prop
     if prop.parent.isValid(self):
         descriptor, _clazz = getAttrAndClass(self.__class__, prop.name)
         if not isinstance(descriptor, IContained): return True
         # In case the descriptor has no contained method we consider that the property should always be present.
         assert isinstance(descriptor, IContained)
         return descriptor.__contained__(self)
     return False
Esempio n. 12
0
    def validations(self, mapped):
        '''
        Provides the mapped class validations that can be performed based on the SQL alchemy mapping.
        
        @param mapped: class
            The mapped model class.
        @return: list[Validation, DeclarativeMetaModel]
            The list of validations obtained.
        '''
        assert isclass(mapped), 'Invalid class %s' % mapped
        assert isinstance(mapped.metadata,
                          MetaData), 'Invalid mapped class %s' % mapped
        mapper, model = mappingFor(mapped), typeFor(mapped)
        assert isinstance(mapper, Mapper), 'Invalid mapped class %s' % mapped
        assert isinstance(model, TypeModel), 'Invalid model class %s' % mapped

        validations = []
        mvalidations = validationsFor(mapped)
        if mvalidations:
            for validation, target in mvalidations:
                assert isinstance(
                    validation,
                    IValidation), 'Invalid created validation %s' % validation
                validations.append((validation, target))

        for name, prop in model.properties.items():
            descriptor, dclazz = getAttrAndClass(mapped, name)
            dvalidations = validationsFor(descriptor)
            if dvalidations:
                for creator, target in dvalidations:
                    validation = creator(prop)
                    assert isinstance(
                        validation, IValidation
                    ), 'Invalid created validation %s' % validation
                    if target == descriptor: target = dclazz
                    validations.append((validation, target))
                continue

            if isinstance(descriptor, hybrid_property):
                assert isinstance(descriptor, hybrid_property)
                if descriptor.fset is None:
                    validations.append((ReadOnly(prop), dclazz))
                continue

            column = getattr(mapper.c, name, None)
            if column is None or not isinstance(column, Column): continue

            if column.primary_key:
                if column.autoincrement:
                    validations.append((AutoId(prop), mapped))
                else:
                    validations.append((Mandatory(prop), mapped))
            elif not column.nullable:
                validations.append((Mandatory(prop), mapped))

            if isinstance(column.type, String) and column.type.length:
                validations.append((MaxLen(prop, column.type.length), mapped))

            if isinstance(prop, TypePropertyContainer):
                validations.append((Relation(prop), mapped))

        return validations