diff options
Diffstat (limited to '')
-rw-r--r-- | archivist/cli.py | 77 |
1 files changed, 67 insertions, 10 deletions
diff --git a/archivist/cli.py b/archivist/cli.py index 0b8abe1..8238a30 100644 --- a/archivist/cli.py +++ b/archivist/cli.py @@ -61,6 +61,12 @@ def list_prefixes(): for p in Prefix.select(): print(" * %s (builtin: %s; pseudo: %s)" % (p.name, p.builtin, p.pseudo)) +def prefix_tag_name(name, prefix=None): + if prefix: + return "%s:%s" % (prefix, name) + else: + return name + class PrefixTag: def __init__(self, tag, prefix = None): self.tag = tag @@ -72,6 +78,16 @@ class PrefixTag: else: return self.tag + def __iter__(self): + yield self.prefix + yield self.tag + + def prefixed_name(self): + return prefix_tag_name(self.tag, self.prefix) + + def __str__(self): + return self.prefixed_name() + class PrefixTagType(click.ParamType): name = 'prefixed tag' @@ -87,12 +103,8 @@ class PrefixTagType(click.ParamType): TAG = PrefixTagType() -@tag.command('add') -@click.argument('name', type = TAG) -@click.argument('description', required = False) -def add_tag(name, description): +def create_tag(name, description=None): from .model import Tag, Prefix, db - with db.atomic(): if name.prefix: prefix, created = Prefix.get_or_create(name = name.prefix) @@ -100,14 +112,20 @@ def add_tag(name, description): if not created and prefix.pseudo: raise click.UsageError("Prefix '%s' is not allowed to carry additional tags." % name.prefix) - tag, created = Tag.create_or_get(name = name.tag, prefix = name.prefix, description = description) - if not created: - print("Tag already existed:", tag) + return Tag.create_or_get(name = name.tag, prefix = name.prefix, description = description) + +@tag.command('add') +@click.argument('name', type = TAG) +@click.argument('description', required = False) +def add_tag(name, description): + created, tag = create_tag(name, description) + if not created: + print("Tag already existed:", tag) @tag.command('edit') -@click.option('--description') @click.argument('name', type = TAG) -def edit_tag(description, name): +@click.option('--description') +def edit_tag(name, description): from .model import Tag, Prefix, db try: @@ -118,3 +136,42 @@ def edit_tag(description, name): if description: tag.description = description tag.save() + +@cli.group() +def doc(): + """Document handling""" + pass + +@doc.command('add') +@click.argument('file', type=click.File(mode = 'rb')) +@click.argument('tags', type = TAG, nargs = -1) +@click.option('--create-tags', '-c', is_flag = True) +@click.option('--ignore-missing-tags', '-i', is_flag = True) +def add_doc(file, tags, create_tags, ignore_missing_tags): + """Add a new document together with the given tags.""" + + from .model import Document, Tag, db, DocumentTag + from .peewee_ext import sqlite_tuple_in + import magic + + with db.atomic(): + if tags: + if create_tags: + tags = [create_tag(tag)[0] for tag in tags] + else: + fetched_tags = Tag.select().where(sqlite_tuple_in((Tag.prefix, Tag.name), tags)).execute() + if len(fetched_tags) < len(tags): + print("Some tags were not present:", ', '.join(set(map(str, tags)).difference(map(str, fetched_tags)))) + if not ignore_missing_tags: + raise click.ClickException("Not all tags present") + tags = fetched_tags + + mimetype = magic.from_file(file.name, mime=True) + + doc = Document.create(content = file.read(), + file_type = mimetype, + original_path = file.name, + direction = Document.Direction.IN) + + for t in tags: + DocumentTag.create(document = doc, tag = t) |