-
Notifications
You must be signed in to change notification settings - Fork 3
/
commons.py
146 lines (113 loc) · 3.79 KB
/
commons.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os
from pathlib import Path
from datetime import datetime
import yaml
# Windows
if os.name == 'nt':
import msvcrt
# Posix (Linux, OS X)
else:
import sys
import termios
import atexit
from select import select
class Constants:
dir_logs = Path("logs/")
dir_bank = Path("bank/")
config_file = Path('config.yml')
class KBHit:
"""
A Python class implementing KBHIT, the standard keyboard-interrupt poller.
Works transparently on Windows and Posix (Linux, Mac OS X). Doesn't work
with IDLE.
Under LGP License.
Made by Simon D. Levy as part of a compilation of software
https://simondlevy.academic.wlu.edu/software/
Implemented here with a few modifications.
"""
def __init__(self):
"""
Creates a KBHit object that you can call to do various keyboard things.
"""
if os.name == 'nt':
pass
else:
# Save the terminal settings
self.fd = sys.stdin.fileno()
self.new_term = termios.tcgetattr(self.fd)
self.old_term = termios.tcgetattr(self.fd)
# New terminal setting unbuffered
self.new_term[3] = (self.new_term[3] & ~termios.ICANON & ~termios.ECHO)
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.new_term)
# Support normal-terminal reset at exit
atexit.register(self.set_normal_term)
def set_normal_term(self):
"""
Resets to normal terminal. On Windows this is a no-op.
"""
if os.name == 'nt':
pass
else:
termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old_term)
def getch(self):
"""
Returns a keyboard character after kbhit() has been called.
Should not be called in the same program as getarrow().
"""
s = ''
if os.name == 'nt':
return msvcrt.getch().decode('utf-8')
else:
return sys.stdin.read(1)
def getarrow(self):
"""Returns an arrow-key code after kbhit() has been called. Codes are
0 : up
1 : right
2 : down
3 : left
Should not be called in the same program as getch().
"""
if os.name == 'nt':
msvcrt.getch() # skip 0xE0
c = msvcrt.getch()
vals = [72, 77, 80, 75]
else:
c = sys.stdin.read(3)[2]
vals = [65, 67, 66, 68]
return vals.index(ord(c.decode('utf-8')))
def kbhit(self):
"""
Returns True if keyboard character was hit, False otherwise.
"""
if os.name == 'nt':
return msvcrt.kbhit()
else:
dr,dw,de = select([sys.stdin], [], [], 0)
return dr != []
class BaseClass:
"""
Base class for both bank and inspector
"""
def _log(self, message, stdio: bool = True, in_file: bool = False, file_mode: str = "a"):
prefix = datetime.now().strftime("%Y-%m-%d:%H:%M:%S ")
output_string = f"{prefix}{message}"
if stdio:
print(output_string)
if in_file:
with open(self.log_database, mode=file_mode) as f:
f.write(output_string + "\n")
def _id_to_index(self, bid: int) -> int:
for i, branch in enumerate(self.branches):
if branch["id"] == bid:
return i
def get_config(self, path=None):
"""
Gets and prepares configurations from yaml config file
"""
if path is None:
path = Constants.config_file
with open(path, 'r') as yaml_file:
self.config = yaml.load(yaml_file, Loader=yaml.FullLoader)
self.brnch_confs = self.config['branches']
self.bank_confs = self.config['bank']
self.inspctr_confs = self.config['inspector']