forked from infobyte/faraday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorators.py
75 lines (61 loc) · 2.37 KB
/
decorators.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
'''
Faraday Penetration Test IDE
Copyright (C) 2013 Infobyte LLC (http://www.infobytesec.com/)
See the file 'doc/LICENSE' for the license information
'''
import restkit.errors
import model
def simple_decorator(decorator):
'''this decorator can be used to turn simple functions
into well-behaved decorators, so long as the decorators
are fairly simple. If a decorator expects a function and
returns a function (no descriptors), and if it doesn't
modify function attributes or docstring, then it is
eligible to use this. Simply apply @simple_decorator to
your decorator and it will automatically preserve the
docstring and function attributes of functions to which
it is applied.'''
def new_decorator(f):
g = decorator(f)
g.__name__ = f.__name__
g.__doc__ = f.__doc__
g.__dict__.update(f.__dict__)
return g
new_decorator.__name__ = decorator.__name__
new_decorator.__doc__ = decorator.__doc__
new_decorator.__dict__.update(decorator.__dict__)
return new_decorator
@simple_decorator
def modify_class_field(func):
def wrapper(self, *args, **kwargs):
self.cuca = "eehh"
return func(self, *args, **kwargs)
return wrapper
@simple_decorator
def updateLocalMetadata(func):
def wrapper(self, *args, **kwargs):
self.updateMetadata()
return func(self, *args, **kwargs)
return wrapper
@simple_decorator
def passPermissionsOrRaise(func):
def wrapper(self, *args, **kwargs):
self.checkPermissions(op = func.func_name)
return func(self, *args, **kwargs)
return wrapper
@simple_decorator
def trap_timeout(func):
def wrapper(self, *args, **kwargs):
try:
if self._lostConnection:
# REFACTOR
WorkspacePersister.addPendingAction(self, func, args, kwargs)
return func(self, *args, **kwargs)
except restkit.errors.RequestError as req_error:
self.lostConnectionResolv()
WorkspacePersister.stopThreads()
WorkspacePersister.addPendingAction(self, func, args, kwargs)
WorkspacePersister.notifyPersisterConnectionLost()
model.api.devlog("Operation [%s] timeout" % func.__name__)
return func(self, *args, **kwargs)
return wrapper