Add config init command

This commit is contained in:
Tim Van Baak 2020-01-01 18:38:22 -08:00
parent 7a85604ece
commit bf27d2bdaf
3 changed files with 80 additions and 2 deletions

View File

@ -85,6 +85,7 @@ def get_parser(valid_commands):
parser.set_defaults(func=lambda args: repl(args) if args.lexicon else parser.print_help()) parser.set_defaults(func=lambda args: repl(args) if args.lexicon else parser.print_help())
subp = parser.add_subparsers( subp = parser.add_subparsers(
metavar="COMMAND", metavar="COMMAND",
dest="command",
help="The command to execute") help="The command to execute")
# Set up command subparsers. # Set up command subparsers.
@ -109,8 +110,11 @@ def main(argv):
args = get_parser(commands).parse_args(argv) args = get_parser(commands).parse_args(argv)
# With the arguments parsed, initialize the configs. # If the command is the init command, a config directory will be
configs.init(args) # initialized at args.config_dir. Otherwise, initialize configs using
# that directory.
if args.command and args.command != "init":
configs.init(args)
# Execute command. # Execute command.
args.func(args) args.func(args)

View File

@ -38,6 +38,31 @@ def no_argument(command):
command(cmd_args) command(cmd_args)
return augmented_command 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") @add_argument("--foo", action="store_true")
def command_dump(args): def command_dump(args):
"""Dumps the global config or the config for the given lexicon""" """Dumps the global config or the config for the given lexicon"""

View File

@ -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
}
}