Add index cli and create command

This commit is contained in:
Tim Van Baak 2021-09-04 20:46:34 -07:00
parent bff25fb97f
commit 03c0b4ce70
3 changed files with 47 additions and 0 deletions

View File

@ -6,6 +6,7 @@ from typing import Callable
import amanuensis.cli.admin import amanuensis.cli.admin
import amanuensis.cli.character import amanuensis.cli.character
import amanuensis.cli.index
import amanuensis.cli.lexicon import amanuensis.cli.lexicon
import amanuensis.cli.user import amanuensis.cli.user
from amanuensis.db import DbContext from amanuensis.db import DbContext
@ -110,6 +111,7 @@ def main():
subparsers = parser.add_subparsers(metavar="COMMAND") subparsers = parser.add_subparsers(metavar="COMMAND")
add_subcommand(subparsers, amanuensis.cli.admin) add_subcommand(subparsers, amanuensis.cli.admin)
add_subcommand(subparsers, amanuensis.cli.character) add_subcommand(subparsers, amanuensis.cli.character)
add_subcommand(subparsers, amanuensis.cli.index)
add_subcommand(subparsers, amanuensis.cli.lexicon) add_subcommand(subparsers, amanuensis.cli.lexicon)
add_subcommand(subparsers, amanuensis.cli.user) add_subcommand(subparsers, amanuensis.cli.user)

42
amanuensis/cli/index.py Normal file
View File

@ -0,0 +1,42 @@
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

View File

@ -463,6 +463,9 @@ class IndexType(enum.Enum):
PREFIX = 2 PREFIX = 2
ETC = 3 ETC = 3
def __str__(self):
return self.name
class ArticleIndex(ModelBase): class ArticleIndex(ModelBase):
""" """