From f0cc11e3f13e51518fec0ba6857a8fcba03b8e88 Mon Sep 17 00:00:00 2001 From: Erik Skultety Date: Fri, 10 Mar 2023 13:57:03 +0100 Subject: lcitool: Introduce a __main__ module The thing with transitioning from setup.py to pyproject.toml build strategy is that we now must specify a list of all entry points for the project, IOW whatever is going to become the CLI executable. With setup.py we got away by having a bin/lcitool script which would get installed directly, but that's not how setuptools builds work after PEP 517 [1]. The build backend now creates the executables automatically as wrappers over the entry points that are required to be listed in the project's build config. There's one more requirement - the entry point needs to be importable through Python's standard means. Since bin/lcitool is not part of the lcitool package, hence not easily importable as a module, we need to have its logic elsewhere. One of the standard ways of doing that is by creating a __main__.py module which python also automatically looks for when instructed to run the project directly from its repo. [1] https://peps.python.org/pep-0517/ Signed-off-by: Erik Skultety --- lcitool/__main__.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 lcitool/__main__.py diff --git a/lcitool/__main__.py b/lcitool/__main__.py new file mode 100644 index 0000000..a045e81 --- /dev/null +++ b/lcitool/__main__.py @@ -0,0 +1,41 @@ +import sys +import logging + +from lcitool.application import Application +from lcitool.commandline import CommandLine +from lcitool.logger import LevelFormatter + + +class LcitoolLogger(logging.Logger): + def debug(self, *args, **kwargs): + super().debug(*args, **kwargs, exc_info=True) + + +def main(): + log_level_formats = { + logging.DEBUG: "[%(levelname)s] %(module)s:%(funcName)s:%(lineno)d: %(message)s", + logging.ERROR: "[%(levelname)s]: %(message)s", + } + + logging.setLoggerClass(LcitoolLogger) + + custom_formatter = LevelFormatter(log_level_formats) + custom_handler = logging.StreamHandler(stream=sys.stderr) + custom_handler.setFormatter(custom_formatter) + + log = logging.getLogger() + log.addHandler(custom_handler) + + args = CommandLine().parse() + + if args.debug: + log.setLevel(logging.DEBUG) + + try: + Application().run(args) + except Exception: + log.exception("An unexpected error occurred") + sys.exit(1) + + +main() -- cgit v1.1