CLean up server cli commands

This commit is contained in:
Tim Van Baak 2020-04-23 20:02:44 -07:00
parent 850d07b171
commit f7611bb79b
3 changed files with 37 additions and 32 deletions

View File

@ -1,13 +1,21 @@
import logging
import os import os
from amanuensis.config import RootConfigDirectoryContext
from amanuensis.cli.helpers import ( from amanuensis.cli.helpers import (
add_argument, no_argument, alias, add_argument,
config_get, config_set, CONFIG_GET_ROOT_VALUE) no_argument,
alias,
config_get,
config_set,
CONFIG_GET_ROOT_VALUE)
logger = logging.getLogger(__name__)
@alias('i') @alias('i')
@add_argument( @add_argument("--refresh",
"--refresh", action="store_true", action="store_true",
help="Refresh an existing config directory") help="Refresh an existing config directory")
def command_init(args): def command_init(args):
""" """
@ -29,6 +37,7 @@ def command_init(args):
# Internal call # Internal call
create_config_dir(args.config_dir, args.refresh) create_config_dir(args.config_dir, args.refresh)
logger.info(f'Initialized config dir at {args.config_dir}')
return 0 return 0
@ -41,12 +50,10 @@ def command_generate_secret(args):
The Flask server will not run unless a secret key has The Flask server will not run unless a secret key has
been generated. been generated.
""" """
# Module imports root: RootConfigDirectoryContext = args.root
from amanuensis.config import json_rw, logger secret_key: bytes = os.urandom(32)
with root.config(edit=True) as cfg:
secret_key = os.urandom(32) cfg.secret_key = secret_key.hex()
with json_rw("config.json") as cfg:
cfg['secret_key'] = secret_key.hex()
logger.info("Regenerated Flask secret key") logger.info("Regenerated Flask secret key")
return 0 return 0
@ -74,10 +81,17 @@ def command_run(args):
@alias('n') @alias('n')
@add_argument("--get", metavar="PATHSPEC", dest="get", @add_argument("--get",
nargs="?", const=CONFIG_GET_ROOT_VALUE, help="Get the value of a config key") metavar="PATHSPEC",
@add_argument("--set", metavar=("PATHSPEC", "VALUE"), dest="set", dest="get",
nargs=2, help="Set the value of a config key") 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): def command_config(args):
""" """
Interact with the global config Interact with the global config
@ -85,19 +99,18 @@ def command_config(args):
PATHSPEC is a path into the config object formatted as PATHSPEC is a path into the config object formatted as
a dot-separated sequence of keys. a dot-separated sequence of keys.
""" """
# Module imports root: RootConfigDirectoryContext = args.root
from amanuensis.config import json_ro, json_rw, logger
if args.get and args.set: if args.get and args.set:
logger.error("Specify one of --get and --set") logger.error("Specify one of --get and --set")
return -1 return -1
if args.get: if args.get:
with json_ro('config.json') as cfg: with root.config(edit=False) as cfg:
config_get(cfg, args.get) config_get(cfg, args.get)
if args.set: if args.set:
with json_rw('config.json') as cfg: with root.config(edit=True) as cfg:
config_set("config", cfg, args.set) config_set("config", cfg, args.set)
return 0 return 0

View File

@ -123,7 +123,7 @@ class IndexDirectoryContext(ConfigDirectoryContext):
return self.read('index') return self.read('index')
class RootConfigDirectoryContext(ConfigDirectoryContext): class RootConfigDirectoryContext(ConfigFileConfigDirectoryContext):
""" """
Context for the config directory with links to the lexicon and Context for the config directory with links to the lexicon and
user contexts. user contexts.

View File

@ -1,13 +1,13 @@
# Standard library imports # Standard library imports
import copy from collections import OrderedDict
import fcntl
import json import json
import logging.config
import os import os
import shutil import shutil
# Module imports # Module imports
from amanuensis.errors import MissingConfigError, MalformedConfigError # from amanuensis.errors import MissingConfigError, MalformedConfigError
from amanuensis.config.loader import json_ro, json_rw from amanuensis.config.context import json_ro, json_rw
from amanuensis.resources import get_stream from amanuensis.resources import get_stream
@ -15,8 +15,6 @@ def create_config_dir(config_dir, refresh=False):
""" """
Create or refresh a config directory Create or refresh a config directory
""" """
from collections import OrderedDict
import fcntl
def prepend(*path): def prepend(*path):
joined = os.path.join(*path) joined = os.path.join(*path)
@ -38,7 +36,7 @@ def create_config_dir(config_dir, refresh=False):
global_config_path = prepend("config.json") global_config_path = prepend("config.json")
if refresh and os.path.isfile(global_config_path): if refresh and os.path.isfile(global_config_path):
# We need to write an entirely different ordereddict to the config # 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: with open(global_config_path, 'r+', encoding='utf8') as cfg_file:
fcntl.lockf(cfg_file, fcntl.LOCK_EX) fcntl.lockf(cfg_file, fcntl.LOCK_EX)
old_cfg = json.load(cfg_file, object_pairs_hook=OrderedDict) 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: with open(prepend("config.json"), 'wb') as f:
f.write(def_cfg.read()) 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. # Ensure lexicon subdir exists.
if not os.path.isdir(prepend("lexicon")): if not os.path.isdir(prepend("lexicon")):
os.mkdir(prepend("lexicon")) os.mkdir(prepend("lexicon"))
@ -101,4 +94,3 @@ def create_config_dir(config_dir, refresh=False):
print("Removing stale {} index entry '{}: {}'" print("Removing stale {} index entry '{}: {}'"
.format(dir_name, name, entry)) .format(dir_name, name, entry))
del index[name] del index[name]