示例#1
0
    def __getattribute__(self, name):
        """Get an attribute value

        See the very important note in the comment below!
        """
        #################################################################
        # IMPORTANT! READ THIS! 8->
        #
        # We *always* give Persistent a chance first.
        # Persistent handles certain special attributes, like _p_
        # attributes. In particular, the base class handles __dict__
        # and __class__.
        #
        # We call _p_getattr. If it returns True, then we have to
        # use Persistent.__getattribute__ to get the value.
        #
        #################################################################
        if Persistent._p_getattr(self, name):
            return Persistent.__getattribute__(self, name)

        # Data should be in our secret dictionary:
        secret = self.__dict__['__secret__']
        if name in secret:
            return secret[name]

        # Maybe it's a method:
        meth = getattr(self.__class__, name, None)
        if meth is None:
            raise AttributeError(name)

        return meth.__get__(self, self.__class__)
    def __getattribute__(self, name):
        """Get an attribute value

        See the very important note in the comment below!
        """
        #################################################################
        # IMPORTANT! READ THIS! 8->
        #
        # We *always* give Persistent a chance first.
        # Persistent handles certain special attributes, like _p_
        # attributes. In particular, the base class handles __dict__
        # and __class__.
        #
        # We call _p_getattr. If it returns True, then we have to
        # use Persistent.__getattribute__ to get the value.
        #
        #################################################################
        if Persistent._p_getattr(self, name):
            return Persistent.__getattribute__(self, name)

        # Data should be in our secret dictionary:
        secret = self.__dict__['__secret__']
        if name in secret:
            return secret[name]

        # Maybe it's a method:
        meth = getattr(self.__class__, name, None)
        if meth is None:
            raise AttributeError(name)

        return meth.__get__(self, self.__class__)
示例#3
0
 def __getattr__(self, key):
     """Maps values to attributes.
     Only called if there *isn't* an attribute with this name
     """
     if Persistent._p_getattr(self, key):
         return Persistent.__getattribute__(self, key) 
     try:
         return self.__getitem__(key)
     except KeyError:
         raise AttributeError(key)
示例#4
0
 def __getattr__(self, key):
     """Maps values to attributes.
     Only called if there *isn't* an attribute with this name
     """
     if Persistent._p_getattr(self, key):
         return Persistent.__getattribute__(self, key)
     try:
         return self.__getitem__(key)
     except KeyError:
         raise AttributeError(key)
示例#5
0
 def __getattribute__(self, name):
     """ see http://svn.zope.org/ZODB/trunk/src/persistent/tests/test_overriding_attrs.py?rev=38214&view=markup """
     #allow persistent to handle any special attributes it needs
     if Persistent._p_getattr(self, name):
         return Persistent.__getattribute__(self, name)
     #otherwise, we use this hook point to see if the user is allowed to perform the action:
     if name == 'privileges':
         return super(Auth, self).__getattribute__(name)
     elif name == 'has_privileges_on':
         return super(Auth, self).__getattribute__(name)
     elif name == '_v_current_user':
         return super(Auth, self).__getattribute__(name)
     elif Auth._v_current_user is None:#this is the case at startup, or if config.USE_AUTH is False
         return super(Auth, self).__getattribute__(name)
     elif Auth._v_current_user.has_privileges_on(self.__class__.__name__, name) is True:
         return super(Auth, self).__getattribute__(name)
     raise PermissionError(Auth._v_current_user, ' does not have permission to access ' , name)
示例#6
0
    def __getattribute__(self, name):
        """Get an attribute value

        The __getattribute__ method is called for all attribute
        accesses.  It overrides the attribute access support inherited
        from Persistent.

        Our sample class let's us provide initial values as keyword
        arguments to the constructor:

        >>> o = SampleOverridingGetattributeSetattrAndDelattr(x=1)
        >>> o._p_changed
        0
        >>> o._p_oid
        >>> o._p_jar
        >>> o.x
        1
        >>> o.y
        Traceback (most recent call last):
        ...
        AttributeError: y

        Next, we'll save the object in a database so that we can
        deactivate it:

        >>> db = DB()
        >>> conn = db.open()
        >>> conn.root()['o'] = o
        >>> transaction.commit()
        >>> o._p_deactivate()
        >>> o._p_changed

        And we'll get some data:

        >>> o.x
        1

        which activates the object:

        >>> o._p_changed
        0

        It works for missing attribes too:

        >>> o._p_deactivate()
        >>> o._p_changed

        >>> o.y
        Traceback (most recent call last):
        ...
        AttributeError: y

        >>> o._p_changed
        0

        See the very important note in the comment below!

        We always close databases after we use them:

        >>> db.close()
        """

        #################################################################
        # IMPORTANT! READ THIS! 8->
        #
        # We *always* give Persistent a chance first.
        # Persistent handles certain special attributes, like _p_
        # attributes. In particular, the base class handles __dict__
        # and __class__.
        #
        # We call _p_getattr. If it returns True, then we have to
        # use Persistent.__getattribute__ to get the value.
        #
        #################################################################
        if Persistent._p_getattr(self, name):
            return Persistent.__getattribute__(self, name)

        # Data should be in our secret dictionary:
        secret = self.__dict__['__secret__']
        if name in secret:
            return secret[name]

        # Maybe it's a method:
        meth = getattr(self.__class__, name, None)
        if meth is None:
            raise AttributeError(name)

        return meth.__get__(self, self.__class__)
    def __getattribute__(self, name):
        """Get an attribute value

        The __getattribute__ method is called for all attribute
        accesses.  It overrides the attribute access support inherited
        from Persistent.

        Our sample class let's us provide initial values as keyword
        arguments to the constructor:

        >>> o = SampleOverridingGetattributeSetattrAndDelattr(x=1)
        >>> o._p_changed
        0
        >>> o._p_oid
        >>> o._p_jar
        >>> o.x
        1
        >>> o.y
        Traceback (most recent call last):
        ...
        AttributeError: y

        Next, we'll save the object in a database so that we can
        deactivate it:

        >>> jar = _rememberingJar()
        >>> jar.add(o)
        >>> o._p_deactivate()
        >>> o._p_changed

        And we'll get some data:

        >>> o.x
        1

        which activates the object:

        >>> o._p_changed
        0

        It works for missing attribes too:

        >>> o._p_deactivate()
        >>> o._p_changed

        >>> o.y
        Traceback (most recent call last):
        ...
        AttributeError: y

        >>> o._p_changed
        0

        See the very important note in the comment below!
        """

        #################################################################
        # IMPORTANT! READ THIS! 8->
        #
        # We *always* give Persistent a chance first.
        # Persistent handles certain special attributes, like _p_
        # attributes. In particular, the base class handles __dict__
        # and __class__.
        #
        # We call _p_getattr. If it returns True, then we have to
        # use Persistent.__getattribute__ to get the value.
        #
        #################################################################
        if Persistent._p_getattr(self, name):
            return Persistent.__getattribute__(self, name)

        # Data should be in our secret dictionary:
        secret = self.__dict__['__secret__']
        if name in secret:
            return secret[name]

        # Maybe it's a method:
        meth = getattr(self.__class__, name, None)
        if meth is None:
            raise AttributeError(name)

        return meth.__get__(self, self.__class__)