From 51b2bfe875a74da9b6b8ea6c3e5481023b81acb4 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Sat, 18 Jan 2020 11:51:54 -0800 Subject: [PATCH] Simplify @requires --- amanuensis/__main__.py | 4 ++-- amanuensis/cli/helpers.py | 26 ++++++++++++++++++-------- amanuensis/cli/lexicon.py | 36 +++++++++++++++--------------------- amanuensis/cli/user.py | 6 ++++-- 4 files changed, 39 insertions(+), 33 deletions(-) diff --git a/amanuensis/__main__.py b/amanuensis/__main__.py index b085e3d..c30b207 100644 --- a/amanuensis/__main__.py +++ b/amanuensis/__main__.py @@ -78,11 +78,11 @@ def get_parser(valid_commands): # Lexicon settings. parser.add_argument("-n", metavar="LEXICON", - dest="lexicon", + dest="tl_lexicon", help="Specify a lexicon to operate on") parser.add_argument("-u", metavar="USERNAME", - dest="username", + dest="tl_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( diff --git a/amanuensis/cli/helpers.py b/amanuensis/cli/helpers.py index 4d9c221..9ea48c3 100644 --- a/amanuensis/cli/helpers.py +++ b/amanuensis/cli/helpers.py @@ -29,26 +29,36 @@ def no_argument(command): command(cmd_args) return augmented_command -# This wrapper is another verification step +# Wrapper for commands requiring lexicon or username options -def requires(argument, verify=lambda a: a is not None): - """Errors out if the given argument is not present""" +def requires(arg, metavar, dest, fallback, help): + """Subparser fallback for -u and -n options""" def req_checker(command): - second_layer = command.__dict__.get('wrapper', False) @wraps(command) def augmented_command(cmd_args): + if type(cmd_args) is ArgumentParser: + cmd_args.add_argument(arg, metavar=metavar, dest=dest, help=help) + if command.__dict__.get('wrapper', False): + command(cmd_args) if type(cmd_args) is Namespace: - if not hasattr(cmd_args, argument) or not verify(getattr(cmd_args, argument)): + base_val = hasattr(cmd_args, fallback) and getattr(cmd_args, fallback) + subp_val = hasattr(cmd_args, dest) and getattr(cmd_args, dest) + val = subp_val or base_val or None + if not val: import config - config.logger.error( - "This command requires specifying {}".format(argument)) + config.logger.error("This command requires {}".format(arg)) return -1 - if type(cmd_args) is not ArgumentParser or second_layer: + setattr(cmd_args, dest, val) command(cmd_args) augmented_command.__dict__['wrapper'] = True return augmented_command return req_checker +requires_lexicon = requires("-n", "LEXICON", "lexicon", "tl_lexicon", + "Specify a lexicon to operate on") +requires_username = requires("-u", "USERNAME", "username", "tl_username", + "Specify a user to operate on") + # Helpers for common command tasks CONFIG_GET_ROOT_VALUE = object() diff --git a/amanuensis/cli/lexicon.py b/amanuensis/cli/lexicon.py index a3f137b..66c662b 100644 --- a/amanuensis/cli/lexicon.py +++ b/amanuensis/cli/lexicon.py @@ -1,13 +1,13 @@ from cli.helpers import ( - add_argument, no_argument, requires, + add_argument, no_argument, requires_lexicon, requires_username, config_get, config_set, CONFIG_GET_ROOT_VALUE) # # CRUD commands # -@add_argument("--name", required=True, help="Name of the lexicon") -@add_argument("--editor", required=True, help="Name of the user who will be editor") +@requires_lexicon +@add_argument("--editor", "-e", required=True, help="Name of the user who will be editor") def command_create(args): """ Create a lexicon @@ -23,8 +23,7 @@ def command_create(args): u = user.user_from_uid(uid) create_lexicon(args.name, u) -@no_argument -@requires("lexicon") +@requires_lexicon def command_delete(args): """ Delete a lexicon and all its data @@ -38,8 +37,7 @@ def command_list(args): """ raise NotImplementedError() # TODO -@no_argument -@requires("lexicon") +@requires_lexicon def command_config(args): """ Interact with a lexicon's config @@ -50,16 +48,16 @@ def command_config(args): # Player/character commands # -@no_argument -@requires("lexicon") +@requires_lexicon +@requires_username def command_player_add(args): """ Add a player to a lexicon """ raise NotImplementedError() # TODO -@no_argument -@requires("lexicon") +@requires_lexicon +@requires_username def command_player_remove(args): """ Remove a player from a lexicon @@ -69,24 +67,22 @@ def command_player_remove(args): """ raise NotImplementedError() # TODO -@no_argument -@requires("lexicon") +@requires_lexicon +@requires_username def command_player_list(args): """ List all players in a lexicon """ raise NotImplementedError() # TODO -@no_argument -@requires("lexicon") +@requires_lexicon def command_char_create(args): """ Create a character for a lexicon """ raise NotImplementedError() # TODO -@no_argument -@requires("lexicon") +@requires_lexicon def command_char_delete(args): """ Delete a character from a lexicon @@ -96,8 +92,7 @@ def command_char_delete(args): """ raise NotImplementedError() # TODO -@no_argument -@requires("lexicon") +@requires_lexicon def command_char_list(args): """ List all characters in a lexicon @@ -108,8 +103,7 @@ def command_char_list(args): # Procedural commands # -@no_argument -@requires("lexicon") +@requires_lexicon def command_publish_turn(args): """ Publishes the current turn of a lexicon diff --git a/amanuensis/cli/user.py b/amanuensis/cli/user.py index b7237e8..f33a5f5 100644 --- a/amanuensis/cli/user.py +++ b/amanuensis/cli/user.py @@ -1,4 +1,6 @@ -from cli.helpers import add_argument, no_argument, requires, config_get, config_set, CONFIG_GET_ROOT_VALUE +from cli.helpers import ( + add_argument, no_argument, requires_username, + 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,11 +73,11 @@ def command_list(args): for user in users: print("{0} {1} ({2})".format(user['uid'], user['displayname'], user['username'])) +@requires_username @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