From 562d7d8a4b2e39e12c36756f79873e2eeab40192 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Wed, 16 Jun 2021 20:13:07 -0700 Subject: [PATCH] Add some basic lexicon cli commands --- amanuensis/backend/lexicon.py | 21 +++++++++++-- amanuensis/cli/lexicon.py | 55 +++++++++++++++++++++++++++++------ 2 files changed, 64 insertions(+), 12 deletions(-) diff --git a/amanuensis/backend/lexicon.py b/amanuensis/backend/lexicon.py index 8c50c7b..6df3473 100644 --- a/amanuensis/backend/lexicon.py +++ b/amanuensis/backend/lexicon.py @@ -3,11 +3,11 @@ Lexicon query interface """ import re -from typing import Sequence +from typing import Sequence, Optional from sqlalchemy import select, func -from amanuensis.db import DbContext, Lexicon +from amanuensis.db import DbContext, Lexicon, Membership from amanuensis.errors import ArgumentError @@ -17,7 +17,7 @@ RE_ALPHANUM_DASH_UNDER = re.compile(r"^[A-Za-z0-9-_]*$") def create( db: DbContext, name: str, - title: str, + title: Optional[str], prompt: str, ) -> Lexicon: """ @@ -55,6 +55,21 @@ def create( return new_lexicon +def from_name(db: DbContext, name: str) -> Lexicon: + """Get a lexicon by its name.""" + return db(select(Lexicon).where(Lexicon.name == name)).scalar_one() + + def get_all(db: DbContext) -> Sequence[Lexicon]: """Get all lexicons.""" return db(select(Lexicon)).scalars() + + +def get_joined(db: DbContext, user_id: int) -> Sequence[Lexicon]: + """Get all lexicons that a player is in.""" + return db(select(Lexicon).join(Lexicon.memberships).where(Membership.user_id == user_id)).scalars() + + +def get_public(db: DbContext) -> Sequence[Lexicon]: + """Get all publicly visible lexicons.""" + return db(select(Lexicon).where(Lexicon.public == True)).scalars() diff --git a/amanuensis/cli/lexicon.py b/amanuensis/cli/lexicon.py index 92fc7ab..99a3150 100644 --- a/amanuensis/cli/lexicon.py +++ b/amanuensis/cli/lexicon.py @@ -1,5 +1,13 @@ +from argparse import BooleanOptionalAction import logging +from sqlalchemy import update + +import amanuensis.backend.lexicon as lexiq +import amanuensis.backend.membership as memq +import amanuensis.backend.user as userq +from amanuensis.db import DbContext, Lexicon + from .helpers import add_argument @@ -9,22 +17,51 @@ COMMAND_HELP = "Interact with lexicons." LOG = logging.getLogger(__name__) +@add_argument("lexicon") +@add_argument("user") +@add_argument("--editor", action="store_true") +def command_add(args) -> int: + db: DbContext = args.get_db() + lexicon = lexiq.from_name(db, args.lexicon) + user = userq.from_username(db, args.user) + assert user is not None + memq.create(db, user.id, lexicon.id, args.editor) + LOG.info(f"Added {args.user} to lexicon {args.lexicon}") + return 0 + + +@add_argument("name") def command_create(args): """ Create a lexicon. """ - raise NotImplementedError() + db: DbContext = args.get_db() + lexiq.create(db, args.name, None, f"Prompt for Lexicon {args.name}") + LOG.info(f"Created lexicon {args.name}") + return 0 -def command_delete(args): +@add_argument("name") +@add_argument("--public", action=BooleanOptionalAction) +@add_argument("--join", action=BooleanOptionalAction) +def command_edit(args): """ - Delete a lexicon. + Update a lexicon's configuration. """ - raise NotImplementedError() + db: DbContext = args.get_db() + values = {} + if args.public == True: + values["public"] = True + elif args.public == False: + values["public"] = False -def command_list(args): - """ - List all lexicons and their statuses. - """ - raise NotImplementedError() + if args.join == True: + values["joinable"] = True + elif args.join == False: + values["joinable"] = False + + result = db(update(Lexicon).where(Lexicon.name == args.name).values(**values)) + LOG.info(f"Updated {result.rowcount} lexicons") + db.session.commit() + return 0 if result.rowcount == 1 else -1