From e23019bff60ee0a269c3f71db4e1fae8ea4a916f Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Sun, 13 Jun 2021 20:41:10 -0700 Subject: [PATCH] Add command line entry point --- amanuensis/config.py | 23 +++++++++++++++++++++-- amanuensis/server/__init__.py | 8 +++++++- pyproject.toml | 3 +++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/amanuensis/config.py b/amanuensis/config.py index 08c5b7d..ed1747b 100644 --- a/amanuensis/config.py +++ b/amanuensis/config.py @@ -1,3 +1,4 @@ +from argparse import ArgumentParser from typing import Optional import os @@ -9,8 +10,8 @@ class AmanuensisConfig: # config values defined on the config object itself. CONFIG_FILE: Optional[str] = None STATIC_ROOT: Optional[str] = "static" - SECRET_KEY: Optional[str] = None - DATABASE_URI: Optional[str] = None + SECRET_KEY: Optional[str] = "secret" + DATABASE_URI: Optional[str] = "sqlite:///:memory:" TESTING: bool = False @@ -24,3 +25,21 @@ class EnvironmentConfig(AmanuensisConfig): "AMANUENSIS_DATABASE_URI", AmanuensisConfig.DATABASE_URI ) TESTING = os.environ.get("AMANUENSIS_TESTING", "").lower() in ("true", "1") + + +class CommandLineConfig(AmanuensisConfig): + """Loads config values from command line arguments.""" + def __init__(self) -> None: + parser = ArgumentParser() + parser.add_argument("--config-file", default=AmanuensisConfig.CONFIG_FILE) + parser.add_argument("--static-root", default=AmanuensisConfig.STATIC_ROOT) + parser.add_argument("--secret-key", default=AmanuensisConfig.SECRET_KEY) + parser.add_argument("--database-uri", default=AmanuensisConfig.DATABASE_URI) + parser.add_argument("--debug", action="store_true") + args = parser.parse_args() + + self.CONFIG_FILE = args.config_file + self.STATIC_ROOT = args.static_root + self.SECRET_KEY = args.secret_key + self.DATABASE_URI = args.database_uri + self.TESTING = args.debug diff --git a/amanuensis/server/__init__.py b/amanuensis/server/__init__.py index 4c7711c..9d95b54 100644 --- a/amanuensis/server/__init__.py +++ b/amanuensis/server/__init__.py @@ -2,7 +2,7 @@ import json from flask import Flask, g -from amanuensis.config import AmanuensisConfig +from amanuensis.config import AmanuensisConfig, CommandLineConfig from amanuensis.db import DbContext @@ -54,3 +54,9 @@ def get_app( app.route("/")(test) return app + + +def run(): + """Run the server, populating the config from the command line.""" + config = CommandLineConfig() + get_app(config).run(debug=config.TESTING) diff --git a/pyproject.toml b/pyproject.toml index 48e95d6..fd890c0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,6 +16,9 @@ pytest = "^5.2" black = "^21.5b2" mypy = "^0.812" +[tool.poetry.scripts] +amanuensis-server = "amanuensis.server:run" + [tool.black] extend-exclude = "^/amanuensis/cli/.*|^/amanuensis/config/.*|^/amanuensis/lexicon/.*|^/amanuensis/log/.*|^/amanuensis/models/.*|^/amanuensis/resources/.*|^/amanuensis/server/.*|^/amanuensis/user/.*|^/amanuensis/__main__.py"