update bot_core.py for base reaction functionality, create db_helper.py
This commit is contained in:
44
src/helper/db_helper.py
Normal file
44
src/helper/db_helper.py
Normal file
@@ -0,0 +1,44 @@
|
||||
"""
|
||||
database interaction module of the very-bad bot
|
||||
|
||||
@author: Gabriella 'contrastellar' Agathon
|
||||
"""
|
||||
|
||||
import psycopg2
|
||||
import psycopg2.extensions
|
||||
from configparser import ConfigParser
|
||||
import datetime
|
||||
|
||||
|
||||
def load_config(filename='database.ini', section='postgresql'):
|
||||
|
||||
parser = ConfigParser()
|
||||
parser.read(filename)
|
||||
|
||||
# get section, default is postgresql
|
||||
config = {}
|
||||
if parser.has_section(section):
|
||||
params = parser.items(section)
|
||||
for param in params:
|
||||
config[param[0]] = param[1]
|
||||
|
||||
else:
|
||||
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def connect_config(config) -> psycopg2.extensions.connection:
|
||||
try:
|
||||
with psycopg2.connect(**config) as conn:
|
||||
print('connected to the server')
|
||||
return conn
|
||||
|
||||
except (psycopg2.DatabaseError, Exception) as error:
|
||||
print(error)
|
||||
|
||||
finally:
|
||||
if conn is None:
|
||||
raise psycopg2.DatabaseError("Failed to connect!")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user