summaryrefslogtreecommitdiff
path: root/archivist/cli.py
blob: 4316705fff8fc7b96b9a277ea59c54c8ba064171 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import click

CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])

@click.group(context_settings = CONTEXT_SETTINGS)
@click.option('--debug', '-d', is_flag=True, default=False)
def cli(debug):
    if debug:
        import logging
        logger = logging.getLogger('peewee')
        logger.setLevel(logging.DEBUG)
        logger.addHandler(logging.StreamHandler())

@cli.group()
def db():
    """Database Management"""
    pass

@db.command()
def init():
    """Initialize the database, if not done already."""
    from .model import create_tables, Prefix, db
    create_tables()
    with db.atomic():
        Prefix.create(name='year', builtin=True)
        Prefix.create(name='month', builtin=True)
        Prefix.create(name='day', builtin=True)
        Prefix.create(name='date', builtin=True, pseudo=True)
        Prefix.create(name='direction', builtin=True, pseudo=True)

@db.command()
@click.confirmation_option(prompt="Are you sure you want to drop the database?")
def drop():
    """Completely drop all tables."""
    from .model import drop_tables
    drop_tables()

@cli.group()
def list():
    pass

@list.command("prefixes")
def list_prefixes():
    from .model import Prefix, db
    
    print("Prefixes")
    print("========")
    print()

    for p in Prefix.select():
        print("  * %s (builtin: %s; pseudo: %s)" % (p.name, p.builtin, p.pseudo))