|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
"""This module is used for decorators.""" |
|
3
|
|
|
|
|
4
|
|
|
import time # spinner delay, timer decorator |
|
5
|
|
|
import math # rounding |
|
6
|
|
|
import os # path check |
|
7
|
|
|
import sqlite3 # the sql library |
|
8
|
|
|
from bbarchivist import dummy # useless stdout, dummy exception |
|
9
|
|
|
|
|
10
|
|
|
__author__ = "Thurask" |
|
11
|
|
|
__license__ = "WTFPL v2" |
|
12
|
|
|
__copyright__ = "Copyright 2015-2016 Thurask" |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
def wrap_keyboard_except(method): |
|
16
|
|
|
""" |
|
17
|
|
|
Decorator to absorb KeyboardInterrupt. |
|
18
|
|
|
|
|
19
|
|
|
:param method: Method to use. |
|
20
|
|
|
:type method: function |
|
21
|
|
|
""" |
|
22
|
|
|
def wrapper(*args, **kwargs): |
|
23
|
|
|
""" |
|
24
|
|
|
Try function, absorb KeyboardInterrupt and leave gracefully. |
|
25
|
|
|
""" |
|
26
|
|
|
try: |
|
27
|
|
|
method(*args, **kwargs) |
|
28
|
|
|
except KeyboardInterrupt: |
|
|
|
|
|
|
29
|
|
|
pass |
|
30
|
|
|
return wrapper |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
def timer(method): |
|
34
|
|
|
""" |
|
35
|
|
|
Decorator to time a function. |
|
36
|
|
|
|
|
37
|
|
|
:param method: Method to time. |
|
38
|
|
|
:type method: function |
|
39
|
|
|
""" |
|
40
|
|
|
def wrapper(*args, **kwargs): |
|
41
|
|
|
""" |
|
42
|
|
|
Start clock, do function with args, print rounded elapsed time. |
|
43
|
|
|
""" |
|
44
|
|
|
starttime = time.clock() |
|
45
|
|
|
method(*args, **kwargs) |
|
46
|
|
|
endtime = time.clock() - starttime |
|
47
|
|
|
endtime_proper = math.ceil(endtime * 100) / 100 # rounding |
|
48
|
|
|
mins, secs = divmod(endtime_proper, 60) |
|
49
|
|
|
hrs, mins = divmod(mins, 60) |
|
50
|
|
|
print("COMPLETED IN {0:02d}:{1:02d}:{2:02d}".format(int(hrs), int(mins), int(secs))) |
|
51
|
|
|
return wrapper |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
def sql_excepthandler(integrity): |
|
55
|
|
|
""" |
|
56
|
|
|
Decorator to handle sqlite3.Error. |
|
57
|
|
|
|
|
58
|
|
|
:param integrity: Whether to allow sqlite3.IntegrityError. |
|
59
|
|
|
:type integrity: bool |
|
60
|
|
|
""" |
|
61
|
|
|
def exceptdecorator(method): |
|
62
|
|
|
""" |
|
63
|
|
|
Call function in sqlite3.Error try/except block. |
|
64
|
|
|
|
|
65
|
|
|
:param method: Method to use. |
|
66
|
|
|
:type method: function |
|
67
|
|
|
""" |
|
68
|
|
|
def wrapper(*args, **kwargs): |
|
69
|
|
|
""" |
|
70
|
|
|
Try function, handle sqlite3.Error, optionally pass sqlite3.IntegrityError. |
|
71
|
|
|
""" |
|
72
|
|
|
try: |
|
73
|
|
|
result = method(*args, **kwargs) |
|
74
|
|
|
return result |
|
75
|
|
|
except sqlite3.IntegrityError if bool(integrity) else dummy.DummyException: |
|
76
|
|
|
dummy.UselessStdout.write("ASDASDASD") # DummyException never going to happen |
|
77
|
|
|
except sqlite3.Error as sqerror: |
|
78
|
|
|
print(sqerror) |
|
79
|
|
|
return wrapper |
|
80
|
|
|
return exceptdecorator |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
def sql_existhandler(sqlpath): |
|
84
|
|
|
""" |
|
85
|
|
|
Decorator to check if SQL database exists. |
|
86
|
|
|
|
|
87
|
|
|
:param sqlpath: Path to SQL database. |
|
88
|
|
|
:type sqlpath: str |
|
89
|
|
|
""" |
|
90
|
|
|
def existdecorator(method): |
|
91
|
|
|
""" |
|
92
|
|
|
Call function if SQL database exists. |
|
93
|
|
|
|
|
94
|
|
|
:param method: Method to use. |
|
95
|
|
|
:type method: function |
|
96
|
|
|
""" |
|
97
|
|
|
def wrapper(*args, **kwargs): |
|
98
|
|
|
""" |
|
99
|
|
|
Try function, absorb KeyboardInterrupt and leave gracefully. |
|
100
|
|
|
""" |
|
101
|
|
|
if os.path.exists(sqlpath): |
|
102
|
|
|
result = method(*args, **kwargs) |
|
103
|
|
|
return result |
|
104
|
|
|
else: |
|
105
|
|
|
print("NO SQL DATABASE FOUND!") |
|
106
|
|
|
raise SystemExit |
|
107
|
|
|
return wrapper |
|
108
|
|
|
return existdecorator |
|
109
|
|
|
|
Except handlers which only contain
passand do not have anelseclause can usually simply be removed: