Add trivial frontend

This commit is contained in:
Danny Robson 2020-05-20 12:27:54 +10:00
parent 37ee637f7e
commit 1013169093
2 changed files with 76 additions and 0 deletions

View File

@ -1 +1,2 @@
alembic alembic
sqlalchemy

75
src/pyelephant.py Executable file
View File

@ -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())