|
| 1 | +""" |
| 2 | +Entry point for the GEECS Scanner GUI. |
| 3 | +
|
| 4 | +Run from a terminal (e.g., ``python main.py``). Optional CLI flags allow you |
| 5 | +to tweak logging behavior without editing code. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import argparse |
| 11 | +import logging |
1 | 12 | import sys |
| 13 | +from pathlib import Path |
| 14 | + |
2 | 15 | from PyQt5.QtWidgets import QApplication |
3 | 16 | from PyQt5.QtGui import QIcon |
| 17 | + |
4 | 18 | from geecs_scanner.utils import exception_hook |
5 | | -from geecs_scanner.app import GEECSScannerWindow |
6 | | -from geecs_scanner.app.gui import resources_rc |
| 19 | +from geecs_scanner.logging_setup import ensure_logging |
7 | 20 |
|
8 | 21 |
|
9 | | -if __name__ == '__main__': |
10 | | - """Launches the GEECS Scanner GUI""" |
11 | | - sys.excepthook = exception_hook |
12 | | - app = QApplication(sys.argv) |
| 22 | +def _parse_args() -> argparse.Namespace: |
| 23 | + """ |
| 24 | + Parse command-line arguments. |
| 25 | +
|
| 26 | + Returns |
| 27 | + ------- |
| 28 | + argparse.Namespace |
| 29 | + Parsed arguments including logging controls. |
| 30 | + """ |
| 31 | + p = argparse.ArgumentParser(description="Launch the GEECS Scanner GUI") |
| 32 | + p.add_argument( |
| 33 | + "--log-level", |
| 34 | + default="INFO", |
| 35 | + choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], |
| 36 | + help="Console log level (file log still rotates & captures all levels per config).", |
| 37 | + ) |
| 38 | + p.add_argument( |
| 39 | + "--log-dir", |
| 40 | + type=Path, |
| 41 | + default=None, |
| 42 | + help="Directory for global logs (overrides $GEECS_LOG_DIR).", |
| 43 | + ) |
| 44 | + p.add_argument( |
| 45 | + "--no-console", |
| 46 | + action="store_true", |
| 47 | + help="Disable console logging (file logging remains enabled).", |
| 48 | + ) |
| 49 | + p.add_argument( |
| 50 | + "--max-bytes", |
| 51 | + type=int, |
| 52 | + default=10_000_000, |
| 53 | + help="Max bytes before rotating the global log file.", |
| 54 | + ) |
| 55 | + p.add_argument( |
| 56 | + "--backup-count", |
| 57 | + type=int, |
| 58 | + default=5, |
| 59 | + help="Number of rotated log files to keep.", |
| 60 | + ) |
| 61 | + return p.parse_args() |
| 62 | + |
| 63 | + |
| 64 | +def _wrap_excepthook(): |
| 65 | + """ |
| 66 | + Route uncaught exceptions to logging, then delegate to the existing hook. |
| 67 | +
|
| 68 | + Notes |
| 69 | + ----- |
| 70 | + This preserves your current behavior while ensuring crashes are recorded |
| 71 | + in the central log. |
| 72 | + """ |
| 73 | + |
| 74 | + def _hook(exc_type, exc, tb): |
| 75 | + logging.getLogger("geecs_scanner").exception( |
| 76 | + "Uncaught exception", exc_info=(exc_type, exc, tb) |
| 77 | + ) |
| 78 | + exception_hook(exc_type, exc, tb) |
13 | 79 |
|
| 80 | + sys.excepthook = _hook |
| 81 | + |
| 82 | + |
| 83 | +def main() -> int: |
| 84 | + """ |
| 85 | + Launch the GEECS Scanner GUI. |
| 86 | +
|
| 87 | + This function initializes centralized logging, installs an exception hook, |
| 88 | + and starts the Qt application loop. |
| 89 | +
|
| 90 | + Returns |
| 91 | + ------- |
| 92 | + int |
| 93 | + Qt application exit code. |
| 94 | + """ |
| 95 | + args = _parse_args() |
| 96 | + |
| 97 | + # Initialize logging early |
| 98 | + ensure_logging( |
| 99 | + log_dir=str(args.log_dir) if args.log_dir else None, |
| 100 | + level=getattr(logging, args.log_level), |
| 101 | + console=not args.no_console, |
| 102 | + max_bytes=args.max_bytes, |
| 103 | + backup_count=args.backup_count, |
| 104 | + ) |
| 105 | + |
| 106 | + _wrap_excepthook() |
| 107 | + |
| 108 | + app = QApplication(sys.argv) |
14 | 109 | app.setWindowIcon(QIcon(":/application_icon.ico")) |
15 | 110 |
|
| 111 | + # Import after logging is ready |
| 112 | + from geecs_scanner.app import GEECSScannerWindow |
| 113 | + |
16 | 114 | application = GEECSScannerWindow() |
17 | 115 | application.show() |
18 | 116 | application.raise_() |
19 | 117 | application.activateWindow() |
20 | 118 |
|
21 | | - sys.exit(app.exec_()) |
| 119 | + return app.exec_() |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + raise SystemExit(main()) |
0 commit comments