-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyranj.py
53 lines (42 loc) · 1.46 KB
/
pyranj.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
from logger import logger
PREFIX = 'prefix'
FUNC = 'func'
RUN = 'run'
def pyranj_message(e, prefix=None):
if prefix is None:
prefix = '[EXCEPTION]'
return f'{prefix} :: {e}'
def pyranj_decorator(prefix=None):
def decorator(f):
def wrapper(*args, **kwargs):
try:
return f(*args, **kwargs)
except Exception as e:
logger.log(pyranj_message(e, prefix))
return wrapper
return decorator
def get_pyranj(prefix_=None):
class InnerPyRanjMeta(type):
def __call__(cls, *args, **kwargs):
if cls != InnerPyRanj:
return super(InnerPyRanjMeta, cls).__call__(*args, **kwargs)
param = args[0] if args else None
func = param if type(param) == type(pyranj_decorator) else None
prefix = kwargs.get(PREFIX, None)
if func:
return pyranj_decorator(prefix_)(func)
return get_pyranj(prefix)
def __enter__(self):
pass
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type:
logger.log(pyranj_message(exc_val, prefix=prefix_))
return True
class InnerPyRanj(metaclass=InnerPyRanjMeta):
def __getattribute__(self, item):
attr = super().__getattribute__(item)
if item == RUN:
return pyranj_decorator(prefix_)(attr)
return attr
return InnerPyRanj
PyRanj = get_pyranj()