From 13e135491caf1003c2a52a0dec7835e81492d03c Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Sun, 14 Jun 2020 09:48:02 +1000 Subject: [PATCH] Move database loading into the db module --- src/edible_elephant/cli.py | 6 +----- src/edible_elephant/db.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/edible_elephant/cli.py b/src/edible_elephant/cli.py index 010c714..6f9a90e 100755 --- a/src/edible_elephant/cli.py +++ b/src/edible_elephant/cli.py @@ -121,11 +121,7 @@ if __name__ == '__main__': 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}") - - session = sa.orm.Session(db) + session = db.load_db() # logging.basicConfig() # logger = logging.getLogger('sqlalchemy.engine') diff --git a/src/edible_elephant/db.py b/src/edible_elephant/db.py index a440cf4..54da5d9 100644 --- a/src/edible_elephant/db.py +++ b/src/edible_elephant/db.py @@ -2,6 +2,8 @@ import sqlalchemy as sa import sqlalchemy.ext.declarative.base import sqlalchemy.orm +import os.path + ############################################################################### Base = sqlalchemy.ext.declarative.declarative_base() @@ -32,3 +34,12 @@ class Task(Base): secondaryjoin=id == depends_association.c.second_id, backref="parents" ) + + +############################################################################### +def load_db() -> sa.orm.Session: + 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}") + + return sa.orm.Session(db)