50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
#!/usr/bin/python3
|
|
|
|
from os import environ
|
|
from os.path import exists
|
|
from json import load
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
|
|
def import_config():
|
|
try:
|
|
path = environ['BOT_CONFIG_PATH']
|
|
except:
|
|
path = 'config/config.json'
|
|
if(exists(path)):
|
|
try:
|
|
with open(path) as config_file:
|
|
config_dict = load(config_file)
|
|
except Exception as e:
|
|
print(f'There was some issue opening and loading the config.\n{e}')
|
|
exit(1)
|
|
else:
|
|
print('Didn\'t find the config file.')
|
|
exit(1)
|
|
return config_dict
|
|
|
|
|
|
def get_engine():
|
|
config_data = import_config()
|
|
try:
|
|
with open(config_data['postgres_pass']) as pgdb_pass:
|
|
engine = create_engine(
|
|
f"postgresql+psycopg2://{config_data['postgres_user']}:{pgdb_pass.readline().strip()}@{config_data['postgres_host']}:{config_data['postgres_port']}/{config_data['postgres_db']}")
|
|
return engine
|
|
except Exception as e:
|
|
print(
|
|
f'There was an issue opening the config file for the postgres password.\n{e}')
|
|
exit(1)
|
|
|
|
|
|
def create_session(Base):
|
|
try:
|
|
engine = get_engine()
|
|
Base.metadata.bind = engine
|
|
covidDataSession = sessionmaker(bind=engine)
|
|
except Exception as e:
|
|
print(
|
|
f'There was an issue creating a session for the database.\n{e}')
|
|
exit(1)
|
|
return covidDataSession()
|