Skip to content

Commit 1a9a111

Browse files
author
Sangho Na
authored
refactor: Drop dependency on decorator package (ietf-tools#7199)
1 parent 21f467f commit 1a9a111

1 file changed

Lines changed: 42 additions & 37 deletions

File tree

ietf/utils/decorators.py

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import datetime
66

7-
from decorator import decorator, decorate
87
from functools import wraps
98

109
from django.conf import settings
@@ -20,25 +19,29 @@
2019
from ietf.person.models import Person, PersonalApiKey, PersonApiKeyEvent
2120
from ietf.utils import log
2221

23-
@decorator
24-
def skip_coverage(f, *args, **kwargs):
25-
if settings.TEST_CODE_COVERAGE_CHECKER:
26-
set_coverage_checking(False)
27-
result = f(*args, **kwargs)
28-
set_coverage_checking(True)
29-
return result
30-
else:
31-
return f(*args, **kwargs)
32-
33-
@decorator
34-
def person_required(f, request, *args, **kwargs):
35-
if not request.user.is_authenticated:
36-
raise ValueError("The @person_required decorator should be called after @login_required.")
37-
try:
38-
request.user.person
39-
except Person.DoesNotExist:
40-
return render(request, 'registration/missing_person.html')
41-
return f(request, *args, **kwargs)
22+
def skip_coverage(f):
23+
@wraps(f)
24+
def _wrapper(*args, **kwargs):
25+
if settings.TEST_CODE_COVERAGE_CHECKER:
26+
set_coverage_checking(False)
27+
result = f(*args, **kwargs)
28+
set_coverage_checking(True)
29+
return result
30+
else:
31+
return f(*args, **kwargs)
32+
return _wrapper
33+
34+
def person_required(f):
35+
@wraps(f)
36+
def _wrapper(request, *args, **kwargs):
37+
if not request.user.is_authenticated:
38+
raise ValueError("The @person_required decorator should be called after @login_required.")
39+
try:
40+
request.user.person
41+
except Person.DoesNotExist:
42+
return render(request, 'registration/missing_person.html')
43+
return f(request, *args, **kwargs)
44+
return _wrapper
4245

4346

4447
def require_api_key(f):
@@ -90,29 +93,31 @@ def err(code, text):
9093
return _wrapper
9194

9295

93-
def _memoize(func, self, *args, **kwargs):
94-
'''Memoize wrapper for instance methods. Use @lru_cache for functions.'''
95-
if kwargs: # frozenset is used to ensure hashability
96-
key = args, frozenset(list(kwargs.items()))
97-
else:
98-
key = args
99-
# instance method, set up cache if needed
100-
if not hasattr(self, '_cache'):
101-
self._cache = {}
102-
if not func in self._cache:
103-
self._cache[func] = {}
104-
#
105-
cache = self._cache[func]
106-
if key not in cache:
107-
cache[key] = func(self, *args, **kwargs)
108-
return cache[key]
10996
def memoize(func):
97+
@wraps(func)
98+
def _memoize(self, *args, **kwargs):
99+
'''Memoize wrapper for instance methods. Use @lru_cache for functions.'''
100+
if kwargs: # frozenset is used to ensure hashability
101+
key = args, frozenset(list(kwargs.items()))
102+
else:
103+
key = args
104+
# instance method, set up cache if needed
105+
if not hasattr(self, '_cache'):
106+
self._cache = {}
107+
if not func in self._cache:
108+
self._cache[func] = {}
109+
#
110+
cache = self._cache[func]
111+
if key not in cache:
112+
cache[key] = func(self, *args, **kwargs)
113+
return cache[key]
114+
110115
if not hasattr(func, '__class__'):
111116
raise NotImplementedError("Use @lru_cache instead of memoize() for functions.")
112117
# For methods, we want the cache on the object, not on the class, in order
113118
# to not having to think about cache bloat and content becoming stale, so
114119
# we cannot set up the cache here.
115-
return decorate(func, _memoize)
120+
return _memoize
116121

117122

118123
def ignore_view_kwargs(*args):

0 commit comments

Comments
 (0)