def __delattr__(self, strAttrName): """ Special magic method hooking the deletion of an attribute on the class instance level. Does not allow deletion of the attributes with the names starting with a single underscore, excepting those starting with more than one undescore. Signature: str -> None Args: strAttrName: str, name of an attribute to delete Raises: libexceptions.PrivateAttributeAccess: denied access to a protected attribute libexceptions.CustomAttributeError: denied delition of an existing attribute libexceptions.NotExistingAttribute: the attribute does not exist Version 1.0.0.0 """ objClass = object.__getattribute__(self, '__class__') bCond1 = strAttrName.startswith('_') bCond2 = not strAttrName.startswith('__') if bCond1 and bCond2: raise PrivateAttributeAccess(strAttrName, objClass, iSkipFrames = 1) if hasattr(self, strAttrName): raise CustomAttributeError(strAttrName, objClass, iSkipFrames = 1) else: raise NotExistingAttribute(strAttrName, objClass, iSkipFrames = 1)
def __setattr__(self, strAttrName, gValue): """ Special magic method hooking the modification of an attribute access on the class instance level. Does not allow modification of the attributes with the names starting with a single underscore, excepting those starting with more than one undescore. Signature: str, type A -> None Args: strAttrName: str, name of an attribute to modify gValue: type A, the value to assign Raises: libexceptions.PrivateAttributeAccess: denied access to a protected attribute libexceptions.CustomAttributeError: denied modification of an existing attribute due to its own desriptor limitation, or deletion of an instance, class or static method Version 1.0.0.0 """ objClass = object.__getattribute__(self, '__class__') bLocked = object.__getattribute__(self, '__dict__').get('_bLocked', False) bCond1 = strAttrName.startswith('_') bCond2 = not strAttrName.startswith('__') if bCond1 and bCond2 and bLocked: raise PrivateAttributeAccess(strAttrName, objClass, iSkipFrames = 1) if strAttrName in self.__dict__.keys(): if hasattr(self.__dict__[strAttrName], '__set__'): self.__dict__[strAttrName].__set__(self, gValue) else: self.__dict__[strAttrName] = gValue elif hasattr(self, strAttrName): for objParent in self.__class__.__mro__: if strAttrName in objParent.__dict__.keys(): gAttrValue = objParent.__dict__[strAttrName] bCond1 = hasattr(gAttrValue, '__set__') bCond2 = isinstance(gAttrValue, (classmethod, staticmethod, property, types.MethodType, types.FunctionType)) if bCond1: try: gAttrValue.__set__(self, gValue) except Exception as err: if isinstance(err, ConstantAttributeAssignment): raise ConstantAttributeAssignment(strAttrName, objClass, iSkipFrames = 1) else: raise CustomAttributeError(strAttrName, objClass, iSkipFrames= 1) elif bCond2: raise CustomAttributeError(strAttrName, objClass, iSkipFrames = 1) else: type.__setattr__(objParent, strAttrName, gValue) else: self.__dict__[strAttrName] = gValue
def __delattr__(objCaller, strAttrName): """ Special magic method hooking the deletion of an attribute on the class level using a class object (without instantiation). Does not allow deletion of any attribute. Signature: class A, str -> None Args: objCaller: class A, the class object, to which this method is applied strAttrName: str, name of an attribute to delete Raises: libexceptions.PrivateAttributeAccess: denied deletion of a protected attribute libexceptions.CustomAttributeError: denied deletion of a public or magic attribute libexceptions.NotExistingAttribute: denied deletion of a non-existing attribute Version 1.0.0.0 """ bCond1 = strAttrName.startswith('_') bCond2 = not strAttrName.startswith('__') bCond3 = not strAttrName.startswith('_abc_') if bCond1 and bCond2 and bCond3: raise PrivateAttributeAccess(strAttrName, objCaller, iSkipFrames= 1) if hasattr(objCaller, strAttrName): raise CustomAttributeError(strAttrName, objCaller, iSkipFrames = 1) else: raise NotExistingAttribute(strAttrName, objCaller, iSkipFrames = 1)
def __setattr__(objCaller, strAttrName, gValue): """ Special magic method hooking the modification of an attribute access on the class level using a class object (without instantiation). Does not allow modification of the attributes with the names starting with a single underscore, excepting those starting with the '_abc_' string or more than one undescore. Signature: class A, str, type A -> None Args: objCaller: class A, the class object, to which this method is applied strAttrName: str, name of an attribute to modify gValue: type A, the value to assign Raises: libexceptions.PrivateAttributeAccess: denied access to a protected attribute libexceptions.CustomAttributeError: an existing attribute cannot be modified, or such argument does not exist libexceptions.NotExistingAttribute: the attribute does not exist Version 1.0.0.0 """ bCond1 = strAttrName.startswith('_') bCond2 = not strAttrName.startswith('__') bCond3 = not strAttrName.startswith('_abc_') if bCond1 and bCond2 and bCond3: raise PrivateAttributeAccess(strAttrName, objCaller, iSkipFrames= 1) bModified = False for objParent in objCaller.__mro__: if strAttrName in objParent.__dict__.keys(): gAttrValue = objParent.__dict__[strAttrName] bCond1 = hasattr(gAttrValue, '__set__') bCond2 = isinstance(gAttrValue, (classmethod, staticmethod, types.FunctionType)) bCond4 = isinstance(gAttrValue, property) if bCond1: try: gAttrValue.__set__(objParent, gValue) except Exception as err: if isinstance(err, ConstantAttributeAssignment): raise ConstantAttributeAssignment(strAttrName, objCaller, iSkipFrames = 1) else: raise CustomAttributeError(strAttrName, objCaller, iSkipFrames= 1) elif (bCond4 and not bCond1) or (bCond2 and not bCond4): raise CustomAttributeError(strAttrName, objCaller, iSkipFrames = 1) else: type.__setattr__(objParent, strAttrName, gValue) bModified = True break if not bModified: raise NotExistingAttribute(strAttrName, objCaller, iSkipFrames = 1)
def __delattr__(objCaller, strAttrName): """ Special magic method hooking the deletion of an attribute on the class level using a class object (without instantiation). Does not allow deletion of the attributes with the names starting with a single underscore, excepting those starting with the '_abc_' string or more than one undescore. Signature: class A, str -> None Args: objCaller: class A, the class object, to which this method is applied strAttrName: str, name of an attribute to delete Raises: libexceptions.PrivateAttributeAccess: denied deletion of a protected attribute libexceptions.CustomAttributeError: denied deletion of a public or magic attribute, e.g. method, property or an instance of a class with __delete__ descriptor preventing deletion libexceptions.NotExistingAttribute: denied deletion of a non-existing attribute Version 1.0.0.0 """ bCond1 = strAttrName.startswith('_') bCond2 = not strAttrName.startswith('__') bCond3 = not strAttrName.startswith('_abc_') if bCond1 and bCond2 and bCond3: raise PrivateAttributeAccess(strAttrName, objCaller, iSkipFrames= 1) if strAttrName in objCaller.__dict__.keys(): gAttrValue = objCaller.__dict__[strAttrName] bCond1 = hasattr(gAttrValue, '__delete__') bCond2 = isinstance(gAttrValue, (classmethod, staticmethod, types.FunctionType, types.MethodType)) bCond4 = isinstance(gAttrValue, property) if bCond1: try: gAttrValue.__delete__(objCaller) except Exception as err: if isinstance(err, ConstantAttributeAssignment): raise ConstantAttributeAssignment(strAttrName, objCaller, iSkipFrames = 1) else: raise CustomAttributeError(strAttrName, objCaller, iSkipFrames= 1) type.__setattr__(objCaller, strAttrName, None) elif (bCond4 and not bCond1) or (bCond2 and not bCond4): raise CustomAttributeError(strAttrName, objCaller, iSkipFrames = 1) type.__delattr__(objCaller, strAttrName) del gAttrValue elif hasattr(objCaller, strAttrName): raise CustomAttributeError(strAttrName, objCaller, iSkipFrames = 1) else: raise NotExistingAttribute(strAttrName, objCaller, iSkipFrames = 1)
def __delattr__(self, strAttrName): """ Special magic method hooking the deletion of an attribute on the class instance level. Does not allow deletion of the attributes with the names starting with a single underscore, excepting those starting with more than one undescore. Signature: str -> None Args: strAttrName: str, name of an attribute to delete Raises: libexceptions.PrivateAttributeAccess: denied access to a protected attribute libexceptions.CustomAttributeError: denied delition of an existing class attribute, or of an existing instance attribute due to its own desriptor limitation libexceptions.NotExistingAttribute: the attribute does not exist Version 1.0.0.0 """ objClass = object.__getattribute__(self, '__class__') bCond1 = strAttrName.startswith('_') bCond2 = not strAttrName.startswith('__') if bCond1 and bCond2: raise PrivateAttributeAccess(strAttrName, objClass, iSkipFrames = 1) if strAttrName in self.__dict__.keys(): gAttrValue = self.__dict__[strAttrName] if hasattr(gAttrValue, '__delete__'): try: gAttrValue.__delete__(self) except Exception as err: if isinstance(err, ConstantAttributeAssignment): raise ConstantAttributeAssignment(strAttrName, objClass, iSkipFrames = 1) else: raise CustomAttributeError(strAttrName, objClass, iSkipFrames= 1) self.__dict__[strAttrName] = None object.__delattr__(self, strAttrName) del gAttrValue elif hasattr(self, strAttrName): raise CustomAttributeError(strAttrName, objClass, iSkipFrames = 1) else: raise NotExistingAttribute(strAttrName, objClass, iSkipFrames = 1)
def __getattribute__(self, strAttrName): """ Special magic method hooking the read access to an attribute on the class instance level. Does not allow access to the attributes with the names starting with a single underscore, excepting those starting with more than one undescore. Signature: str -> type A Args: strAttrName: str, name of an attribute to access Returns: type A: the value of the attribute Raises: libexceptions.PrivateAttributeAccess: denied access to a protected attribute libexceptions.CustomAttributeError: denied read access to an existing attribute, i.e. by its own desriptor libexceptions.NotExistingAttribute: the attribute does not exist Version 1.0.0.0 """ objClass = object.__getattribute__(self, '__class__') bLocked = object.__getattribute__(self, '__dict__').get('_bLocked', False) bCond1 = strAttrName.startswith('_') bCond2 = not strAttrName.startswith('__') if bCond1 and bCond2 and bLocked: raise PrivateAttributeAccess(strAttrName, objClass, iSkipFrames = 1) try: gResult = object.__getattribute__(self, strAttrName) except AttributeError: raise NotExistingAttribute(strAttrName, objClass, iSkipFrames = 1) #check if a descriptor but not a method if (hasattr(gResult, '__get__') and (not isinstance(gResult, types.FunctionType))): try: gResult = gResult.__get__(self, objClass) except: raise CustomAttributeError(strAttrName, objClass, iSkipFrames = 1) return gResult
def __getattribute__(objCaller, strAttrName): """ Special magic method hooking the read access to an attribute on the class level using a class object (without instantiation). Does not allow access to the attributes with the names starting with a single underscore, excepting those starting with the '_abc_' string or more than one undescore. Signature: class A, str -> type A Args: objCaller: class A, the class object, to which this method is applied strAttrName: str, name of an attribute to access Returns: type A: the value of the attribute Raises: libexceptions.PrivateAttributeAccess: denied access to a protected attribute libexceptions.NotExistingAttribute: non-existing attribute is accessed Version 1.0.0.0 """ bCond1 = strAttrName.startswith('_') bCond2 = not strAttrName.startswith('__') bCond3 = not strAttrName.startswith('_abc_') if bCond1 and bCond2 and bCond3: raise PrivateAttributeAccess(strAttrName, objCaller, iSkipFrames= 1) try: gResult = type.__getattribute__(objCaller, strAttrName) except AttributeError: raise NotExistingAttribute(strAttrName, objCaller, iSkipFrames= 1) return gResult