From e480658ebe01d0629202f33fd0429cb404797da6 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Thu, 19 Aug 2021 18:17:06 -0700 Subject: [PATCH] Add character create command --- amanuensis/cli/__init__.py | 2 ++ amanuensis/cli/character.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 amanuensis/cli/character.py diff --git a/amanuensis/cli/__init__.py b/amanuensis/cli/__init__.py index 1fa9234..f1d0807 100644 --- a/amanuensis/cli/__init__.py +++ b/amanuensis/cli/__init__.py @@ -5,6 +5,7 @@ import os from typing import Callable import amanuensis.cli.admin +import amanuensis.cli.character import amanuensis.cli.lexicon import amanuensis.cli.user from amanuensis.db import DbContext @@ -108,6 +109,7 @@ def main(): # Add commands from cli submodules subparsers = parser.add_subparsers(metavar="COMMAND") add_subcommand(subparsers, amanuensis.cli.admin) + add_subcommand(subparsers, amanuensis.cli.character) add_subcommand(subparsers, amanuensis.cli.lexicon) add_subcommand(subparsers, amanuensis.cli.user) diff --git a/amanuensis/cli/character.py b/amanuensis/cli/character.py new file mode 100644 index 0000000..f49fe61 --- /dev/null +++ b/amanuensis/cli/character.py @@ -0,0 +1,31 @@ +import logging + +from amanuensis.backend import lexiq, userq, charq +from amanuensis.db import DbContext, Character + +from .helpers import add_argument + + +COMMAND_NAME = "char" +COMMAND_HELP = "Interact with characters." + +LOG = logging.getLogger(__name__) + + +@add_argument("--lexicon", required=True) +@add_argument("--user", required=True) +@add_argument("--name", required=True) +def command_create(args) -> int: + """ + Create a character. + """ + db: DbContext = args.get_db() + lexicon = lexiq.try_from_name(db, args.lexicon) + if not lexicon: + raise ValueError("Lexicon does not exist") + user = userq.try_from_username(db, args.user) + if not user: + raise ValueError("User does not exist") + char: Character = charq.create(db, lexicon.id, user.id, args.name, signature=None) + LOG.info(f"Created {char.name} in {lexicon.full_title}") + return 0