Expand char-add, implement char-delete

This commit is contained in:
Tim Van Baak 2020-01-20 19:57:13 -08:00
parent 2608a992fe
commit ccc027d5ef
2 changed files with 46 additions and 8 deletions

View File

@ -226,7 +226,7 @@ def command_char_create(args):
# u in lx # u in lx
# Internal call # Internal call
add_character(lex, u, args.charname) add_character(lex, u, {"name": args.charname})
@requires_lexicon @requires_lexicon
@ -238,7 +238,19 @@ def command_char_delete(args):
Deleting a character dissociates them from any content Deleting a character dissociates them from any content
they have contributed rather than deleting it. they have contributed rather than deleting it.
""" """
raise NotImplementedError() # TODO # Module imports
from config import logger
from lexicon import LexiconModel
from lexicon.manage import delete_character
# Verify arguments
lex = LexiconModel.by(name=args.lexicon)
if lex is None:
logger.error("Could not find lexicon '{}'".format(args.lexicon))
return -1
# Internal call
delete_character(lex, args.charname)
@requires_lexicon @requires_lexicon
def command_char_list(args): def command_char_list(args):

View File

@ -165,28 +165,54 @@ def remove_player(lex, player):
# TODO Reassign the player's characters to the editor # TODO Reassign the player's characters to the editor
def add_character(lex, player, charname): def add_character(lex, player, charinfo={}):
""" """
Unconditionally adds a character to a lexicon Unconditionally adds a character to a lexicon
charinfo is a dictionary of character settings
""" """
# Verify arguments # Verify arguments
if lex is None: if lex is None:
raise ValueError("Invalid lexicon: '{}'".format(lex)) raise ValueError("Invalid lexicon: '{}'".format(lex))
if player is None: if player is None:
raise ValueError("Invalid player: '{}'".format(player)) raise ValueError("Invalid player: '{}'".format(player))
if not charname: if not charinfo or not charinfo.get("name"):
raise ValueError("Invalid character name: '{}'".format(charname)) raise ValueError("Invalid character info: '{}'".format(charinfo))
charname = charinfo.get("name")
if any([char.name for char in lex.character.values() if char.name == charname]):
raise ValueError("Duplicate character name: '{}'".format(charinfo))
# Load the character template # Load the character template
with resources.get_stream('character.json') as template: with resources.get_stream('character.json') as template:
character = json.load(template, object_pairs_hook=AttrOrderedDict) character = json.load(template, object_pairs_hook=AttrOrderedDict)
# Fill out the character's information # Fill out the character's information
character.cid = uuid.uuid4().hex character.cid = charinfo.get("cid") or uuid.uuid4().hex
character.name = charname character.name = charname
character.player = player.id character.player = charinfo.get("player") or player.id
character.signature = "~" + charname character.signature = charinfo.get("signature") or ("~" + character.name)
# Add the character to the lexicon # Add the character to the lexicon
with config.json_rw(lex.config_path) as cfg: with config.json_rw(lex.config_path) as cfg:
cfg.character.new(character.cid, character) cfg.character.new(character.cid, character)
def delete_character(lex, charname):
"""
Delete a character from a lexicon
"""
# Verify arguments
if lex is None:
raise ValueError("Invalid lexicon: '{}'".format(lex))
if charname is None:
raise ValueError("Invalid character name: '{}'".format(charinfo))
# Find character in this lexicon
matches = [char for cid, char in lex.character.items() if char.name == charname]
if len(matches) != 1:
raise ValueError(matches)
char = matches[0]
# Remove character from character list
with config.json_rw(lex.config_path) as cfg:
del cfg.character[char.cid]