class MethodDecorator(decorator): """Descriptor class callable with a function or a descriptor object as argument. It defines a default printing representation on method decorators objects and a default ``get`` method. All the rest is provided by the metaclass ``MetaDecorator``. """ __klass__ = type('?', (), {}) # dummy definition class, to be overridden def __init__(self, objfunc): "objfunc is a decorator object or a function" assert isinstance(objfunc, (FunctionType, decorator)) super(MethodDecorator, self).__init__(objfunc) self.__func__ = getattr(objfunc, '__func__', objfunc) def get(self, obj, cls=None): "aliased to __get__ by the metaclass, to be overridden" return self.__func__.__get__(obj, cls) def __str__(self): "Printing representation of kind <decoratorclass:functionname>" return '<%s:%s>' % (self.__class__.__name__, self.__func__.__name__) def _call_(dec, obj): "Returns a method decorator object." return type.__call__(dec, obj) # calls __new__ and __init__ _call_ = classmethod(_call_)
def compose(dclasses,*defaultclasses): """Retrieves or creates a decorator for a tuple of decorators. If defaults decorators are given, they get the precedence. It removes redundant classes.""" dclasses=remove_redundant(defaultclasses+dclasses) decname=''.join([d.__name__ for d in dclasses]) dec=MetaDecorator.dic.get(decname) if not dec: dec=type(decname,dclasses,{}) return dec
def compose(dclasses, *defaultclasses): """Retrieves or creates a decorator for a tuple of decorators. If defaults decorators are given, they get the precedence. It removes redundant classes.""" dclasses = remove_redundant(defaultclasses + dclasses) decname = ''.join([d.__name__ for d in dclasses]) dec = MetaDecorator.dic.get(decname) if not dec: dec = type(decname, dclasses, {}) return dec
def get(self,obj,cls=None): if cls is None: cls=type(obj) return super(classmethod,self).get(cls,cls)
def get(self, obj, cls=None): if cls is None: cls = type(obj) return super(classmethod, self).get(cls, cls)