winziprint.py: Extract daemon() function from main()

This commit is contained in:
2024-03-05 12:33:30 +01:00
parent 75689983df
commit f2f3e10e2a

View File

@ -16,7 +16,7 @@ import weasyprint
import pypdf
VERSION = __version__ = '0.2.3'
VERSION = __version__ = '0.2.4'
SOCKET_ADDRESS = ('127.0.0.1', 30983)
BATCH_SIZE = 10
@ -166,6 +166,21 @@ def _wrapper_convert(args: list[str],
gc.collect()
def daemon() -> None:
# a tcp server is used due to the lack of unix sockets on Windows
with socketserver.ThreadingTCPServer(SOCKET_ADDRESS, ConnectionHandler) as server:
def exit_gracefully(_signum: int, _frame) -> None:
raise KeyboardInterrupt()
signal.signal(signal.SIGINT, exit_gracefully)
signal.signal(signal.SIGTERM, exit_gracefully)
print('Running as daemon')
try:
server.serve_forever()
except KeyboardInterrupt:
print('', file=sys.stderr)
print('Shutting down')
def usage(error: bool = False) -> None:
print(f'usage: {sys.argv[0]} [-h] [-v] [-d DIR] [ -D | [-p] [-2] [-e ENCODING] [ - | INPUT [INPUT...] OUTPUT ] ]\n'
'\n'
@ -233,20 +248,9 @@ def main() -> None:
os.chdir(working_dir)
if '-D' in args:
print('Running as daemon')
if len(args) != 1:
usage(True)
# a tcp server is used due to the lack of unix sockets on Windows
with socketserver.ThreadingTCPServer(SOCKET_ADDRESS, ConnectionHandler) as server:
def exit_gracefully(_signum: int, _frame) -> None:
raise KeyboardInterrupt()
signal.signal(signal.SIGINT, exit_gracefully)
signal.signal(signal.SIGTERM, exit_gracefully)
try:
server.serve_forever()
except KeyboardInterrupt:
print('', file=sys.stderr)
print('Shutting down')
daemon()
return
encoding = _get_arg(args, '-e', '--encoding')