From b041d5d7843598b009bf90e2289ec5c8d9fc1cf2 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Fri, 17 Jan 2020 06:58:11 -0800 Subject: [PATCH] Implement user-config --- amanuensis/__main__.py | 6 +++++- amanuensis/cli/helpers.py | 10 +++++++--- amanuensis/cli/user.py | 29 ++++++++++++++++++++++++++--- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/amanuensis/__main__.py b/amanuensis/__main__.py index 278c22f..b085e3d 100644 --- a/amanuensis/__main__.py +++ b/amanuensis/__main__.py @@ -79,7 +79,11 @@ def get_parser(valid_commands): parser.add_argument("-n", metavar="LEXICON", dest="lexicon", - help="The name of the lexicon to operate on") + help="Specify a lexicon to operate on") + parser.add_argument("-u", + metavar="USERNAME", + dest="username", + help="Specify a user to operate on") parser.set_defaults(func=lambda args: repl(args) if args.lexicon else parser.print_help()) subp = parser.add_subparsers( metavar="COMMAND", diff --git a/amanuensis/cli/helpers.py b/amanuensis/cli/helpers.py index df2a1b4..4d9c221 100644 --- a/amanuensis/cli/helpers.py +++ b/amanuensis/cli/helpers.py @@ -43,7 +43,8 @@ def requires(argument, verify=lambda a: a is not None): config.logger.error( "This command requires specifying {}".format(argument)) return -1 - command(cmd_args) + if type(cmd_args) is not ArgumentParser or second_layer: + command(cmd_args) augmented_command.__dict__['wrapper'] = True return augmented_command return req_checker @@ -79,13 +80,16 @@ def config_set(cfg, set_tuple): config is from a with json_rw context set_tuple is a tuple of the pathspec and the value """ + import json from config import logger pathspec, value = set_tuple if not pathspec: logger.error("Path must be non-empty") path = pathspec.split('.') - if not value: - value = None + try: + value = json.loads(value) + except: + pass # Leave value as string for spec in path[:-1]: if spec not in cfg: logger.error("Path not found") diff --git a/amanuensis/cli/user.py b/amanuensis/cli/user.py index 376332b..b7237e8 100644 --- a/amanuensis/cli/user.py +++ b/amanuensis/cli/user.py @@ -1,4 +1,4 @@ -from cli.helpers import add_argument, no_argument +from cli.helpers import add_argument, no_argument, requires, config_get, config_set, CONFIG_GET_ROOT_VALUE @add_argument("--username", help="User's login handle") @add_argument("--displayname", help="User's publicly displayed name") @@ -71,12 +71,35 @@ def command_list(args): for user in users: print("{0} {1} ({2})".format(user['uid'], user['displayname'], user['username'])) -@no_argument +@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") +@requires("username") def command_config(args): """ Interact with a user's config """ - raise NotImplementedError() + import json + import config + import user + + if args.get and args.set: + config.logger.error("Specify one of --get and --set") + return -1 + + uid = user.uid_from_username(args.username) + if not uid: + config.logger.error("User not found") + return -1 + + if args.get: + with config.json_ro('user', uid, 'config.json') as cfg: + config_get(cfg, args.get) + + if args.set: + with config.json_rw('user', uid, 'config.json') as cfg: + config_set(cfg, args.set) @add_argument("--username", help="The user to change password for") def command_passwd(args):