Skip to content

Commit 2e2e1de

Browse files
authored
Merge pull request #275 from Der-Henning/signal_handling
Signal handling
2 parents 6577eb4 + c72eac2 commit 2e2e1de

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/main.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
import http.client as http_client
33
import json
44
import logging
5+
import platform
6+
import signal
57
import sys
68
from os import path
7-
from typing import NoReturn
9+
from typing import Any, NoReturn
810

911
import colorlog
1012
import requests
@@ -28,9 +30,13 @@
2830
# set to 1 to debug http headers
2931
http_client.HTTPConnection.debuglevel = 0
3032

33+
SYS_PLATFORM = platform.system()
34+
IS_WINDOWS = SYS_PLATFORM.lower() in ('windows', 'cygwin')
35+
3136

3237
def main() -> NoReturn:
3338
"""Wrapper for Scanner and Helper functions."""
39+
_register_signals()
3440
parser = argparse.ArgumentParser(
3541
description="TooGoodToGo scanner and notifier.",
3642
prog="scanner"
@@ -245,6 +251,24 @@ def _print_welcome_message() -> None:
245251
log.info("")
246252

247253

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+
248272
def query_yes_no(question, default="yes") -> bool:
249273
"""Ask a yes/no question via raw_input() and return their answer.
250274

0 commit comments

Comments
 (0)