[Subversion] / ObjectRoles / peak / util / roles.py  

View of /ObjectRoles/peak/util/roles.py

Parent Directory | Revision Log
Revision: 2343 - (download) (as text)
Wed Jul 11 19:30:49 2007 UTC (16 years, 8 months ago) by pje
File size: 1251 byte(s)
Created ObjectRoles package to replace the 'Aspect' class from PEAK-Rules.
from peak.util.decorators import decorate

def aspects_for(ob):
    #try:
    return ob.__dict__
    #except (AttributeError, TypeError):
    #           

class Role(object):
    """Attach extra state to an object"""

    __slots__ = ()

    class __metaclass__(type):
        def __call__(cls, ob, *key):
            a = aspects_for(ob)
            try:
                return a[cls, key]
            except KeyError:
                # Use setdefault() to prevent race conditions
                ob = a.setdefault((cls, key), type.__call__(cls, ob, *key))
                return ob

    decorate(classmethod)
    def exists_for(cls, ob, *key):
        """Does an aspect of this type for the given key exist?""" 
        return (cls, key) in aspects_for(ob)

    decorate(classmethod)
    def delete_from(cls, ob, *key):
        """Ensure an aspect of this type for the given key does not exist"""
        a = aspects_for(ob)
        try:
            del a[cls, key]
        except KeyError:
            pass

    def __init__(self, owner):
        pass




def additional_tests():
    import doctest
    return doctest.DocFileSuite(
        'README.txt', package='__main__',
        optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE,
    )


cvs-admin@eby-sarna.com

Powered by ViewCVS 1.0-dev

ViewCVS and CVS Help