diff options
Diffstat (limited to '')
-rw-r--r-- | archivist/cli.py | 71 | ||||
-rw-r--r-- | archivist/model.py | 15 |
2 files changed, 74 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() diff --git a/archivist/model.py b/archivist/model.py index 375f13d..fa24304 100644 --- a/archivist/model.py +++ b/archivist/model.py @@ -65,6 +65,16 @@ class Prefix(BaseModel): virtual = BooleanField(default = False) description = CharField(null=True) + @classmethod + @db.atomic() + def create(cls, **query): + inst = super().create(**query) + Tag.create_prefix_default(inst) + + @property + def default_tag(self): + return Tag.get(prefix = self, default = True) + def __str__ (self): return self.name @@ -77,6 +87,11 @@ class Tag(BaseModel): name = CharField() prefix = ForeignKeyField(Prefix, null=True, related_name = 'tag', db_column = 'prefix') description = CharField(null=True) + default = BooleanField(default = False) + + @classmethod + def create_prefix_default(cls,prefix): + return cls.create(name = '*', prefix = prefix, description = "Default for prefix '%s'" % prefix, default = True) @hybrid_method def matches(self, prefix, name): |