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 <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2022-02-09 07:25:27 -05:00
parent 6efcffa028
commit 737cf8a2cb
1 changed files with 24 additions and 25 deletions

View File

@ -6,8 +6,16 @@
# This file may be distributed under the terms of the GNU GPLv3 license # This file may be distributed under the terms of the GNU GPLv3 license
from __future__ import annotations from __future__ import annotations
import argparse
import sys 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 importlib
import os import os
import io import io
@ -858,21 +866,7 @@ class BaseRequest:
return {'id': self.id, 'method': self.rpc_method, return {'id': self.id, 'method': self.rpc_method,
'params': self.params} 'params': self.params}
def main() -> None: def main(cmd_line_args: argparse.Namespace) -> None:
# Parse start arguments
parser = argparse.ArgumentParser(
description="Moonraker - Klipper API Server")
parser.add_argument(
"-c", "--configfile", default="~/moonraker.conf",
metavar='<configfile>',
help="Location of moonraker configuration file")
parser.add_argument(
"-l", "--logfile", default="/tmp/moonraker.log", metavar='<logfile>',
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()
cfg_file = cmd_line_args.configfile cfg_file = cmd_line_args.configfile
app_args = {'config_file': cfg_file} app_args = {'config_file': cfg_file}
@ -888,14 +882,6 @@ def main() -> None:
if warning is not None: if warning is not None:
app_args['log_warning'] = warning 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 # Start asyncio event loop and server
event_loop = EventLoop() event_loop = EventLoop()
alt_config_loaded = False alt_config_loaded = False
@ -964,4 +950,17 @@ def main() -> None:
if __name__ == '__main__': if __name__ == '__main__':
main() # Parse start arguments
parser = argparse.ArgumentParser(
description="Moonraker - Klipper API Server")
parser.add_argument(
"-c", "--configfile", default="~/moonraker.conf",
metavar='<configfile>',
help="Location of moonraker configuration file")
parser.add_argument(
"-l", "--logfile", default="/tmp/moonraker.log", metavar='<logfile>',
help="log file name and location")
parser.add_argument(
"-n", "--nologfile", action='store_true',
help="disable logging to a file")
main(parser.parse_args())