/usr/lib/python2.7/dist-packages/versuchung/tools.py is in python-versuchung 1.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # This file is part of versuchung.
#
# versuchung is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# versuchung is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# versuchung. If not, see <http://www.gnu.org/licenses/>.
import logging
class JavascriptStyleDictAccess(dict):
def __init__(self, d):
self.update(d)
def __getattribute__(self, name):
try:
return dict.__getattribute__(self, name)
except AttributeError:
pass
if name in self:
return self[name]
name = name.replace("_", "-")
if name in self:
return self[name]
raise AttributeError
def setup_logging(log_level):
""" setup the logging module with the given log_level """
l = logging.WARNING # default
if log_level == 1:
l = logging.INFO
elif log_level >= 2:
l = logging.DEBUG
logging.basicConfig(level=l)
def before(decorator_argument):
"""Decorator for executing functions before other functions"""
def decorator(func):
def wrapped(self, *args, **kwargs):
# Late binding
inb4 = decorator_argument
if type(decorator_argument) == str:
inb4 = getattr(self, decorator_argument)
if "func_code" in dir(inb4):
argcount = inb4.func_code.co_argcount
else:
raise RuntimeError("Invalid argument to decorator")
if argcount == 1:
inb4(self)
elif argcount == 0:
inb4()
else:
raise RuntimeError("Unexpected parameter count")
return func(self, *args, **kwargs)
wrapped.__doc__ = func.__doc__
return wrapped
return decorator
class Singleton(object):
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(Singleton, cls).__new__(
cls, *args, **kwargs)
return cls._instance
class AdviceManager(Singleton):
def __init__(self):
if not "before" in dir(self):
self.before = dict()
self.around = dict()
self.after = dict()
def around_wrapper(self, func, last = None):
def wrapped(args, kwargs):
if last:
return func(last, args, kwargs)
else:
return func(*args, **kwargs)
return wrapped
@staticmethod
def advicable(func):
"""Decorator to mark a function as advicable"""
if not "func_name" in dir(func):
raise ValueError("No function adviced")
full_name = "%s.%s" % (func.__module__, func.func_name)
self = AdviceManager()
if full_name in self.before:
raise RuntimeError("Function already marked as advicable")
self.before[full_name] = []
self.around[full_name] = []
self.after[full_name] = []
def wrapped(*args, **kwargs):
am = AdviceManager()
for f in am.before[full_name]:
ret = f(args, kwargs)
if ret:
(args, kwargs) = ret
if len(am.around[full_name]) > 0:
func_ = am.around_wrapper(func, None)
for f in am.around[full_name]:
func_ = am.around_wrapper(f, func_)
ret = func_(args, kwargs)
else:
ret = func(*args, **kwargs)
for f in am.after[full_name]:
ret = f(ret)
return ret
wrapped.__doc__ = func.__doc__
return wrapped
class Advice:
def __init__(self, method, enabled = False):
self.method = method
am = AdviceManager()
self.am = am
if not method in am.before:
raise RuntimeError("Function was not marked @advicable")
self.enabled = False
if enabled:
self.enable()
def disable(self):
am = self.am
am.before[self.method] = [ x for x in am.before[self.method]
if x != self.before ]
am.around[self.method] = [ x for x in am.around[self.method]
if x != self.around ]
am.after[self.method] = [ x for x in am.after[self.method]
if x != self.after ]
def enable(self):
am = self.am
if self.enabled:
return
# Hook only in if the methods are overwritten
if self.before.im_func != Advice.before.im_func:
am.before[self.method].append(self.before)
if self.around.im_func != Advice.around.im_func:
am.around[self.method].append(self.around)
if self.after.im_func != Advice.after.im_func:
am.after[self.method].append(self.after)
self.enabled = True
def before(self, args, kwargs):
return (args, kwargs)
def around(self, func, args, kwargs):
return func(args, kwargs)
def after(self, ret):
return ret
|