11# -*- coding: utf-8 -*-
22"""Provides the variant form decorator."""
33
4- import six
5-
64import types
75
86__all__ = ['variants' ]
97
108
11- class _StaticCallableMetaclass (type ):
12- """Metaclass for a static callable function."""
13-
14- def __call__ (cls , * args , ** kwargs ):
15- """Rather than calling the constructor, call a static function."""
16- return cls .__main_form__ (* args , ** kwargs )
17-
18- def __repr__ (cls ):
19- return cls .__func_repr__ ()
20-
21-
229def variants (f ):
2310 """
2411 Decorator to register a function that has variant forms.
@@ -40,26 +27,31 @@ def myfunc(url):
4027 do_something(r.text)
4128 """
4229
43- @six .add_metaclass (_StaticCallableMetaclass )
4430 class VariantFunction :
4531 __doc__ = f .__doc__
4632
47- @staticmethod
48- def __main_form__ (* args , ** kwargs ):
33+ def __call__ (self , * args , ** kwargs ):
4934 return f (* args , ** kwargs )
5035
51- @ classmethod
52- def variant ( cls , func_name ):
36+ def variant ( self , func_name ):
37+ """Decorator to add a new variant form to the function."""
5338 def decorator (vfunc ):
54- setattr (cls , func_name , vfunc )
55- return cls
39+ setattr (self . __class__ , func_name , staticmethod ( vfunc ) )
40+ return self
5641
5742 return decorator
5843
59- @classmethod
60- def __func_repr__ (cls ):
61- return '<VariantFunction {}>' .format (cls .__name__ )
44+ def __get__ (self , instance , owner ):
45+ # This is necessary to bind instance methods
46+ if instance is None :
47+ return self
48+
49+ return types .MethodType (self , instance )
50+
51+ def __repr__ (self ):
52+ return '<VariantFunction {}>' .format (self .__name__ )
6253
63- VariantFunction .__name__ = f .__name__
54+ f_out = VariantFunction ()
55+ f_out .__name__ = f .__name__
6456
65- return VariantFunction
57+ return f_out
0 commit comments