Files
mtga-assistant/src/mtga_assistant/app.py
T
Aeth3randClaude Opus 5.5 c4533d12c0 Run in the background and open when MTGA starts
- Closing the window hides it to the system tray; tracking and history
  recording continue. Quit from the tray, the menu or Ctrl+Q.
- Show the window when MTGA starts a new Player.log, detected with OS
  file notifications on the log folder (QFileSystemWatcher), not polling.
- Single instance over a local socket: launching again shows the
  running window (or stays hidden with --background).
- Menu options for background mode, show-on-launch and start at login
  (XDG autostart on Linux, HKCU Run key on Windows); --background flag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 09:54:08 -04:00

81 lines
3.1 KiB
Python

"""Entry point: mtga-assistant [--background] [--log PATH] [--replay PATH [--rate N]]."""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
from PySide6.QtGui import QIcon
from PySide6.QtWidgets import QApplication
from . import paths
from .background import SingleInstance
from .carddb import open_card_db
from .history import HistoryStore
from .images import ImageCache
from .ui import theme
from .ui.main_window import MainWindow
def main() -> int:
ap = argparse.ArgumentParser(description="MTG Arena deck and game tracker")
ap.add_argument("--log", type=Path, help="Player.log to follow (default: auto-detect)")
ap.add_argument("--replay", type=Path, help="replay a saved log instead of following the live one")
ap.add_argument("--rate", type=float, default=40.0, help="replay speed in log events per second")
ap.add_argument("--mtga-dir", type=Path, help="MTGA install directory (default: auto-detect)")
ap.add_argument("--background", action="store_true",
help="start hidden (e.g. at login); the window opens when MTGA starts")
args, qt_args = ap.parse_known_args()
cfg = paths.load_config()
if args.log:
cfg.log_path = args.log
if args.mtga_dir:
cfg.mtga_dir = args.mtga_dir
if sys.platform == "win32":
# Own taskbar identity (icon and grouping) instead of python.exe's.
import ctypes
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("mtga-assistant")
app = QApplication([sys.argv[0], *qt_args])
app.setQuitOnLastWindowClosed(False) # closing the window keeps recording in the background
# One instance per user: a second launch just asks the running one to show its window.
instance = None
if not args.replay:
instance = SingleInstance()
if instance.send("background" if args.background else "show"):
return 0
instance.listen()
app.setWindowIcon(QIcon(str(Path(__file__).with_name("icon.png"))))
app.setApplicationName("MTGA Assistant")
app.setDesktopFileName("mtga-assistant") # Wayland app_id, usable in Hyprland window rules
theme.apply(app)
mtga_dir = paths.find_mtga_dir(cfg)
db_path = paths.find_card_db(mtga_dir) if mtga_dir else None
cards = open_card_db(db_path, paths.CACHE_DIR / "carddb")
images = ImageCache(paths.CACHE_DIR, cards, cfg.cache_max_mb * 1024 * 1024)
history = HistoryStore(paths.DATA_DIR / "history.db", cfg.history_max_mb)
log_path = paths.find_log_path(cfg)
if args.replay:
win = MainWindow(cards, images, history, args.replay, replay_rate=args.rate)
else:
win = MainWindow(cards, images, history, log_path)
if log_path is not None:
# Matches from the previous MTGA session are only in Player-prev.log.
win.import_history(log_path.with_name("Player-prev.log"))
app.aboutToQuit.connect(win.shutdown)
if instance is not None:
instance.message.connect(lambda msg: win.bring_to_front() if msg == "show" else None)
if not args.background:
win.show()
return app.exec()
if __name__ == "__main__":
sys.exit(main())