diff options
Diffstat (limited to '')
-rw-r--r-- | archivist/cli.py | 71 |
1 files changed, 59 insertions, 12 deletions
diff --git a/archivist/cli.py b/archivist/cli.py index 1ebe2db..80b6094 100644 --- a/archivist/cli.py +++ b/archivist/cli.py @@ -39,18 +39,32 @@ def tag(): """Handling of tags""" pass +@cli.group() +def prefix(): + """Handling of prefixes of tags""" + pass + @tag.command('list') -def list_tags(): +@click.argument('pattern', required = False) +def list_tags(pattern): from .model import Tag print("Tags") print("====") print() - for t in Tag.select(): - print(' *', t) + query = Tag.select().where(~Tag.default) + + if pattern: + query = query.where(Tag.name.contains(pattern) | Tag.prefix.contains(pattern)) -@tag.command('prefixes') + for t in query: + descr = t.description or '' + if descr: + descr = '-- ' + descr + print(' *', t, descr) + +@prefix.command('list') def list_prefixes(): from .model import Prefix @@ -61,6 +75,16 @@ def list_prefixes(): for p in Prefix.select(): print(" * %s" % p) +@prefix.command('add') +@click.argument('name') +@click.argument('description', required = False) +def add_prefix(name, description): + from .model import Prefix + + prefix, created = Prefix.create_or_get(name = name, description = description) + if not created: + print("Prefix already existed:", prefix) + def prefix_tag_name(name, prefix=None): if prefix: return "%s:%s" % (prefix, name) @@ -100,6 +124,25 @@ class PrefixTagType(click.ParamType): TAG = PrefixTagType() +@prefix.command('edit') +@click.argument('name') +@click.argument('implies', type = TAG, nargs = -1) +@click.option('--description') +def edit_prefix(name, implies, description): + from .model import Prefix, db + + try: + prefix = Prefix.get(name = name) + except Prefix.DoesNotExist: + raise click.UsageError("Prefix '%s' does not exist." % name) + + with db.atomic(): + add_implications(prefix.default_tag, implies) + if description: + prefix.description = description + prefix.save() + + def create_tag(name, description=None): from .model import Tag, Prefix, db with db.atomic(): @@ -134,13 +177,22 @@ def add_tag(name, description): if not created: print("Tag already existed:", tag) +def add_implications(tag, implies): + from .model import TagImplications + from peewee import IntegrityError + + for i in fetch_tags(implies): + try: + TagImplications.create(tag = tag, implies_tag = i) + except IntegrityError: + print("Implication onto '%s' already existing. Skipping." % i) + @tag.command('edit') @click.argument('name', type = TAG) @click.argument('implies', type = TAG, nargs = -1) @click.option('--description') def edit_tag(name, implies, description): - from .model import Tag, TagImplications, db - from peewee import IntegrityError + from .model import Tag, db try: tag = Tag.get(name = name.tag, prefix = name.prefix) @@ -148,12 +200,7 @@ def edit_tag(name, implies, description): raise click.UsageError("Tag '%s' does not exist." % name) with db.atomic(): - for i in fetch_tags(implies): - try: - TagImplications.create(tag = tag, implies_tag = i) - except IntegrityError: - print("Implication onto '%s' already existing. Skipping." % i) - + add_implications(tag, implies) if description: tag.description = description tag.save() |