-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
69 lines (53 loc) · 2.13 KB
/
Copy pathmain.py
File metadata and controls
69 lines (53 loc) · 2.13 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
"""
KeyMagic — entry point.
Run with: python main.py
Or build a windowless executable with:
pyinstaller --onefile --noconsole --name KeyMagic main.py
"""
from __future__ import annotations
import logging
import os
import sys
from core import single_instance
from core.app import KeyMagicApp
from core.config import UI
from core.elevation import ensure_admin
def _configure_logging() -> None:
log_dir = os.path.join(os.environ.get("LOCALAPPDATA", "."), UI.APP_NAME)
os.makedirs(log_dir, exist_ok=True)
log_path = os.path.join(log_dir, UI.LOG_FILENAME)
handlers: list[logging.Handler] = [logging.FileHandler(log_path, encoding="utf-8")]
# Only attach a console handler if we actually have a console (i.e. not
# a --noconsole PyInstaller build, where sys.stdout may be None).
if sys.stdout is not None:
handlers.append(logging.StreamHandler(sys.stdout))
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
handlers=handlers,
)
def main() -> None:
# Must happen before anything else: if we're not elevated yet, this
# relaunches the process with a UAC prompt and exits the current one,
# so there is no point doing any other setup first.
ensure_admin()
_configure_logging()
logger = logging.getLogger(__name__)
# If KeyMagic is already running (e.g. the logon task started it and the
# user then clicked the shortcut), don't start a second tray whose
# hotkeys would silently fail to register — hand off to the running one
# and exit. Must come after logging is configured so the hand-off is
# recorded, and after elevation so the mutex name matches across launches.
if not single_instance.acquire():
single_instance.signal_existing_instance()
logger.info("Exiting: handed off to the already-running instance.")
return
logger.info("Starting %s (elevated)...", UI.APP_NAME)
app = KeyMagicApp()
single_instance.start_listener(app.open_panel)
try:
app.run()
except KeyboardInterrupt:
app.shutdown()
if __name__ == "__main__":
main()