Parent: [7a8f9a] (diff)

Child: [ccec7e] (diff)

Download this file

decorators.py    198 lines (176 with data), 6.3 kB

  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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import sys
import json
import logging
from collections import defaultdict
from urllib import unquote
from decorator import decorator
from tg.decorators import before_validate
from tg import request, redirect
from webob import exc
def task(func):
'''Decorator to add some methods to task functions'''
def post(*args, **kwargs):
from allura import model as M
return M.MonQTask.post(func, args, kwargs)
func.post = post
return func
class event_handler(object):
'''Decorator to register event handlers'''
listeners = defaultdict(set)
def __init__(self, *topics):
self.topics = topics
def __call__(self, func):
for t in self.topics:
self.listeners[t].add(func)
return func
class require_post(object):
def __init__(self, redir=None):
self.redir = redir
def __call__(self, func):
def check_method(remainder, params):
if request.method != 'POST':
if self.redir is not None:
redirect(self.redir)
raise exc.HTTPMethodNotAllowed(headers={'Allow':'POST'})
before_validate(check_method)(func)
return func
class log_action(object): # pragma no cover
def __init__(self,
logger=None,
level=logging.INFO,
msg=None,
*args, **kwargs):
if logger is None: logger = logging
self._logger = logger
self._level = level
self._msg = msg
self._args = args
self._kwargs = kwargs
self._extra_proto = dict(
user=None,
user_id=None,
source=None,
project_name=None,
group_id=None)
def __call__(self, func):
self._func = func
self._extra_proto.update(action=func.__name__)
if self._msg is None:
self._msg = func.__name__
result = lambda *args,**kwargs: self._wrapper(*args,**kwargs)
# assert not hasattr(func, 'decoration')
if hasattr(func, 'decoration'):
result.decoration = func.decoration
return result
def _wrapper(self, *args, **kwargs):
result = None
try:
try:
result = self._func(*args, **kwargs)
except exc.HTTPServerError:
raise
except exc.HTTPException, e:
result = e
args = self._args
kwargs = self._kwargs
extra = kwargs.setdefault('extra', {})
extra.update(self._make_extra(result))
self._logger.log(self._level, self._msg,
*self._args, **self._kwargs)
return result
except:
args = self._args
kwargs = self._kwargs
extra = kwargs.setdefault('extra', {})
extra.update(self._make_extra(result))
kwargs['exc_info'] = sys.exc_info()
self._logger.log(logging.ERROR, self._msg,
*self._args, **self._kwargs)
raise
def _make_extra(self, result=None):
'''Create a dict of extra items to be added to a log record
'''
extra = self._extra_proto.copy()
# Save the client IP address
client_ip = request.headers.get('X_FORWARDED_FOR', request.remote_addr)
client_ip = client_ip.split(',')[0].strip()
extra.update(client_ip=client_ip)
# Save the user info
user = getattr(request, 'user', None)
if user:
extra.update(user=user.username,
user_id=user.id)
# Save the project info
if (result
and isinstance(result, dict)
and 'p' in result
and result['p'] is not None):
extra.update(
source=result['p']['source'],
project_name=result['p']['shortname'],
group_id=result['p'].get('sf_id'))
# Log the referer cookie if it exists
referer_link = request.cookies.get('referer_link')
if referer_link:
referer_link = unquote(referer_link)
try:
referer_link = json.loads(referer_link)
except ValueError:
pass
extra['referer_link'] = referer_link
return extra
class exceptionless(object):
'''Decorator making the decorated function return 'error_result' on any
exceptions rather than propagating exceptions up the stack
'''
def __init__(self, error_result, log=None):
self.error_result = error_result
self.log = log
def __call__(self, fun):
fname = 'exceptionless(%s)' % fun.__name__
def inner(*args, **kwargs):
try:
return fun(*args, **kwargs)
except Exception as e:
if self.log:
self.log.exception('Error calling %s(args=%s, kwargs=%s): %s',
fname, args, kwargs, str(e))
return self.error_result
inner.__name__ = fname
return inner
def Property(function):
'''Decorator to easily assign descriptors based on sub-function names
See <http://code.activestate.com/recipes/410698-property-decorator-for-python-24/>
'''
keys = 'fget', 'fset', 'fdel'
func_locals = {'doc':function.__doc__}
def probeFunc(frame, event, arg):
if event == 'return':
locals = frame.f_locals
func_locals.update(dict((k,locals.get(k)) for k in keys))
sys.settrace(None)
return probeFunc
sys.settrace(probeFunc)
function()
return property(**func_locals)
def getattr_(obj, name, default_thunk):
"Similar to .setdefault in dictionaries."
try:
return getattr(obj, name)
except AttributeError:
default = default_thunk()
setattr(obj, name, default)
return default
@decorator
def memoize(func, *args):
"""
Cache the method's result, for the given args
"""
dic = getattr_(func, "memoize_dic", dict)
# memoize_dic is created at the first call
if args in dic:
return dic[args]
else:
result = func(*args)
dic[args] = result
return result