-
Notifications
You must be signed in to change notification settings - Fork 8
/
basic_tools.py
90 lines (63 loc) · 2.22 KB
/
basic_tools.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
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
#!/usr/bin/env python3
import json
from datetime import datetime, timezone
from dns_tls_constants import *
def load_cache(filename):
try:
with open(f'{filename}.json', 'r') as settings:
cache = json.load(settings)
except FileNotFoundError:
cache = {'top_domains': {}, 'filter': []}
return cache
def write_cache(top_domains):
with open('top_domains.json', 'r') as cache:
f_cache = json.load(cache)
f_cache['top_domains'] = top_domains
with open('top_domains.json', 'w') as cache:
json.dump(f_cache, cache, indent=4)
def looper(sleep_len):
def decorator(loop_function):
# pre-process logic to optimize decorated functions with NO_DELAY set
if (sleep_len):
def wrapper(*args):
for _ in RUN_FOREVER():
loop_function(*args)
fast_sleep(sleep_len)
else:
def wrapper(*args):
for _ in RUN_FOREVER():
loop_function(*args)
return wrapper
return decorator
class Log:
@classmethod
def setup(cls, *, console, verbose):
# define function to print log message. this will overload verbose function if enabled.
if (verbose):
@classmethod
def func(cls, thing_to_print):
console_log(f'[{cls.time()}][verbose]{thing_to_print}')
# overloading verbose method with newly defined function.
setattr(cls, 'verbose', func)
if (console):
@classmethod
def func(cls, thing_to_print):
console_log(f'[{cls.time()}][console]{thing_to_print}')
# overloading console method with newly defined function.
setattr(cls, 'console', func)
@classmethod
def system(cls, msg):
console_log(f'[{cls.time()}][system]{msg}')
@classmethod
def console(cls, msg):
pass
@classmethod
def error(cls, msg):
console_log(f'[{cls.time()}][error]{msg}')
@staticmethod
def verbose(msg):
pass
@staticmethod
def time(tz=timezone.utc):
xt = datetime.now(tz).timetuple()
return f'{xt.tm_mon}/{xt.tm_mday} {xt.tm_hour}:{xt.tm_min}:{xt.tm_sec}'