-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
124 lines (97 loc) · 3.1 KB
/
main.py
File metadata and controls
124 lines (97 loc) · 3.1 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
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
#!/usr/bin/env python3
"""
main.py
~~~~~~~
MailBot entrypoint — menu-driven interactive CLI.
Usage:
python main.py # interactive menu (default)
python main.py -c path.json # custom config path
python main.py --headless # run service without menu (requires config)
"""
from __future__ import annotations
import argparse
import logging
import signal
import sys
import time
from pathlib import Path
# Add project root to sys.path
PROJECT_ROOT = Path(__file__).resolve().parent
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
from utils.logger import setup_logging
logger = logging.getLogger("mailbot.main")
def default_config_path() -> Path:
"""Config path in the current working directory."""
return Path.cwd() / "config.json"
def parse_args() -> argparse.Namespace:
"""Parse CLI arguments."""
parser = argparse.ArgumentParser(
prog="MailBot",
description="IMAP-to-Telegram mail forwarder (interactive CLI)",
)
parser.add_argument(
"-c", "--config",
type=str,
default=str(default_config_path()),
help="Config file path (default: config.json in current directory)",
)
parser.add_argument(
"--headless",
action="store_true",
help="Run service without menu (requires existing config)",
)
return parser.parse_args()
def run_headless(config_path: Path) -> None:
"""Headless mode — start service, block until Ctrl+C."""
from core.manager import ServiceManager
from core.models import AppConfig
from utils.helpers import apply_global_proxy
if not config_path.exists():
print(f"Error: Config not found — {config_path}")
print("Run without --headless to use the setup wizard.")
sys.exit(1)
try:
config = AppConfig.load(config_path)
except Exception as exc:
print(f"Error: Invalid config — {exc}")
sys.exit(1)
apply_global_proxy(config.proxy)
setup_logging(level=config.log_level)
manager = ServiceManager(config)
manager.start()
logger.info("Headless mode — Ctrl+C to stop")
stop = False
def _sigint(sig, frame): # noqa: ANN001
nonlocal stop
stop = True
signal.signal(signal.SIGINT, _sigint)
try:
while not stop:
time.sleep(0.5)
finally:
manager.stop()
logger.info("Service stopped")
def run_interactive(config_path: Path) -> None:
"""Interactive menu mode."""
from interface.menu import main_menu
from core.models import AppConfig
from utils.helpers import apply_global_proxy
# Ensure proxy is applied before network ops (menus/tests)
try:
cfg = AppConfig.load(config_path)
except Exception:
cfg = AppConfig()
apply_global_proxy(cfg.proxy)
setup_logging(level="INFO")
main_menu(config_path)
def main() -> None:
"""Entry point."""
args = parse_args()
config_path = Path(args.config)
if args.headless:
run_headless(config_path)
else:
run_interactive(config_path)
if __name__ == "__main__":
main()