From 737cf8a2cb423c996b59681e7c5d0c6ead8712ea Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Wed, 9 Feb 2022 07:25:27 -0500 Subject: [PATCH] mooraker: refactor main entry point Move the version check to the top of the module. An older version of python is unlikely to make it beyond the initial import statements, so print a message to stdout and stderr. Move argument parsing out of the main method. This makes it possible to invoke main without parsing command line arguments, which is useful for tests. Signed-off-by: Eric Callahan --- moonraker/moonraker.py | 49 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/moonraker/moonraker.py b/moonraker/moonraker.py index 44e2dfc..1ee6620 100755 --- a/moonraker/moonraker.py +++ b/moonraker/moonraker.py @@ -6,8 +6,16 @@ # This file may be distributed under the terms of the GNU GPLv3 license from __future__ import annotations -import argparse import sys +if sys.version_info < (3, 7): + msg = ( + "Moonraker requires Python 3.7 or above. " + "Detected Version: %s\n" + ) + sys.stdout.write(msg % (sys.version,)) + sys.stderr.write(msg % (sys.version,)) + exit(1) +import argparse import importlib import os import io @@ -858,21 +866,7 @@ class BaseRequest: return {'id': self.id, 'method': self.rpc_method, 'params': self.params} -def main() -> None: - # Parse start arguments - parser = argparse.ArgumentParser( - description="Moonraker - Klipper API Server") - parser.add_argument( - "-c", "--configfile", default="~/moonraker.conf", - metavar='', - help="Location of moonraker configuration file") - parser.add_argument( - "-l", "--logfile", default="/tmp/moonraker.log", metavar='', - help="log file name and location") - parser.add_argument( - "-n", "--nologfile", action='store_true', - help="disable logging to a file") - cmd_line_args = parser.parse_args() +def main(cmd_line_args: argparse.Namespace) -> None: cfg_file = cmd_line_args.configfile app_args = {'config_file': cfg_file} @@ -888,14 +882,6 @@ def main() -> None: if warning is not None: app_args['log_warning'] = warning - if sys.version_info < (3, 7): - msg = f"Moonraker requires Python 3.7 or above. " \ - f"Detected Version: {sys.version}" - logging.info(msg) - print(msg) - ql.stop() - exit(1) - # Start asyncio event loop and server event_loop = EventLoop() alt_config_loaded = False @@ -964,4 +950,17 @@ def main() -> None: if __name__ == '__main__': - main() + # Parse start arguments + parser = argparse.ArgumentParser( + description="Moonraker - Klipper API Server") + parser.add_argument( + "-c", "--configfile", default="~/moonraker.conf", + metavar='', + help="Location of moonraker configuration file") + parser.add_argument( + "-l", "--logfile", default="/tmp/moonraker.log", metavar='', + help="log file name and location") + parser.add_argument( + "-n", "--nologfile", action='store_true', + help="disable logging to a file") + main(parser.parse_args())