-
Notifications
You must be signed in to change notification settings - Fork 239
/
core.py
365 lines (305 loc) · 8.83 KB
/
core.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
import logging
import traceback
import inspect
import sys
import os
from os.path import (
join,
isfile,
isdir,
exists,
realpath,
dirname,
)
import platform
VERSION = "4.3.0"
homePage = "https://github.com/ilius/pyglossary"
TRACE = 5
logging.addLevelName(TRACE, "TRACE")
class Formatter(logging.Formatter):
def __init__(self, *args, **kwargs):
logging.Formatter.__init__(self, *args, **kwargs)
self.fill = None # type: Optional[Callable[[str], str]]
def formatMessage(self, record):
msg = logging.Formatter.formatMessage(self, record)
if self.fill is not None:
msg = self.fill(msg)
return msg
class MyLogger(logging.Logger):
levelsByVerbosity = (
logging.CRITICAL,
logging.ERROR,
logging.WARNING,
logging.INFO,
logging.DEBUG,
TRACE,
logging.NOTSET,
)
levelNamesCap = [
"Critical",
"Error",
"Warning",
"Info",
"Debug",
"Trace",
"All", # "Not-Set",
]
def __init__(self, *args):
logging.Logger.__init__(self, *args)
self._verbosity = 3
self._timeEnable = False
def setVerbosity(self, verbosity: int) -> None:
self.setLevel(self.levelsByVerbosity[verbosity])
self._verbosity = verbosity
def getVerbosity(self) -> int:
return self._verbosity
def trace(self, msg: str):
self.log(TRACE, msg)
def pretty(self, data: "Any", header: str = "") -> None:
from pprint import pformat
self.debug(header + pformat(data))
def isDebug(self) -> bool:
return self.getVerbosity() >= 4
def newFormatter(self):
timeEnable = self._timeEnable
if timeEnable:
fmt = "%(asctime)s [%(levelname)s] %(message)s"
else:
fmt = "[%(levelname)s] %(message)s"
return Formatter(fmt)
def setTimeEnable(self, timeEnable: bool):
self._timeEnable = timeEnable
formatter = self.newFormatter()
for handler in self.handlers:
handler.setFormatter(formatter)
def addHandler(self, handler: "logging.Handler"):
# if want to add separate format (new config keys and flags) for ui_gtk
# and ui_tk, you need to remove this function and run handler.setFormatter
# in ui_gtk and ui_tk
logging.Logger.addHandler(self, handler)
handler.setFormatter(self.newFormatter())
def formatVarDict(
dct: "Dict[str, Any]",
indent: int = 4,
max_width: int = 80,
) -> str:
lines = []
pre = " " * indent
for key, value in dct.items():
line = pre + key + " = " + repr(value)
if len(line) > max_width:
line = line[:max_width - 3] + "..."
try:
value_len = len(value)
except TypeError:
pass
else:
line += f"\n{pre}len({key}) = {value_len}"
lines.append(line)
return "\n".join(lines)
def format_exception(
exc_info: "Optional[Tuple[Type, Exception, types.TracebackType]]" = None,
add_locals: bool = False,
add_globals: bool = False,
) -> str:
if not exc_info:
exc_info = sys.exc_info()
_type, value, tback = exc_info
text = "".join(traceback.format_exception(_type, value, tback))
if add_locals or add_globals:
try:
frame = inspect.getinnerframes(tback, context=0)[-1][0]
except IndexError:
pass
else:
if add_locals:
text += f"Traceback locals:\n{formatVarDict(frame.f_locals)}\n"
if add_globals:
text += f"Traceback globals:\n{formatVarDict(frame.f_globals)}\n"
return text
class StdLogHandler(logging.Handler):
colorsConfig = {
"CRITICAL": ("color.cmd.critical", 196),
"ERROR": ("color.cmd.error", 1),
"WARNING": ("color.cmd.warning", 208),
}
# 1: dark red (like 31m), 196: real red, 9: light red
# 15: white, 229: light yellow (#ffffaf), 226: real yellow (#ffff00)
def __init__(self, noColor: bool = False):
logging.Handler.__init__(self)
self.set_name("std")
self.noColor = noColor
self.config = {}
@property
def endFormat(self):
if self.noColor:
return ""
return "\x1b[0;0;0m"
def emit(self, record: logging.LogRecord) -> None:
msg = ""
if record.getMessage():
msg = self.format(record)
###
if record.exc_info:
_type, value, tback = record.exc_info
tback_text = format_exception(
exc_info=record.exc_info,
add_locals=(log.level <= logging.DEBUG),
add_globals=False,
)
if not msg:
msg = "unhandled exception:"
msg += "\n"
msg += tback_text
###
levelname = record.levelname
if levelname in ("CRITICAL", "ERROR"):
fp = sys.stderr
else:
fp = sys.stdout
if not self.noColor and levelname in self.colorsConfig:
key, default = self.colorsConfig[levelname]
colorCode = self.config.get(key, default)
startColor = f"\x1b[38;5;{colorCode}m"
msg = startColor + msg + self.endFormat
###
if fp is None:
print(f"fp=None, levelname={record.levelname}")
print(msg)
return
fp.write(msg + "\n")
fp.flush()
def checkCreateConfDir() -> None:
if not isdir(confDir):
if exists(confDir): # file, or anything other than directory
os.rename(confDir, confDir + ".bak") # we do not import old config
os.mkdir(confDir)
if not exists(userPluginsDir):
try:
os.mkdir(userPluginsDir)
except Exception as e:
log.warning(f"failed to create user plugins directory: {e}")
if not isfile(confJsonFile):
with open(rootConfJsonFile) as srcF, open(confJsonFile, "w") as usrF:
usrF.write(srcF.read())
def getDataDir():
virtualEnv = os.getenv("VIRTUAL_ENV")
if virtualEnv:
return join(
dirname(dirname(dirname(rootDir))),
virtualEnv, "share", "pyglossary",
)
if not (
rootDir.endswith("dist-packages") or
rootDir.endswith("site-packages")
):
return rootDir
parent3 = dirname(dirname(dirname(rootDir)))
if os.sep == "/":
return join(parent3, "share", "pyglossary")
_dir = join(
parent3,
f"Python{sys.version_info.major}{sys.version_info.minor}",
"share",
"pyglossary",
)
if isdir(_dir):
return _dir
_dir = join(parent3, "Python3", "share", "pyglossary")
if isdir(_dir):
return _dir
_dir = join(parent3, "Python", "share", "pyglossary")
if isdir(_dir):
return _dir
_dir = join(sys.prefix, "share", "pyglossary")
if isdir(_dir):
return _dir
if os.getenv("CONDA_PREFIX"):
_dir = join(os.getenv("CONDA_PREFIX"), "share", "pyglossary")
if isdir(_dir):
return _dir
raise OSError("failed to detect dataDir")
def windows_show_exception(*exc_info):
import ctypes
msg = format_exception(
exc_info=exc_info,
add_locals=(log.level <= logging.DEBUG),
add_globals=False,
)
log.critical(msg)
ctypes.windll.user32.MessageBoxW(0, msg, "PyGlossary Error", 0)
# __________________________________________________________________________ #
logging.setLoggerClass(MyLogger)
log = logging.getLogger("pyglossary")
if os.sep == "\\":
sys.excepthook = windows_show_exception
else:
sys.excepthook = lambda *exc_info: log.critical(
format_exception(
exc_info=exc_info,
add_locals=(log.level <= logging.DEBUG),
add_globals=False,
)
)
sysName = platform.system().lower()
# platform.system() is in ["Linux", "Windows", "Darwin", "FreeBSD"]
# sysName is in ["linux", "windows", "darwin', "freebsd"]
# can set env var WARNINGS to:
# "error", "ignore", "always", "default", "module", "once"
if os.getenv("WARNINGS"):
import warnings
warnings.filterwarnings(os.getenv("WARNINGS"))
if getattr(sys, "frozen", False):
# PyInstaller frozen executable
log.info(f"sys.frozen = {sys.frozen}")
rootDir = dirname(sys.executable)
uiDir = join(rootDir, "pyglossary", "ui")
else:
_srcDir = dirname(realpath(__file__))
uiDir = join(_srcDir, "ui")
rootDir = dirname(_srcDir)
dataDir = getDataDir()
appResDir = join(dataDir, "res")
if os.sep == "/": # Operating system is Unix-Like
homeDir = os.getenv("HOME")
user = os.getenv("USER")
tmpDir = os.getenv("TMPDIR", "/tmp")
if sysName == "darwin": # MacOS X
_libDir = join(homeDir, "Library")
confDir = join(_libDir, "Preferences", "PyGlossary")
# or maybe: join(_libDir, "PyGlossary")
# os.environ["OSTYPE"] == "darwin10.0"
# os.environ["MACHTYPE"] == "x86_64-apple-darwin10.0"
# platform.dist() == ("", "", "")
# platform.release() == "10.3.0"
cacheDir = join(_libDir, "Caches", "PyGlossary")
pip = "pip3"
else: # GNU/Linux, Termux, FreeBSD, etc
# should switch to "$XDG_CONFIG_HOME/pyglossary" in version 5.0.0
# which generally means ~/.config/pyglossary
confDir = join(homeDir, ".pyglossary")
cacheDir = join(homeDir, ".cache", "pyglossary")
if "/com.termux/" in homeDir:
pip = "pip3"
else:
pip = "sudo pip3"
elif os.sep == "\\": # Operating system is Windows
homeDir = join(os.getenv("HOMEDRIVE"), os.getenv("HOMEPATH"))
user = os.getenv("USERNAME")
tmpDir = os.getenv("TEMP")
_appData = os.getenv("APPDATA")
confDir = join(_appData, "PyGlossary")
_localAppData = os.getenv("LOCALAPPDATA")
if not _localAppData:
# Windows Vista or older
_localAppData = abspath(join(_appData, "..", "Local"))
cacheDir = join(_localAppData, "PyGlossary", "Cache")
pip = "pip3"
else:
raise RuntimeError(
f"Unknown path seperator(os.sep=={os.sep!r})"
f", unknown operating system!"
)
confJsonFile = join(confDir, "config.json")
rootConfJsonFile = join(dataDir, "config.json")
userPluginsDir = join(confDir, "plugins")