-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLaunch.pyw
More file actions
84 lines (65 loc) · 2.11 KB
/
Launch.pyw
File metadata and controls
84 lines (65 loc) · 2.11 KB
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
"""
Launch.pyw — Console-free toolbox launcher.
Double-click this file to start the toolbox without a CMD window.
Uses pythonw.exe automatically (.pyw extension).
"""
from __future__ import annotations
import logging
import os
import sys
from logging.handlers import RotatingFileHandler
from pathlib import Path
def _bootstrap_paths() -> Path:
"""Set cwd to this file's dir and insert project + venv site-packages on path.
Returns:
The project root directory.
"""
project_root = Path(__file__).resolve().parent
os.chdir(project_root)
project_root_str = str(project_root)
if project_root_str not in sys.path:
sys.path.insert(0, project_root_str)
venv_site = project_root / "venv" / "Lib" / "site-packages"
if venv_site.is_dir():
sys.path.insert(0, str(venv_site))
return project_root
def _configure_logging(project_root: Path) -> None:
"""Install a RotatingFileHandler on the root logger.
Captures warnings/errors from unconfigured libraries (and anything
written via ``logging``) into ``toolbox_crash.log`` in the project
root. Caps file growth at 2MB with 3 backups.
"""
log_path = project_root / "toolbox_crash.log"
handler = RotatingFileHandler(
str(log_path),
maxBytes=2_000_000,
backupCount=3,
encoding="utf-8",
)
handler.setFormatter(
logging.Formatter(
"%(asctime)s %(levelname)s %(name)s: %(message)s",
"%Y-%m-%d %H:%M:%S",
)
)
logging.basicConfig(level=logging.INFO, handlers=[handler])
def _main() -> int:
project_root = _bootstrap_paths()
_configure_logging(project_root)
log = logging.getLogger("launcher.bootstrap")
try:
import customtkinter as ctk
ctk.set_appearance_mode("Dark")
ctk.set_default_color_theme("blue")
from Main import ToolboxApp
app = ToolboxApp()
app.mainloop()
return 0
except Exception:
log.exception("launcher crashed")
return 1
if __name__ == "__main__":
try:
sys.exit(_main())
finally:
logging.shutdown()