예제 #1
0
    def __init__(self, origin):
        self.origin = origin
        self.dct = dict(
            __metachao_aspects__=getattr(origin, '__metachao_aspects__', [])[:]
        )
        if utils.isclass(origin):
            # Aspect application does not change the name. This can
            # lead to messages like "... expects a.A not a.A".
            self.name = origin.__name__
            self.baseclasses = (origin,)
            self.type = type(origin)

            # Aspect application does not change the module. If that
            # is not what you want, consider subclassing first.
            # XXX: it totally should indicate that sth is different
            #self.dct['__module__'] = origin.__module__
            self.dct['__doc__'] = origin.__doc__
        else:
            # we are pretty much creating an object that uses origin
            # as prototype.
            self.name = "Prototyper:%s" % (origin.__class__.__name__,)
            self.baseclasses = ()
            self.type = type

            # bound methods found on origin, except if blacklisted
            blacklist = (
                '__class__', '__delattr__', '__doc__', '__format__',
                '__getattribute__', '__hash__',
                '__init__', '__metachao_origin__',
                '__metachao_prototype__', '__new__', '__reduce__',
                '__reduce_ex__', '__repr__', '__setattr__',
                '__sizeof__', '__str__', '__subclasshook__',
            )
            self.dct.update(((k, getattr(origin, k))
                             for k, v in getmembers(origin)
                             if callable(v) and not k in blacklist))

            # properties bound to origin for all properties found on
            # origin's class
            self.dct.update(((k, prototype_property(origin, v))
                             for k, v in getmembers(origin.__class__)
                             if type(v) is property))

            # getattr fallback to origin, setattr and delattr on new
            self.dct['__getattr__'] = lambda _, name: getattr(origin, name)

            # empty __init__ needed if a later aspect plumbs it
            self.dct['__init__'] = lambda *a, **kw: None
            self.dct['__metachao_prototype__'] = origin

            self.dct['__doc__'] = origin.__doc__

        if '__metachao_effective__' in getattr(origin, '__dict__', ()):
            self.dct['__metachao_effective__'] = \
                origin.__metachao_effective__.copy()
예제 #2
0
파일: _aspect.py 프로젝트: fbauer/metachao
    def __init__(self, origin, **kw):
        self.origin = origin
        self.kw = kw
        self.dct = dict()
        if isclass(origin):
            self.name = origin.__name__
            blacklist = DICT_KEYS_OF_PLAIN_CLASS + [
                '__metachao_origin__',
                ]
            self.dct.update(((k, v)
                             for k, v in origin.__dict__.iteritems()
                             if k not in blacklist))
            # XXX: fix naming (also see self.baseclasses)
            self.bases = Bases(origin)
            self.baseclasses = origin.__bases__
            self.type = type(origin)
            self.dct['__metachao_origin__'] = origin
        else:
            # we are pretty much creating an object that uses origin
            # as prototype.
            self.name = "Prototyper"
            self.baseclasses = ()
            self.type = type

            # bound methods found on origin, except if blacklisted
            blacklist = (
                '__class__', '__delattr__', '__doc__', '__format__',
                '__getattr__', '__getattribute__', '__hash__',
                '__init__', '__metachao_origin__',
                '__metachao_prototype__', '__new__', '__reduce__',
                '__reduce_ex__', '__repr__', '__setattr__',
                '__sizeof__', '__str__', '__subclasshook__',
                )
            self.dct.update(((k, getattr(origin, k))
                             for k, v in getmembers(origin)
                             if callable(v) and not k in blacklist))

            # properties bound to origin for all properties found on
            # origin's class
            self.dct.update(((k, prototype_property(origin, v))
                             for k, v in getmembers(origin.__class__)
                             if type(v) is property))

            # getattr fallback to origin, setattr and delattr on new
            self.dct['__getattr__'] = lambda _, name: getattr(origin, name)

            # empty __init__ needed if a later aspects plumbs it
            self.dct['__init__'] = lambda *a, **kw : None
            self.dct['__metachao_prototype__'] = origin