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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
'''
Very commonly used methods supporting
most things, starting threads, writing
or reading files or executing a program,
things that both the client & server do.
Verified: 2021 February 8
* Follows PEP8
* Tested Platforms
* Windows 10
'''
from shared.state import Static
from shared.error import Error
import subprocess
import threading
import tempfile
import shutil
import time
import os
class Store:
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
class Helper:
WRITE_BYTES = 'wb'
READ_BYTES = 'rb'
APPEND = 'a'
WRITE = 'w'
READ = 'r'
@staticmethod
@Error.quiet
def write_file(filepath, data, mode=APPEND):
if mode == Helper.WRITE_BYTES:
with open(filepath, mode=mode) as wf:
wf.write(data)
else:
with open(filepath, mode=mode,
encoding=Static.ENCODING,
errors=Static.ERRORS) as wf:
wf.write(data)
@staticmethod
@Error.quiet
def read_file(filepath, mode=READ):
if mode == Helper.READ_BYTES:
with open(filepath, mode=mode) as rf:
return rf.read()
else:
with open(filepath, mode=mode,
encoding=Static.ENCODING) as rf:
return rf.read()
@staticmethod
def clear_pyinstaller_temp():
if Static.EXE:
temp_dir = tempfile.gettempdir()
for filename in os.listdir(temp_dir):
if filename.startswith('_MEI'):
if filename != Static.MEI:
try:
shutil.rmtree(os.path.join(
temp_dir, filename), True)
except Exception:
pass
@staticmethod
def store(dictionary, keys):
for key in keys:
if key not in dictionary:
dictionary[key] = False
return (dictionary, Store(**dictionary))
@staticmethod
def run(args, shell=False):
process = subprocess.run(args, shell=shell,
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
if process.returncode == 0:
return True
else:
return False
@staticmethod
def start(filepath):
if Static.WINDOWS:
os.startfile(filepath)
elif Static.MAC:
Helper.run(('open', filepath))
else:
Helper.run(('xdg-open', filepath))
@staticmethod
def thread(callback, *args):
threading.Thread(target=callback,
args=args, daemon=True).start()
@staticmethod
def timestamp(date=time):
return date.strftime('%Y-%m-%d (%H:%M:%S)')
@staticmethod
def plural(iterable, end='s'):
if len(iterable) == 1:
return ''
else:
return end
@staticmethod
def join(*args):
return '\n'.join(args)
|