From bf27d2bdaff2a6e83218d69069242a8458a9a0d5 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Wed, 1 Jan 2020 18:38:22 -0800 Subject: [PATCH] Add config init command --- amanuensis/__main__.py | 8 +++- amanuensis/cli.py | 25 ++++++++++++ amanuensis/resources/default_config.json | 49 ++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 amanuensis/resources/default_config.json diff --git a/amanuensis/__main__.py b/amanuensis/__main__.py index 77c9dfb..5105800 100644 --- a/amanuensis/__main__.py +++ b/amanuensis/__main__.py @@ -85,6 +85,7 @@ def get_parser(valid_commands): parser.set_defaults(func=lambda args: repl(args) if args.lexicon else parser.print_help()) subp = parser.add_subparsers( metavar="COMMAND", + dest="command", help="The command to execute") # Set up command subparsers. @@ -109,8 +110,11 @@ def main(argv): args = get_parser(commands).parse_args(argv) - # With the arguments parsed, initialize the configs. - configs.init(args) + # If the command is the init command, a config directory will be + # initialized at args.config_dir. Otherwise, initialize configs using + # that directory. + if args.command and args.command != "init": + configs.init(args) # Execute command. args.func(args) diff --git a/amanuensis/cli.py b/amanuensis/cli.py index 24d011a..3856632 100644 --- a/amanuensis/cli.py +++ b/amanuensis/cli.py @@ -38,6 +38,31 @@ def no_argument(command): command(cmd_args) return augmented_command + +@no_argument +def command_init(args): + """Initialize an Amanuensis config directory at the directory given by + --config-dir""" + import os + import pkg_resources + cfd = args.config_dir + # Create the directory if it doesn't exist. + if not os.path.isdir(cfd): + os.mkdir(cfd) + # Check if the directory is empty, check with user if not. + if len(os.listdir(cfd)) > 0: + check = input("Directory {} is not empty, proceed? [y/N] ".format(cfd)) + if check != "y": + return -1 + # Directory validated, set up config directory. + def_cfg = pkg_resources.resource_stream(__name__, "resources/default_config.json") + with open(os.path.join(cfd, "config.json"), 'wb') as f: + f.write(def_cfg.read()) + with open(os.path.join(cfd, "pid"), 'w') as f: + f.write(str(os.getpid())) + os.mkdir(os.path.join(cfd, "lexicon")) + os.mkdir(os.path.join(cfd, "user")) + @add_argument("--foo", action="store_true") def command_dump(args): """Dumps the global config or the config for the given lexicon""" diff --git a/amanuensis/resources/default_config.json b/amanuensis/resources/default_config.json new file mode 100644 index 0000000..050beb8 --- /dev/null +++ b/amanuensis/resources/default_config.json @@ -0,0 +1,49 @@ +{ + "address": "127.0.0.1", + "port": "5000", + "lexicon_data": "./lexicon", + "logging": { + "version": 1, + "formatters": { + "fmt_basic": { + "format": "[{levelname}] {message}", + "style": "{" + }, + "fmt_verbose": { + "format": "[{asctime}] [{levelname}:{filename}:{lineno}] {message}", + "style": "{" + } + }, + "handlers": { + "cli_basic": { + "class": "logging.StreamHandler", + "formatter": "fmt_basic", + "level": "INFO" + }, + "cli_verbose": { + "class": "logging.StreamHandler", + "formatter": "fmt_verbose", + "level": "DEBUG" + }, + "file": { + "class": "logging.handlers.RotatingFileHandler", + "formatter": "fmt_verbose", + "level": "DEBUG", + "filename": "./amanuensis.log", + "delay": true, + "encoding": "utf8", + "maxBytes": 1000000, + "backupCount": 10 + } + }, + "loggers": { + "amanuensis": { + "level": "DEBUG", + "handlers": [ + "cli_basic" + ] + } + }, + "disable_existing_loggers": false + } +}