1
|
|
|
#!/usr/bin/env python3 |
2
|
5 |
|
"""This module is used for decorators.""" |
3
|
|
|
|
4
|
5 |
|
import math # rounding |
5
|
5 |
|
import os # path check |
6
|
5 |
|
import sqlite3 # the sql library |
7
|
5 |
|
import sys # frozen |
8
|
|
|
|
9
|
5 |
|
from bbarchivist import compat # backwards compat |
10
|
5 |
|
from bbarchivist import dummy # useless stdout |
11
|
5 |
|
from bbarchivist import exceptions # exceptions |
12
|
|
|
|
13
|
5 |
|
__author__ = "Thurask" |
14
|
5 |
|
__license__ = "WTFPL v2" |
15
|
5 |
|
__copyright__ = "2015-2019 Thurask" |
16
|
|
|
|
17
|
|
|
|
18
|
5 |
|
def wrap_keyboard_except(method): |
19
|
|
|
""" |
20
|
|
|
Decorator to absorb KeyboardInterrupt. |
21
|
|
|
|
22
|
|
|
:param method: Method to use. |
23
|
|
|
:type method: function |
24
|
|
|
""" |
25
|
5 |
|
def wrapper(*args, **kwargs): |
26
|
|
|
""" |
27
|
|
|
Try function, absorb KeyboardInterrupt and leave gracefully. |
28
|
|
|
""" |
29
|
5 |
|
try: |
30
|
5 |
|
method(*args, **kwargs) |
31
|
5 |
|
except KeyboardInterrupt: |
32
|
5 |
|
dummy.UselessStdout.write("ASDASDASD") |
33
|
5 |
|
return wrapper |
34
|
|
|
|
35
|
|
|
|
36
|
5 |
|
def timer(method): |
37
|
|
|
""" |
38
|
|
|
Decorator to time a function. |
39
|
|
|
|
40
|
|
|
:param method: Method to time. |
41
|
|
|
:type method: function |
42
|
|
|
""" |
43
|
5 |
|
def wrapper(*args, **kwargs): |
44
|
|
|
""" |
45
|
|
|
Start clock, do function with args, print rounded elapsed time. |
46
|
|
|
""" |
47
|
5 |
|
starttime = compat.perf_clock() |
48
|
5 |
|
method(*args, **kwargs) |
49
|
5 |
|
endtime = compat.perf_clock() - starttime |
50
|
5 |
|
endtime_proper = math.ceil(endtime * 100) / 100 # rounding |
51
|
5 |
|
mins, secs = divmod(endtime_proper, 60) |
52
|
5 |
|
hrs, mins = divmod(mins, 60) |
53
|
5 |
|
print("COMPLETED IN {0:02d}:{1:02d}:{2:02d}".format(int(hrs), int(mins), int(secs))) |
54
|
5 |
|
return wrapper |
55
|
|
|
|
56
|
|
|
|
57
|
5 |
|
def sql_excepthandler(integrity): |
58
|
|
|
""" |
59
|
|
|
Decorator to handle sqlite3.Error. |
60
|
|
|
|
61
|
|
|
:param integrity: Whether to allow sqlite3.IntegrityError. |
62
|
|
|
:type integrity: bool |
63
|
|
|
""" |
64
|
5 |
|
def exceptdecorator(method): |
65
|
|
|
""" |
66
|
|
|
Call function in sqlite3.Error try/except block. |
67
|
|
|
|
68
|
|
|
:param method: Method to use. |
69
|
|
|
:type method: function |
70
|
|
|
""" |
71
|
5 |
|
def wrapper(*args, **kwargs): |
72
|
|
|
""" |
73
|
|
|
Try function, handle sqlite3.Error, optionally pass sqlite3.IntegrityError. |
74
|
|
|
""" |
75
|
5 |
|
return sql_exceptwrapper(method, integrity, *args, **kwargs) |
76
|
5 |
|
return wrapper |
77
|
5 |
|
return exceptdecorator |
78
|
|
|
|
79
|
|
|
|
80
|
5 |
|
def sql_exceptwrapper(method, integrity, *args, **kwargs): |
81
|
|
|
""" |
82
|
|
|
Try function, handle sqlite3.Error, optionally pass sqlite3.IntegrityError. |
83
|
|
|
|
84
|
|
|
:param method: Method to use. |
85
|
|
|
:type method: function |
86
|
|
|
|
87
|
|
|
:param integrity: Whether to allow sqlite3.IntegrityError. |
88
|
|
|
:type integrity: bool |
89
|
|
|
""" |
90
|
5 |
|
try: |
91
|
5 |
|
result = method(*args, **kwargs) |
92
|
5 |
|
return result |
93
|
5 |
|
except sqlite3.IntegrityError if bool(integrity) else exceptions.DummyException: |
94
|
5 |
|
dummy.UselessStdout.write("ASDASDASD") # DummyException never going to happen |
95
|
5 |
|
except sqlite3.Error as sqerror: |
96
|
5 |
|
print(sqerror) |
97
|
|
|
|
98
|
|
|
|
99
|
5 |
|
def sql_existhandler(sqlpath): |
100
|
|
|
""" |
101
|
|
|
Decorator to check if SQL database exists. |
102
|
|
|
|
103
|
|
|
:param sqlpath: Path to SQL database. |
104
|
|
|
:type sqlpath: str |
105
|
|
|
""" |
106
|
5 |
|
def existdecorator(method): |
107
|
|
|
""" |
108
|
|
|
Call function if SQL database exists. |
109
|
|
|
|
110
|
|
|
:param method: Method to use. |
111
|
|
|
:type method: function |
112
|
|
|
""" |
113
|
5 |
|
def wrapper(*args, **kwargs): |
114
|
|
|
""" |
115
|
|
|
Check existence of database, leave if it doesn't. |
116
|
|
|
""" |
117
|
5 |
|
if os.path.exists(sqlpath): |
118
|
5 |
|
result = method(*args, **kwargs) |
119
|
5 |
|
return result |
120
|
|
|
else: |
121
|
5 |
|
print("NO SQL DATABASE FOUND!") |
122
|
5 |
|
raise SystemExit |
123
|
5 |
|
return wrapper |
124
|
5 |
|
return existdecorator |
125
|
|
|
|
126
|
|
|
|
127
|
5 |
|
def enter_to_exit(checkfreeze=True): |
128
|
|
|
""" |
129
|
|
|
Press enter to exit a script. |
130
|
|
|
|
131
|
|
|
:param checkfreeze: If this triggers only in frozen executables. Default is true. |
132
|
|
|
:type checkfreeze: bool |
133
|
|
|
""" |
134
|
5 |
|
greenlight = bool(getattr(sys, 'frozen', False)) if checkfreeze else True |
135
|
5 |
|
if greenlight: |
136
|
5 |
|
smeg = input("Press Enter to exit") |
137
|
5 |
|
if smeg or not smeg: |
138
|
|
|
raise SystemExit |
139
|
|
|
|