From f7611bb79b37d60adaa28039d8e8e48787dba482 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Thu, 23 Apr 2020 20:02:44 -0700 Subject: [PATCH] CLean up server cli commands --- amanuensis/cli/server.py | 49 +++++++++++++++++++++------------- amanuensis/config/directory.py | 2 +- amanuensis/config/init.py | 18 ++++--------- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/amanuensis/cli/server.py b/amanuensis/cli/server.py index c9a0d30..5585bf8 100644 --- a/amanuensis/cli/server.py +++ b/amanuensis/cli/server.py @@ -1,13 +1,21 @@ +import logging import os +from amanuensis.config import RootConfigDirectoryContext from amanuensis.cli.helpers import ( - add_argument, no_argument, alias, - config_get, config_set, CONFIG_GET_ROOT_VALUE) + add_argument, + no_argument, + alias, + config_get, + config_set, + CONFIG_GET_ROOT_VALUE) + +logger = logging.getLogger(__name__) @alias('i') -@add_argument( - "--refresh", action="store_true", +@add_argument("--refresh", + action="store_true", help="Refresh an existing config directory") def command_init(args): """ @@ -29,6 +37,7 @@ def command_init(args): # Internal call create_config_dir(args.config_dir, args.refresh) + logger.info(f'Initialized config dir at {args.config_dir}') return 0 @@ -41,12 +50,10 @@ def command_generate_secret(args): The Flask server will not run unless a secret key has been generated. """ - # Module imports - from amanuensis.config import json_rw, logger - - secret_key = os.urandom(32) - with json_rw("config.json") as cfg: - cfg['secret_key'] = secret_key.hex() + root: RootConfigDirectoryContext = args.root + secret_key: bytes = os.urandom(32) + with root.config(edit=True) as cfg: + cfg.secret_key = secret_key.hex() logger.info("Regenerated Flask secret key") return 0 @@ -74,10 +81,17 @@ def command_run(args): @alias('n') -@add_argument("--get", metavar="PATHSPEC", dest="get", - nargs="?", const=CONFIG_GET_ROOT_VALUE, help="Get the value of a config key") -@add_argument("--set", metavar=("PATHSPEC", "VALUE"), dest="set", - nargs=2, help="Set the value of a config key") +@add_argument("--get", + metavar="PATHSPEC", + dest="get", + nargs="?", + const=CONFIG_GET_ROOT_VALUE, + help="Get the value of a config key") +@add_argument("--set", + metavar=("PATHSPEC", "VALUE"), + dest="set", + nargs=2, + help="Set the value of a config key") def command_config(args): """ Interact with the global config @@ -85,19 +99,18 @@ def command_config(args): PATHSPEC is a path into the config object formatted as a dot-separated sequence of keys. """ - # Module imports - from amanuensis.config import json_ro, json_rw, logger + root: RootConfigDirectoryContext = args.root if args.get and args.set: logger.error("Specify one of --get and --set") return -1 if args.get: - with json_ro('config.json') as cfg: + with root.config(edit=False) as cfg: config_get(cfg, args.get) if args.set: - with json_rw('config.json') as cfg: + with root.config(edit=True) as cfg: config_set("config", cfg, args.set) return 0 diff --git a/amanuensis/config/directory.py b/amanuensis/config/directory.py index 37d4d66..0ea783e 100644 --- a/amanuensis/config/directory.py +++ b/amanuensis/config/directory.py @@ -123,7 +123,7 @@ class IndexDirectoryContext(ConfigDirectoryContext): return self.read('index') -class RootConfigDirectoryContext(ConfigDirectoryContext): +class RootConfigDirectoryContext(ConfigFileConfigDirectoryContext): """ Context for the config directory with links to the lexicon and user contexts. diff --git a/amanuensis/config/init.py b/amanuensis/config/init.py index 8189e98..dfa1e16 100644 --- a/amanuensis/config/init.py +++ b/amanuensis/config/init.py @@ -1,13 +1,13 @@ # Standard library imports -import copy +from collections import OrderedDict +import fcntl import json -import logging.config import os import shutil # Module imports -from amanuensis.errors import MissingConfigError, MalformedConfigError -from amanuensis.config.loader import json_ro, json_rw +# from amanuensis.errors import MissingConfigError, MalformedConfigError +from amanuensis.config.context import json_ro, json_rw from amanuensis.resources import get_stream @@ -15,8 +15,6 @@ def create_config_dir(config_dir, refresh=False): """ Create or refresh a config directory """ - from collections import OrderedDict - import fcntl def prepend(*path): joined = os.path.join(*path) @@ -38,7 +36,7 @@ def create_config_dir(config_dir, refresh=False): global_config_path = prepend("config.json") if refresh and os.path.isfile(global_config_path): # We need to write an entirely different ordereddict to the config - # file, so we mimic the config.loader functionality manually. + # file, so we mimic the config.context functionality manually. with open(global_config_path, 'r+', encoding='utf8') as cfg_file: fcntl.lockf(cfg_file, fcntl.LOCK_EX) old_cfg = json.load(cfg_file, object_pairs_hook=OrderedDict) @@ -60,11 +58,6 @@ def create_config_dir(config_dir, refresh=False): with open(prepend("config.json"), 'wb') as f: f.write(def_cfg.read()) - # Ensure pidfile exists. - if not os.path.isfile(prepend("pid")): - with open(prepend("pid"), 'w') as f: - f.write(str(os.getpid())) - # Ensure lexicon subdir exists. if not os.path.isdir(prepend("lexicon")): os.mkdir(prepend("lexicon")) @@ -101,4 +94,3 @@ def create_config_dir(config_dir, refresh=False): print("Removing stale {} index entry '{}: {}'" .format(dir_name, name, entry)) del index[name] -