-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_decorator.py
executable file
·126 lines (103 loc) · 3.67 KB
/
class_decorator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
# Copyright 2017 Alexey Stepanov aka penguinolog
##
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Base class for decorators."""
from __future__ import absolute_import
from __future__ import print_function
import abc
import functools
import sys
import typing
PY34 = sys.version_info[:2] > (3, 3)
class BaseDecorator(typing.Callable):
"""Base class for decorators.
Implements wrapping and __call__, wrapper getter is abstract.
Note:
wrapper getter is called only on function call,
if decorator used without braces.
Usage example:
>>> class TestDecorator(BaseDecorator):
... def _get_function_wrapper(self, func):
... print('Wrapping: {}'.format(func.__name__))
... @functools.wraps(func)
... def wrapper(*args, **kwargs):
... print('call_function: {}'.format(func.__name__))
... return func(*args, **kwargs)
... return wrapper
>>> @TestDecorator
... def func_no_init():
... pass
>>> func_no_init()
Wrapping: func_no_init
call_function: func_no_init
>>> isinstance(func_no_init, TestDecorator)
True
>>> func_no_init._func is func_no_init.__wrapped__
True
>>> @TestDecorator()
... def func_init():
... pass
Wrapping: func_init
>>> func_init()
call_function: func_init
>>> isinstance(func_init, TestDecorator)
False
"""
def __init__(self, func=None):
"""Decorator.
:param func: function to wrap
:type func: typing.Optional[typing.Callable]
"""
# pylint: disable=assigning-non-slot
self.__func = func
if self.__func is not None:
functools.update_wrapper(self, self.__func)
if not PY34: # pragma: no cover
self.__wrapped__ = self.__func
# pylint: enable=assigning-non-slot
# noinspection PyArgumentList
super(BaseDecorator, self).__init__()
@property
def _func(self):
"""Get wrapped function.
:rtype: typing.Optional[typing.Callable]
"""
return self.__func # pragma: no cover
@abc.abstractmethod
def _get_function_wrapper(self, func):
"""Here should be constructed and returned real decorator.
:param func: Wrapped function
:type func: typing.Callable
:rtype: typing.Callable
"""
raise NotImplementedError() # pragma: no cover
def __call__(self, *args, **kwargs):
"""Main decorator getter."""
args = list(args)
wrapped = self.__func or args.pop(0)
wrapper = self._get_function_wrapper(wrapped)
if self.__func:
return wrapper(*args, **kwargs)
return wrapper
def __repr__(self):
"""For debug purposes."""
return "<{cls}({func!r}) at 0x{id:X}>".format(
cls=self.__class__.__name__,
func=self.__func,
id=id(self)
) # pragma: no cover
# 8<----------------------------------------------------------------------------
if __name__ == '__main__':
import doctest # pragma: no cover
doctest.testmod(verbose=2) # pragma: no cover