-
Notifications
You must be signed in to change notification settings - Fork 1
/
decorators.py
47 lines (40 loc) · 1.4 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
import logging
import logging.handlers
from functools import wraps
from config import ADMIN, PASSWORD
from handlers import TlsSMTPHandler
from filepath import Filepath
class smoothBotError(object):
"""
decorator for Bot functions. If any bot method throws an exception,
the bot's browser quits, and the exception is reraised so it can pass through
to the upper-level logging logic
"""
def __init__(self, f):
self.f = f
def __get__(self, instance, owner):
""" Supports instance methods """
self.cls = owner
self.obj = instance
return self.__call__
def __call__(self):
@wraps(self.f) # So that the .__name__ and .__doc__ of f don't change
def wrapper(*args, **kwargs):
try:
self.f.__call__(*args, **kwargs)
except:
self.obj.quit_browser() # also closes display, if appropriate
raise
return wrapper
# def smoothBotError(func):
# @wraps(func) # So that the .__name__ and .__doc__ of f don't change
# def decorator(self, *args, **kwargs):
# print 'instance %s of class %s is now decorated whee!' % (
# self.username, self.__class__
# )
# try:
# func(*args, **kwargs)
# except:
# self.quit_browser() # also closes display, if appropriate
# raise
# return decorator