-
Notifications
You must be signed in to change notification settings - Fork 4
/
wireutils.py
402 lines (367 loc) · 10.2 KB
/
wireutils.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
"""
Wire Segal's utility library.
Do whatever with it, I seriously couldn't care less.
Runs 2.6+ onwards.
"""
from __future__ import print_function
import os, json, time, sys, re, traceback, threading
def format(string, **kwargs):
"""
Format strings with **kwargs.
"""
for arg in kwargs:
regex = re.compile(r"\{" + arg + r"\}", re.IGNORECASE)
string = regex.sub(str(kwargs[arg]), string)
for color in ansi_colors.COLORS:
regex = re.compile(r"\{" + color + r"\}", re.IGNORECASE)
string = regex.sub(str(ansi_colors.COLORS[color]), string)
return string
def format_traceback(e, text="Traceback (most recent call last):"):
"""
Format a traceback into a printable string.
"""
if not hasattr(e, "__traceback__"):
if str(e):
return str(type(e).__name__) + ": " + str(e)
return str(type(e).__name__)
trace = traceback.extract_tb(e.__traceback__) # Get the traceback object
error = format("{text}\n", text=text) # Start out with `text`
# Iterate through the traceback and add each iteration to the string
for filename,lineno,function,message in trace:
error += format(" File \"{name}\", line {num}, in {funcname}\n",
name=filename,
num=lineno,
funcname=function)
if message:
error += format(" {data}\n", data=message)
# Add the type and message of the error
error += str(type(e).__name__)
if str(e): error += format(": {description}", description=e)
return error
class Registry:
def __init__(self):
self.events = {}
def on(self, tag, *args):
if tag not in self.events:
self.events[tag] = {}
funcid = -1
for i in self.events[tag]:
funcid = max(funcid, i)
funcid += 1
self.events[tag][funcid] = args
return funcid
def deregister(self, tag, funcid):
if tag in self.events:
if funcid in self.events[tag]:
del self.events[tag][funcid]
return True
return False
def hash(self):
return hash(str(self.events))
def graft(self, reg):
for key in reg.events:
if key not in self.events:
self.events[key] = {}
for oldjob in reg.events[key]:
newjob = -1
for i in self.events[key]:
newjob = max(newjob, i)
newjob += 1
self.events[key][newjob] = reg.events[key][oldjob]
class Config:
"""
A JSON read-only loader that will update automatically from `path`.
"""
def __init__(self, path):
self.path = path
self.lastmodtime = os.path.getctime(path) # get the last modified time of the target file
self.data = json.load(open(path))
def reload(self):
if os.path.getctime(self.path) > self.lastmodtime: # check the last modified time of the target file
self.data = json.load(open(self.path))
self.lastmodtime = os.path.getctime(self.path)
# These are extensions of self.data's methods, except they run self.reload.
def __getitem__(self, y):
self.reload()
return self.data[y]
def __contains__(self, key):
self.reload()
return key in self.data
def get(self, k, d=None):
self.reload()
return self.data.get(k, d)
def date_time_string(timestamp=None):
"""
Return the current date and time formatted for a message header.
"""
monthname = [None,
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
now = time.time()
year, month, day, hh, mm, ss, x, y, z = time.localtime(now)
# Add zeroes to keep the length of the timestamp constant
hh = format("0{hours}", hours=hh) if hh < 10 else str(hh)
mm = format("0{minutes}", minutes=mm) if mm < 10 else str(mm)
ss = format("0{seconds}", seconds=ss) if ss < 10 else str(ss)
day = format("0{day}", day=day) if day < 10 else str(day)
s = format("{magenta}[{dd}/{mon}/{yyyy} {hh}:{mm}:{ss}]{endc} ",
dd = day,
mon = monthname[month],
yyyy = year,
hh = hh,
mm = mm,
ss = ss)
return s
def supports_color():
"""
Returns True if the running system's terminal supports color, and False
otherwise.
"""
plat = sys.platform
supported_platform = plat != 'Pocket PC' and (plat != 'win32' or
'ANSICON' in os.environ)
# isatty is not always implemented, #6223.
is_a_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
if not supported_platform or not is_a_tty:
return False
return True
color_supported = supports_color()
if color_supported:
class ansi_colors: # All color codes
"""
A helper class containing colors (for pretty printing.)
"""
BLACK = '\033[30m'
DARKRED = '\033[31m'
DARKGREEN = '\033[32m'
DARKYELLOW = '\033[33m'
DARKBLUE = '\033[34m'
PURPLE = '\033[35m'
DARKCYAN = '\033[36m'
GRAY = '\033[37m'
DARKGRAY = '\033[90m'
RED = '\033[91m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
BLUE = '\033[94m'
MAGENTA = '\033[95m'
CYAN = '\033[96m'
WHITE = '\033[97m'
ORANGE = '\033[38;5;202m'
DARKPURPLE = '\033[38;5;53m'
BROWN = '\033[38;5;94m'
PEACH = '\033[38;5;208m'
GOLDEN = '\033[38;5;166m'
BOLD = '\033[1m'
LINE = '\033[4m'
REMAKELINE = '\033[F\033[K'
ENDC = '\033[0m'
COLORS = {
"black": BLACK,
"darkred": DARKRED,
"darkgreen": DARKGREEN,
"darkyellow": DARKYELLOW,
"darkblue": DARKBLUE,
"purple": PURPLE,
"darkcyan": DARKCYAN,
"gray": GRAY,
"darkgray": DARKGRAY,
"red": RED,
"green": GREEN,
"yellow": YELLOW,
"blue": BLUE,
"magenta": MAGENTA,
"cyan": CYAN,
"white": WHITE,
"orange": ORANGE,
"darkpurple": DARKPURPLE,
"brown": BROWN,
"peach": PEACH,
"golden": GOLDEN,
"bold": BOLD,
"line": LINE,
"remakeline": REMAKELINE,
"endc": ENDC
}
else:
class ansi_colors: # No color codes
"""
A helper class containing no colors, allowing systems that don't support ANSI to continue running without strange logs.
"""
BLACK = ''
DARKRED = ''
DARKGREEN = ''
DARKYELLOW = ''
DARKBLUE = ''
PURPLE = ''
DARKCYAN = ''
GRAY = ''
DARKGRAY = ''
RED = ''
GREEN = ''
YELLOW = ''
BLUE = ''
MAGENTA = ''
CYAN = ''
WHITE = ''
ORANGE = ''
DARKPURPLE = ''
BROWN = ''
PEACH = ''
GOLDEN = ''
BOLD = ''
LINE = ''
REMAKELINE = ''
ENDC = ''
COLORS = {
"black": BLACK,
"darkred": DARKRED,
"darkgreen": DARKGREEN,
"darkyellow": DARKYELLOW,
"darkblue": DARKBLUE,
"purple": PURPLE,
"darkcyan": DARKCYAN,
"gray": GRAY,
"darkgray": DARKGRAY,
"red": RED,
"green": GREEN,
"yellow": YELLOW,
"blue": BLUE,
"magenta": MAGENTA,
"cyan": CYAN,
"white": WHITE,
"orange": ORANGE,
"darkpurple": DARKPURPLE,
"brown": BROWN,
"peach": PEACH,
"golden": GOLDEN,
"bold": BOLD,
"line": LINE,
"remakeline": REMAKELINE,
"endc": ENDC
}
def rainbonify(string):
if not color_supported: return string
else:
colors = [ansi_colors.RED, ansi_colors.ORANGE, ansi_colors.YELLOW, ansi_colors.GREEN,
ansi_colors.BLUE, ansi_colors.PURPLE, ansi_colors.DARKPURPLE]
nstring = ""
cind = 0
for i in string:
nstring += colors[cind] + i
cind += 1
cind %= len(colors)
return nstring + ansi_colors.ENDC
class color_config:
"""
An object used to configure color_print and color_input.
"""
def __init__(self):
self.color = ansi_colors.WHITE
self.name = "Generic"
def tag(self):
"""
Return the tag for pretty printing from the config.
"""
return format("{color}[{name}] {endc}",
color=self.color,
name=self.name)
def whitespace(self):
"""
Return the whitespace for non-printed lines.
"""
return " "*(26+len(self.name))
color_printing_config = color_config() # create the instance of color_config used to configure color_print and color_input
lastprinted = None
print_lock = threading.Lock()
def color_print(text, color="", strip=False, func=print, add_newline=False, colorconfig = None, **kwargs):
"""
Pretty print `text`, with `color` as its color, using `func`.
If `strip`, then remove whitespace from both sides of each line.
"""
global lastprinted, print_lock
timestamp = date_time_string()
print_lock.acquire()
if not colorconfig:
colorconfig = color_printing_config
if "whitespace" not in kwargs:
kwargs["whitespace"] = colorconfig.whitespace()
kwargs["color"] = color
text = format(str(text), **kwargs)
# Make sure not to print the same thing twice
if text == lastprinted:
if not color_supported:
print_lock.release()
return
print(ansi_colors.REMAKELINE, end="")
lastprinted = text
# Split the text by lines
if strip:
prints = [i.strip() for i in text.split("\n")]
else:
prints = text.split("\n")
originstr = colorconfig.tag()
func(format("{timestamp}{processtag}{color}{text}{endc}",
timestamp = timestamp,
processtag = originstr,
color = color,
text = prints[0])) # Print the first line with a timestamp
if add_newline: func("\n")
for i in prints[1:]:
func(format("{whitespace}{color}{text}{endc}",
whitespace = colorconfig.whitespace(),
color = color,
text = i)) # Print all consecutive lines
if add_newline: func("\n")
print_lock.release()
try:
agnostic_input = raw_input
except:
agnostic_input = input
def color_input(text, color="", strip=False, func=agnostic_input, colorconfig = None, **kwargs):
"""
Pretty print `text`, with `color` as its color. Take input using `func` on the last line.
If `strip`, then remove whitespace from both sides of each line.
"""
global print_lock
timestamp = date_time_string()
print_lock.acquire()
if not colorconfig:
colorconfig = color_printing_config
if "whitespace" not in kwargs:
kwargs["whitespace"] = colorconfig.whitespace()
kwargs["color"] = color
text = format(str(text), **kwargs)
# Split the text by lines
if strip:
prints = [i.strip() for i in text.split("\n")]
prints[-1] += " " # Add a spacing to the last line
else:
prints = text.split("\n")
originstr = colorconfig.tag()
# Print in order if there's more than one line
if len(prints) > 1:
print(format("{timestamp}{processtag}{color}{text}",
timestamp = timestamp,
processtag = originstr,
color = color,
text = prints[0]))
for i in prints[1:-1]:
print(format("{whitespace}{color}{text}",
whitespace = colorconfig.whitespace(),
color = color,
text = i))
inp = func(format("{whitespace}{color}{text}{endc}",
whitespace = colorconfig.whitespace(),
color = color,
text = prints[-1]))
print_lock.release()
return inp
else:
inp = func(format("{timestamp}{processtag}{color}{text}{endc}",
timestamp = timestamp,
processtag = originstr,
color = color,
text = prints[0]))
print_lock.release()
return inp