-
Notifications
You must be signed in to change notification settings - Fork 0
/
iosetting.py
283 lines (235 loc) · 8.4 KB
/
iosetting.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import json
import time
from enum import Enum
from collections.abc import Iterable
from PyQt5.QtWidgets import QWidget, QTextBrowser
from PyQt5 import QtGui
_iosetting__tag_dict = {
'CAPTAIN': '\033[94m', # Blue bright
'CAPTAIN_BUY_3': '\033[38;2;0;191;255m', # Deep Sky Blue
'CAPTAIN_BUY_2': '\033[38;2;30;144;255m', # Doder Blue
'CAPTAIN_BUY_1': '\033[38;2;65;105;255m', # Royal Blue
'CTRL': '\033[96m', # Sky blue bright
'ENTER': '\033[38;2;150;150;150m', # light gray
'ERROR': '\033[41m', # Red bottom
'FANS': '', # Normal
'GIFT': '\033[93m',
'GIFT_COMBO': '\033[93m', # Yellow bright
'NORMAL': '\033[90m', # Gray
'LIVE_SYS': '\033[38;2;125m', # half red
'SYSTEM': '\033[91m', # Red bright
'SUCCESS': '\033[42m', # Green bottom
'UP': '\033[92m', # Green bright
'WARNING': '\033[43m', # Yellow bottom
'TIPS': '\033[38;2;0;150;150m' # light blue with some green
}
_iosetting__head_set = {
'ERROR',
'LIVE_SYS',
'SYSTEM',
'SUCCESS',
'WARNING',
'TIPS'
}
class HeadSet(Enum):
captain_buy_3 = "DeepSkyBlue"
captain_buy_2 = "DodgerBlue"
captain_buy_1 = "RoyalBlue"
normal = 'Gray'
tips = "DarkCyan"
error = "Red"
warning = "Yellow"
success = "Lime"
system = "OrangeRed"
up = "Chartreuse"
fans = "Azure"
ctrl = "Aqua"
captain = "Blue"
_iosetting__head = {
'CAPTAIN': HeadSet.captain.value,
'CAPTAIN_BUY_3': HeadSet.captain_buy_3.value, # Deep Sky Blue
'CAPTAIN_BUY_2': HeadSet.captain_buy_2.value, # Doder Blue
'CAPTAIN_BUY_1': HeadSet.captain_buy_1.value, # Royal Blue
'CTRL': HeadSet.ctrl.value,
'ERROR': HeadSet.error.value,
'FANS': HeadSet.fans.value,
'NORMAL': HeadSet.normal.value,
'SYSTEM': HeadSet.system.value, # Red bright
'SUCCESS': HeadSet.success.value, # Green bottom
'UP': HeadSet.up.value, # Green bright
'WARNING': HeadSet.warning.value, # Yellow bottom
'TIPS': HeadSet.tips.value,
"SPECIAL": None
}
def print_simple(text: str, base: str = 'NORMAL',
special_color='FFFFFF', end='\n'):
begin_str = ''
end_str = '\033[m'
if base in _iosetting__tag_dict:
begin_str = _iosetting__tag_dict[base]
match base:
case 'SC' | 'SC_JPN': # customizing color
color_str = hex2dec_str(special_color)
begin_str = f'\033[38;2;{color_str}m'
case 'SPECIAL':
color_str = hex2dec_str(special_color)
begin_str = f'\033[38;2;{color_str}m'
print(begin_str, end='')
print(text, end='')
print(end_str, end=end)
def print_details(text: str, tag: str = 'NORMAL', debug_flag: bool = False,
head: str = None, prefix: str = None, log=False, special_color='FFFFFF', end='\n'):
begin_str = ''
end_str = '\033[m'
if tag in _iosetting__tag_dict:
begin_str = _iosetting__tag_dict[tag]
match tag:
case 'SC' | 'SC_JPN': # customizing color
color_str = hex2dec_str(special_color)
begin_str = f'\033[38;2;{color_str}m'
case 'SPECIAL':
color_str = hex2dec_str(special_color)
begin_str = f'\033[38;2;{color_str}m'
if tag in _iosetting__head_set:
if head is None:
head = tag
if head is not None:
if prefix is not None:
text = set_head(head, prefix) + str(text)
else:
text = set_head(head) + str(text)
# print format:
# head tail
# /---------\ /---\
# color_CTRL head prefix text suffix color_CTRL_end
# \033[xxm[HEAD->PREFIX]TEXT[SUFFIX]\033[m
# \033[91m[SYSTEM->REPLY MODULE]xxxxxxxxx\033[m
# new print format:
#
# bcCTRL head prefix func text suffix ecCTRL
# \033[xxm[HEAD:PREFIX->FUNC]TEXT -> suffix ecCTRL
# \033[91m[SYS:Rec->Queue]xxxxxxxxxxxxx -> auto\033[m
###
if not debug_flag:
print(begin_str, end='')
print(text, end='')
print(end_str, end=end)
if log or debug_flag:
log_file = open("./logging.txt", mode='a', encoding='utf-8')
log_file.write(text+end)
log_file.close()
def display_details(text: str, tag: str = 'NORMAL', ui: QTextBrowser | None = None,
head: str = None, prefix: str = None, special_color: str | None = None, newline: bool = True):
bracket = "<span style=\"color:{}\">{}{}</span>"
if newline:
bracket.join("<br>")
if tag in _iosetting__head_set:
if head is None:
head = tag
if tag not in _iosetting__head.keys():
tag = None
fro = ""
if head is not None:
if prefix is not None:
fro = set_head(head, prefix)
else:
fro = set_head(head)
if special_color is None:
if tag is None:
display_content = text
else:
display_content = bracket.format(_iosetting__head[tag], fro, text)
else:
display_content = bracket.format(special_color, fro, text)
if ui is not None:
ui.append(display_content)
try:
ui.moveCursor(11)
except Exception as e:
pass
return display_content
def display_simple(text: str, base: str = 'NORMAL',
special_color="White", newline=True, ui: QTextBrowser | None = None):
bracket = "<span style=\"color:{}\">{}</span>"
if newline:
bracket.join("<br>")
if base not in _iosetting__head:
base = None
if special_color is None:
if base is None:
display_content = text
else:
display_content = bracket.format(_iosetting__head[base], text)
else:
display_content = bracket.format(special_color, text)
if ui is not None:
ui.append(display_content)
return display_content
def logging(filename: str, txt: Iterable | str,
head: str = None, prefix: str = None, wrong_lvl: str = '',
exception: str = 'Unknown', except_msg: str = 'No Describe'):
if head is None:
log_head = f'{set_head(head=head, prefix=prefix)}'
else:
log_head = '[Default]'
local = time.localtime(time.time())
now = time.strftime("%H:%M:%S", t=local)
if isinstance(txt, Iterable):
lines = [f"--------{wrong_lvl}--------"]
lines.extend([f"[{now}|{log_head[1:-1]}]"])
lines.extend(['\t'+line for line in txt])
lines.extend([f"[except:{exception} | msg: {except_msg}]"])
"""
logging format:
--------CASE(error|warning|oops)--------
[hh:mm:ss|head->prefix]
line1
line2
...
[except: | msg:]
"""
else: # not iterable
lines = [f">> [{now}|{set_head(head=head, prefix=prefix)[1:-1]}]{txt}[ex:{exception}|msg:{except_msg}]"]
with open(f'./logging/{filename}', mode='a', encoding='utf-8') as f:
f.writelines(lines)
def logging_simple(filename: str, txt: str) -> None:
with open(f'./logging/{filename}', mode='a', encoding='utf-8') as f:
f.write(txt)
def set_head(head, prefix=None) -> str:
if prefix is None:
return f'[{head}]'
else:
return f'[{head}->{prefix}]'
def hex2dec_str(str16: str = '#FFFFFF') -> str:
if str16[0] == '#':
str16 = str16[1:]
if len(str16) > 6:
print_details('A wrong RGB color set', tag='WARNING')
str16 = str16[0:6]
print_details(f'new slice is {str16}', tag='WARNING')
R_channel = '0x'+str16[0:2]
G_channel = '0x'+str16[2:4]
B_channel = '0x'+str16[4:]
try:
decR = int(R_channel, 16)
decG = int(G_channel, 16)
decB = int(B_channel, 16)
except ValueError:
print_details('You set a WRONG RGB code, color has been reset to 0x000000', tag='ERROR')
decR = decG = decB = 0
trans_str = str(decR)+';'+str(decG)+';'+str(decB)
return trans_str # str -> xx;xx;xx
class JsonParser:
@staticmethod
def load(filename) -> dict|None:
json2dict = None
try:
with open(filename, mode='r', encoding='utf-8') as f:
json2dict = json.load(f)
except FileNotFoundError:
print(f'[debug]{filename} not Found')
return json2dict
@staticmethod
def dump(filename, dict2json, mode: str = 'a') -> None:
with open(filename, mode=mode, encoding='utf-8') as f:
json.dump(dict2json, f, indent=4, ensure_ascii=False)