Basic character add command

This commit is contained in:
Tim Van Baak 2020-01-20 17:30:43 -08:00
parent 8c70067076
commit 2608a992fe
2 changed files with 50 additions and 2 deletions

View File

@ -208,7 +208,26 @@ def command_char_create(args):
The specified player will be set as the character's player. 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 @requires_lexicon
@add_argument("--charname", required=True, help="The character's name") @add_argument("--charname", required=True, help="The character's name")

View File

@ -2,6 +2,7 @@
Functions for managing lexicons, primarily within the context of the Functions for managing lexicons, primarily within the context of the
Amanuensis config directory. Amanuensis config directory.
""" """
import json
import os import os
import re import re
import shutil import shutil
@ -9,6 +10,7 @@ import time
import uuid import uuid
import config import config
from config.loader import AttrOrderedDict
import lexicon import lexicon
import resources import resources
@ -161,3 +163,30 @@ def remove_player(lex, player):
cfg.join.joined.remove(player.id) cfg.join.joined.remove(player.id)
# TODO Reassign the player's characters to the editor # 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)