Skip to content
This repository was archived by the owner on Jul 2, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions oil_library/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import os
import sys
import logging
import logging; logger = logging.getLogger(__name__)

from pkg_resources import get_distribution

Expand All @@ -21,11 +21,16 @@
#
# currently, the DB is created and located when package is installed
#
_oillib_path = os.path.dirname(__file__)
__module_folder__ = __file__.split(os.sep)[-2]
_db_file = 'OilLib.db'
_db_file_path = os.path.join(_oillib_path, _db_file)
def get_oil_db_path():
"""
Get the path to the Sqlite3 oil database
"""
from pkg_resources import resource_filename, resource_exists

if not resource_exists(__name__, 'OilLib.db'):
logger.warning('OilLib.db does not exist')

return resource_filename(__name__, 'OilLib.db')

def _get_db_session():
'we can call this from scripts to access valid DBSession'
Expand All @@ -35,12 +40,11 @@ def _get_db_session():
try:
eng = session.get_bind()

if eng.url.database.split(os.path.sep)[-2:] != [__module_folder__,
_db_file]:
if os.path.realpath(eng.url.database) != os.path.realpath(get_oil_db_path()):
raise UnboundExecutionError

except UnboundExecutionError:
session.bind = create_engine('sqlite:///' + _db_file_path)
session.bind = create_engine('sqlite:///' + get_oil_db_path())

return session

Expand Down
Empty file added oil_library/tests/__init__.py
Empty file.