|
| 1 | +"""Custom utilities.""" |
| 2 | + |
| 3 | +import six |
| 4 | + |
| 5 | + |
| 6 | +class DynamicDecorator(object): #pylint: disable=too-few-public-methods |
| 7 | + """ |
| 8 | + Decorator that will inject a decorator during class construction. |
| 9 | +
|
| 10 | + This decorator will intercept the __init__(self, *args **kwargs) call, |
| 11 | + and decorate specified methods by instantiating the supplied decorators, |
| 12 | + with arguments extracted and mapped from the constructor call. |
| 13 | + For example: |
| 14 | +
|
| 15 | + def decorator(pos_arg_1, keyword_arg_1=3): |
| 16 | + pass |
| 17 | +
|
| 18 | + @DynamicDecorator( |
| 19 | + decorator, |
| 20 | + ['method1', 'method2'], |
| 21 | + lambda *p, **_: p[0], |
| 22 | + keyword_arg=lambda *_, **kw: kw.get('arg2') |
| 23 | + ) |
| 24 | + class SomeClass |
| 25 | + def __init__(self, arg1, arg2=3: |
| 26 | + pass |
| 27 | +
|
| 28 | + def method1(self, x): |
| 29 | + pass |
| 30 | +
|
| 31 | + def method2(self, x): |
| 32 | + pass |
| 33 | + """ |
| 34 | + |
| 35 | + def __init__(self, decorator, methods_to_decorate, *pos_arg_map, **kw_arg_map): |
| 36 | + """ |
| 37 | + Construct a decorator with it's mappings. |
| 38 | +
|
| 39 | + :param decorator: Original decorator to apply to specified methods. |
| 40 | + :type decorator: callable |
| 41 | + :param methods_to_decorate: List of methods (strings) where the decorator should be applied |
| 42 | + :type methods_to_decorate: list(string) |
| 43 | + :param pos_arg_map: lambdas to be called with __init__ arguments for the decorator's |
| 44 | + positional arguments. |
| 45 | + :type pos_arg_map: expanded list |
| 46 | + :param kw_arg_map: lambdas to be called with __init__ arguments for the decorator's keyword |
| 47 | + arguments. |
| 48 | + :type kw_arg_map: expanded dict |
| 49 | + """ |
| 50 | + self._decorator = decorator |
| 51 | + self._methods = methods_to_decorate |
| 52 | + self._positional_args_lambdas = pos_arg_map |
| 53 | + self._keyword_args_lambdas = kw_arg_map |
| 54 | + |
| 55 | + def __call__(self, to_decorate): |
| 56 | + """ |
| 57 | + Apply the decorator the specified class. |
| 58 | +
|
| 59 | + :param to_decorate: Class to which the decorator will be applied. |
| 60 | + :type to_decorate: class |
| 61 | +
|
| 62 | + :return: a decorated class, which inherits from `to_decorate` |
| 63 | + :rtype: to_decorate |
| 64 | + """ |
| 65 | + decorator = self._decorator |
| 66 | + methods = self._methods |
| 67 | + positional_args_lambdas = self._positional_args_lambdas |
| 68 | + keyword_args_lambdas = self._keyword_args_lambdas |
| 69 | + |
| 70 | + class _decorated(to_decorate): #pylint: disable=too-few-public-methods |
| 71 | + """ |
| 72 | + Decorated class wrapper. |
| 73 | +
|
| 74 | + This wrapper uses the __init__ to catch required arguments, |
| 75 | + instantiate the decorator with the appropriate parameters and then create a child |
| 76 | + class with decorated behaviour. |
| 77 | + """ |
| 78 | + |
| 79 | + def __init__(self, *args, **kwargs): |
| 80 | + """Decorate class constructor.""" |
| 81 | + # calculate positional and keyword arguments needed to build the decorator. |
| 82 | + positional = [pos_func(*args, **kwargs) for pos_func in positional_args_lambdas] |
| 83 | + keyword = { |
| 84 | + key: func(*args, **kwargs) |
| 85 | + for (key, func) in six.iteritems(keyword_args_lambdas) |
| 86 | + } |
| 87 | + |
| 88 | + # call original class constructor |
| 89 | + to_decorate.__init__(self, *args, **kwargs) |
| 90 | + |
| 91 | + # decorate specified methods |
| 92 | + for method in methods: |
| 93 | + decorated_method = decorator(*positional, **keyword)(getattr(self, method)) |
| 94 | + setattr(to_decorate, method, decorated_method) |
| 95 | + |
| 96 | + return _decorated |
0 commit comments