From 53ea6d529812ec07283c58ec9993d4ca3923902b Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Sat, 4 Sep 2021 20:46:34 -0700 Subject: [PATCH] Add index cli and create command --- amanuensis/cli/__init__.py | 2 ++ amanuensis/cli/index.py | 33 +++++++++++++++++++++++++++++++++ amanuensis/db/models.py | 3 +++ 3 files changed, 38 insertions(+) create mode 100644 amanuensis/cli/index.py diff --git a/amanuensis/cli/__init__.py b/amanuensis/cli/__init__.py index f1d0807..e457db4 100644 --- a/amanuensis/cli/__init__.py +++ b/amanuensis/cli/__init__.py @@ -6,6 +6,7 @@ from typing import Callable import amanuensis.cli.admin import amanuensis.cli.character +import amanuensis.cli.index import amanuensis.cli.lexicon import amanuensis.cli.user from amanuensis.db import DbContext @@ -110,6 +111,7 @@ def main(): subparsers = parser.add_subparsers(metavar="COMMAND") add_subcommand(subparsers, amanuensis.cli.admin) add_subcommand(subparsers, amanuensis.cli.character) + add_subcommand(subparsers, amanuensis.cli.index) add_subcommand(subparsers, amanuensis.cli.lexicon) add_subcommand(subparsers, amanuensis.cli.user) diff --git a/amanuensis/cli/index.py b/amanuensis/cli/index.py new file mode 100644 index 0000000..000508a --- /dev/null +++ b/amanuensis/cli/index.py @@ -0,0 +1,33 @@ +import enum +import logging + +from amanuensis.backend import * +from amanuensis.db import DbContext, ArticleIndex, IndexType + +from .helpers import add_argument + + +COMMAND_NAME = "index" +COMMAND_HELP = "Interact with indexes." + +LOG = logging.getLogger(__name__) + +@add_argument("--lexicon", required=True) +@add_argument("--type", required=True, type=lambda s: IndexType[s.upper()], choices=IndexType) +@add_argument("--pattern", required=True) +@add_argument("--logical", type=int, default=0) +@add_argument("--display", type=int, default=0) +@add_argument("--capacity", type=int, default=None) +def command_create(args) -> int: + """ + Create an index for a lexicon. + """ + db: DbContext = args.get_db() + lexicon = lexiq.try_from_name(db, args.lexicon) + if not lexicon: + raise ValueError("Lexicon does not exist") + index: ArticleIndex = indq.create( + db, lexicon.id, args.type, args.pattern, args.logical, args.display, args.capacity + ) + LOG.info(f"Created {index.index_type}:{index.pattern} in {lexicon.full_title}") + return 0 diff --git a/amanuensis/db/models.py b/amanuensis/db/models.py index 2e63c4c..c9461a9 100644 --- a/amanuensis/db/models.py +++ b/amanuensis/db/models.py @@ -463,6 +463,9 @@ class IndexType(enum.Enum): PREFIX = 2 ETC = 3 + def __str__(self): + return self.name + class ArticleIndex(ModelBase): """