From ccc027d5ef654ccaa1a0ff577f7d695b62d9903b Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Mon, 20 Jan 2020 19:57:13 -0800 Subject: [PATCH] Expand char-add, implement char-delete --- amanuensis/cli/lexicon.py | 16 +++++++++++++-- amanuensis/lexicon/manage.py | 38 ++++++++++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/amanuensis/cli/lexicon.py b/amanuensis/cli/lexicon.py index 33a50c4..c04d85f 100644 --- a/amanuensis/cli/lexicon.py +++ b/amanuensis/cli/lexicon.py @@ -226,7 +226,7 @@ def command_char_create(args): # u in lx # Internal call - add_character(lex, u, args.charname) + add_character(lex, u, {"name": args.charname}) @requires_lexicon @@ -238,7 +238,19 @@ def command_char_delete(args): Deleting a character dissociates them from any content 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 def command_char_list(args): diff --git a/amanuensis/lexicon/manage.py b/amanuensis/lexicon/manage.py index 403699a..61edb59 100644 --- a/amanuensis/lexicon/manage.py +++ b/amanuensis/lexicon/manage.py @@ -165,28 +165,54 @@ def remove_player(lex, player): # 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 + + charinfo is a dictionary of character settings """ # Verify arguments if lex is None: raise ValueError("Invalid lexicon: '{}'".format(lex)) if player is None: raise ValueError("Invalid player: '{}'".format(player)) - if not charname: - raise ValueError("Invalid character name: '{}'".format(charname)) + if not charinfo or not charinfo.get("name"): + 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 with resources.get_stream('character.json') as template: character = json.load(template, object_pairs_hook=AttrOrderedDict) # Fill out the character's information - character.cid = uuid.uuid4().hex + character.cid = charinfo.get("cid") or uuid.uuid4().hex character.name = charname - character.player = player.id - character.signature = "~" + charname + character.player = charinfo.get("player") or player.id + character.signature = charinfo.get("signature") or ("~" + character.name) # Add the character to the lexicon with config.json_rw(lex.config_path) as cfg: 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] \ No newline at end of file