Add trivial frontend
This commit is contained in:
parent
37ee637f7e
commit
1013169093
@ -1 +1,2 @@
|
|||||||
alembic
|
alembic
|
||||||
|
sqlalchemy
|
||||||
|
75
src/pyelephant.py
Executable file
75
src/pyelephant.py
Executable 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())
|
Loading…
Reference in New Issue
Block a user