|
2 | 2 | import http.client as http_client |
3 | 3 | import json |
4 | 4 | import logging |
| 5 | +import platform |
| 6 | +import signal |
5 | 7 | import sys |
6 | 8 | from os import path |
7 | | -from typing import NoReturn |
| 9 | +from typing import Any, NoReturn |
8 | 10 |
|
9 | 11 | import colorlog |
10 | 12 | import requests |
|
28 | 30 | # set to 1 to debug http headers |
29 | 31 | http_client.HTTPConnection.debuglevel = 0 |
30 | 32 |
|
| 33 | +SYS_PLATFORM = platform.system() |
| 34 | +IS_WINDOWS = SYS_PLATFORM.lower() in ('windows', 'cygwin') |
| 35 | + |
31 | 36 |
|
32 | 37 | def main() -> NoReturn: |
33 | 38 | """Wrapper for Scanner and Helper functions.""" |
| 39 | + _register_signals() |
34 | 40 | parser = argparse.ArgumentParser( |
35 | 41 | description="TooGoodToGo scanner and notifier.", |
36 | 42 | prog="scanner" |
@@ -245,6 +251,24 @@ def _print_welcome_message() -> None: |
245 | 251 | log.info("") |
246 | 252 |
|
247 | 253 |
|
| 254 | +def _register_signals() -> None: |
| 255 | + # TODO: Define SIGUSR1, SIGUSR2 |
| 256 | + signal.signal(signal.SIGINT, _handle_exit_signal) |
| 257 | + signal.signal(signal.SIGTERM, _handle_exit_signal) |
| 258 | + if hasattr(signal, "SIGBREAK"): |
| 259 | + signal.signal(getattr(signal, "SIGBREAK"), _handle_exit_signal) |
| 260 | + if not IS_WINDOWS: |
| 261 | + signal.signal(signal.SIGHUP, _handle_exit_signal) |
| 262 | + # TODO: SIGQUIT is ideally meant to terminate with core dumps |
| 263 | + signal.signal(signal.SIGQUIT, _handle_exit_signal) |
| 264 | + |
| 265 | + |
| 266 | +def _handle_exit_signal(signum: int, _frame: Any) -> None: |
| 267 | + log = logging.getLogger("tgtg") |
| 268 | + log.debug('Received signal %d' % signum) |
| 269 | + raise KeyboardInterrupt |
| 270 | + |
| 271 | + |
248 | 272 | def query_yes_no(question, default="yes") -> bool: |
249 | 273 | """Ask a yes/no question via raw_input() and return their answer. |
250 | 274 |
|
|
0 commit comments