Update lexicon CRUD for new @requires semantics

This commit is contained in:
Tim Van Baak 2020-01-29 17:15:07 -08:00
parent 4a49433cde
commit 7a2e1905ca
1 changed files with 38 additions and 47 deletions

View File

@ -1,40 +1,42 @@
from amanuensis.cli.helpers import ( from amanuensis.cli.helpers import (
add_argument, no_argument, requires_lexicon, requires_username, add_argument, no_argument, requires_lexicon, requires_user,
config_get, config_set, CONFIG_GET_ROOT_VALUE) config_get, config_set, CONFIG_GET_ROOT_VALUE)
# #
# CRUD commands # CRUD commands
# #
@requires_lexicon @add_argument("--name", required=True, help="The name of the new lexicon")
@add_argument( @requires_user
"--editor", "-e", required=True, @add_argument("--prompt", help="The lexicon's prompt")
help="Name of the user who will be editor")
def command_create(args): def command_create(args):
""" """
Create a lexicon Create a lexicon
A newly created lexicon is not open for joining and requires additional The specified user will be the editor. A newly created created lexicon is
configuration before it is playable. The editor should ensure that all not open for joining and requires additional configuration before it is
settings are as desired before opening the lexicon for player joins. playable. The editor should ensure that all settings are as desired before
opening the lexicon for player joins.
""" """
# Module imports # Module imports
from amanuensis.config import logger from amanuensis.config import logger, json_ro
from amanuensis.lexicon.manage import valid_name, create_lexicon from amanuensis.lexicon.manage import valid_name, create_lexicon
from amanuensis.user import UserModel
# Verify arguments # Verify arguments
if not valid_name(args.lexicon): if not valid_name(args.name):
logger.error("Lexicon name contains illegal characters: '{}'".format( logger.error("Lexicon name contains illegal characters: '{}'".format(
args.lexicon)) args.name))
return -1 return -1
editor = UserModel.by(name=args.editor) with json_ro('lexicon', 'index.json') as index:
if editor is None: if args.name in index.keys():
logger.error("Could not find user '{}'".format(args.editor)) logger.error('A lexicon with name "{}" already exists'.format(
args.name))
return -1 return -1
# Internal call # Perform command
create_lexicon(args.lexicon, editor) lexicon = create_lexicon(args.name, args.user)
# Output already logged by create_lexicon
return 0 return 0
@ -49,15 +51,11 @@ def command_delete(args):
from amanuensis.lexicon import LexiconModel from amanuensis.lexicon import LexiconModel
from amanuensis.lexicon.manage import delete_lexicon from amanuensis.lexicon.manage import delete_lexicon
# Verify arguments # Perform command
lex = LexiconModel.by(name=args.lexicon) delete_lexicon(args.lexicon, args.purge)
if lex is None:
logger.error("Could not find lexicon '{}'".format(args.lexicon))
return -1
# Internal call # Output
delete_lexicon(lex, args.purge) logger.info('Deleted lexicon "{}"'.format(args.lexicon.name))
logger.info("Deleted lexicon '{}'".format(args.lexicon))
return 0 return 0
@ -69,17 +67,13 @@ def command_list(args):
# Module imports # Module imports
from amanuensis.lexicon.manage import get_all_lexicons from amanuensis.lexicon.manage import get_all_lexicons
# Internal call # Execute command
lexicons = get_all_lexicons() lexicons = get_all_lexicons()
# Output
statuses = [] statuses = []
for lex in lexicons: for lex in lexicons:
if lex.turn['current'] is None: statuses.append("{0.lid} {0.name} ({1})".format(lex, lex.status()))
statuses.append("{0.lid} {0.name} ({1})".format(lex, "Unstarted"))
elif lex.turn['current'] > lex.turn['max']:
statuses.append("{0.lid} {0.name} ({1})".format(lex, "Completed"))
else:
statuses.append("{0.lid} {0.name} (Turn {1}/{2})".format(
lex, lex.turn['current'], lex.turn['max']))
for s in statuses: for s in statuses:
print(s) print(s)
return 0 return 0
@ -88,7 +82,8 @@ def command_list(args):
@requires_lexicon @requires_lexicon
@add_argument( @add_argument(
"--get", metavar="PATHSPEC", dest="get", "--get", metavar="PATHSPEC", dest="get",
nargs="?", const=CONFIG_GET_ROOT_VALUE, help="Get the value of a config key") nargs="?", const=CONFIG_GET_ROOT_VALUE,
help="Get the value of a config key")
@add_argument( @add_argument(
"--set", metavar=("PATHSPEC", "VALUE"), dest="set", "--set", metavar=("PATHSPEC", "VALUE"), dest="set",
nargs=2, help="Set the value of a config key") nargs=2, help="Set the value of a config key")
@ -104,20 +99,16 @@ def command_config(args):
if args.get and args.set: if args.get and args.set:
logger.error("Specify one of --get and --set") logger.error("Specify one of --get and --set")
return -1 return -1
lex = LexiconModel.by(name=args.lexicon)
if lex is None:
logger.error("Could not find lexicon '{}'".format(args.lexicon))
return -1
# Internal call # Execute command
if args.get: if args.get:
with json_ro(lex.config_path) as cfg: config_get(args.lexicon.config, args.get)
config_get(cfg, args.get)
if args.set: if args.set:
with json_rw(lex.config_path) as cfg: with json_rw(args.lexicon.config_path) as cfg:
config_set(lex.id, cfg, args.set) config_set(args.lexicon.id, cfg, args.set)
# config_* functions handle output
return 0 return 0
# #
@ -125,7 +116,7 @@ def command_config(args):
# #
@requires_lexicon @requires_lexicon
@requires_username # @requires_username
def command_player_add(args): def command_player_add(args):
""" """
Add a player to a lexicon Add a player to a lexicon
@ -152,7 +143,7 @@ def command_player_add(args):
@requires_lexicon @requires_lexicon
@requires_username # @requires_username
def command_player_remove(args): def command_player_remove(args):
""" """
Remove a player from a lexicon Remove a player from a lexicon
@ -212,7 +203,7 @@ def command_player_list(args):
@requires_lexicon @requires_lexicon
@requires_username # @requires_username
@add_argument("--charname", required=True, help="The character's name") @add_argument("--charname", required=True, help="The character's name")
def command_char_create(args): def command_char_create(args):
""" """