diff --git a/amanuensis/cli/lexicon.py b/amanuensis/cli/lexicon.py index 3db0fb4..33a50c4 100644 --- a/amanuensis/cli/lexicon.py +++ b/amanuensis/cli/lexicon.py @@ -208,7 +208,26 @@ def command_char_create(args): The specified player will be set as the character's player. """ - raise NotImplementedError() # TODO + # Module imports + from config import logger + from lexicon import LexiconModel + from lexicon.manage import add_character + from user import UserModel + + # Verify arguments + u = UserModel.by(name=args.username) + if u is None: + logger.error("Could not find user '{}'".format(args.username)) + return -1 + lex = LexiconModel.by(name=args.lexicon) + if lex is None: + logger.error("Could not find lexicon '{}'".format(args.lexicon)) + return -1 + # u in lx + + # Internal call + add_character(lex, u, args.charname) + @requires_lexicon @add_argument("--charname", required=True, help="The character's name") diff --git a/amanuensis/lexicon/manage.py b/amanuensis/lexicon/manage.py index eeff0ec..403699a 100644 --- a/amanuensis/lexicon/manage.py +++ b/amanuensis/lexicon/manage.py @@ -2,6 +2,7 @@ Functions for managing lexicons, primarily within the context of the Amanuensis config directory. """ +import json import os import re import shutil @@ -9,6 +10,7 @@ import time import uuid import config +from config.loader import AttrOrderedDict import lexicon import resources @@ -160,4 +162,31 @@ def remove_player(lex, player): if player.id in cfg.join.joined: cfg.join.joined.remove(player.id) - # TODO Reassign the player's characters to the editor \ No newline at end of file + # TODO Reassign the player's characters to the editor + + +def add_character(lex, player, charname): + """ + Unconditionally adds a character to a lexicon + """ + # 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)) + + # 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.name = charname + character.player = player.id + character.signature = "~" + charname + + # Add the character to the lexicon + with config.json_rw(lex.config_path) as cfg: + cfg.character.new(character.cid, character)