Add index cli and create command

This commit is contained in:
Tim Van Baak 2021-09-04 20:46:34 -07:00
parent 7e54b14b7f
commit 53ea6d5298
3 changed files with 38 additions and 0 deletions

View File

@ -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)

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

@ -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

View File

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