From 10131690937e87cf39692b654909ddd0f811d121 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 20 May 2020 12:27:54 +1000 Subject: [PATCH] Add trivial frontend --- requirements.txt | 1 + src/pyelephant.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100755 src/pyelephant.py diff --git a/requirements.txt b/requirements.txt index b5bd98d..3a79f41 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ alembic +sqlalchemy diff --git a/src/pyelephant.py b/src/pyelephant.py new file mode 100755 index 0000000..74965cf --- /dev/null +++ b/src/pyelephant.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python3 + +import sqlalchemy as sa +import sqlalchemy.ext.automap + +############################################################################### +Base = sa.ext.automap.automap_base() +Task = None + + +############################################################################### +def add(session: sa.orm.Session, title: str): + session.add(Task(title=title)) + session.commit() + + +# ----------------------------------------------------------------------------- +def rm(session: sa.orm.Session, index: int): + session.query(Task.id == index).delete() + session.commit() + + +# ----------------------------------------------------------------------------- +def ls(session: sa.orm.Session): + for i in session.query(Task): + print(i.id, i.title) + + +############################################################################### +if __name__ == '__main__': + import sys + + def main(): + import argparse + import os + + actions = { + 'add': add, + 'rm': rm, + 'ls': ls + } + + parser = argparse.ArgumentParser() + subparsers = parser.add_subparsers() + + add_parser = subparsers.add_parser('add') + add_parser.add_argument("title", type=str) + add_parser.set_defaults(func=add) + + rm_parser = subparsers.add_parser('rm') + rm_parser.add_argument("index", type=int) + rm_parser.set_defaults(func=rm) + + ls_parser = subparsers.add_parser('ls') + ls_parser.set_defaults(func=ls) + + args = parser.parse_args() + + dirname = os.path.dirname(os.path.realpath(__file__)) + dbpath = os.path.realpath(os.path.join(dirname, '..', 'db', 'data.sqlite')) + db = sa.create_engine(f"sqlite:///{dbpath}") + + Base.prepare(db, reflect=True) + + global Task + Task = Base.classes.tasks + + session = sa.orm.Session(db) + args = vars(args) + func = args['func'] + del args['func'] + func(session, **args) + return 0 + + sys.exit(main())