Simplify @requires

This commit is contained in:
Tim Van Baak 2020-01-18 11:51:54 -08:00
parent 2a314da910
commit 51b2bfe875
4 changed files with 39 additions and 33 deletions

View File

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

View File

@ -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()

View File

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

View File

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