From 25b61a612c4e3b753778c2c0b2e07edba0330d66 Mon Sep 17 00:00:00 2001 From: Ethan Paul <24588726+enpaul@users.noreply.github.com> Date: Fri, 12 Nov 2021 23:34:47 -0500 Subject: [PATCH] Add trivial CLI for maintenance tasks --- kodak/cli.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 3 ++ 2 files changed, 78 insertions(+) create mode 100644 kodak/cli.py diff --git a/kodak/cli.py b/kodak/cli.py new file mode 100644 index 0000000..00f6d5f --- /dev/null +++ b/kodak/cli.py @@ -0,0 +1,75 @@ +import argparse +import logging +import sys + +from kodak import __about__ +from kodak import configuration +from kodak import database +from kodak import tools + + +def get_args() -> argparse.Namespace: + """Setup the flags for the basic CLI""" + + parser = argparse.ArgumentParser( + prog=__about__.__name__, description=__about__.__summary__ + ) + + parser.add_argument( + "--server", action="store_true", help="Run the Flask development server" + ) + parser.add_argument( + "--config", action="store_true", help="Check that the configuration is valid" + ) + parser.add_argument( + "--version", action="store_true", help="Show program version and exit" + ) + parser.add_argument( + "--index", action="store_true", help="Rebuild the source image index" + ) + + return parser.parse_args() + + +def main() -> int: + """Main entrypoint for the CLI tooling""" + args = get_args() + + logging.basicConfig( + stream=sys.stderr, + format="(%(asctime)s) %(levelname)s: %(message)s", + datefmt="%I:%M:%S", + level=logging.DEBUG, + ) + + if args.version: + print(f"{__about__.__title__} {__about__.__version__}", file=sys.stderr) + return 0 + + if args.config: + try: + configuration.load() + except Exception as err: # pylint: disable=broad-except + print(f"Configuration check failed: {err}", file=sys.stderr) + return 1 + else: + print("Configuration check successful!", file=sys.stderr) + return 0 + + if args.index: + config = configuration.load() + database.initialize(config) + tools.index.build(config) + return 0 + + if args.server: + from kodak import application # pylint: disable=import-outside-toplevel + + application.APPLICATION.run(host="127.0.0.1") + return 0 + + return 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/pyproject.toml b/pyproject.toml index 6b51d07..ad5de56 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,6 +36,9 @@ classifiers = [ "Topic :: Multimedia :: Graphics :: Graphics Conversion" ] +[tool.poetry.scripts] +kodak = "kodak.cli:main" + [tool.poetry.dependencies] python = "^3.7" flask = "^1.1.2"