1 from pathlib import Path
7 def __init__(self, cls):
13 except AttributeError:
14 self._instance = self._cls()
18 raise TypeError('Singletons must be accessed through `Instance()`.')
20 def __instancecheck__(self, inst):
21 return isinstance(inst, self._cls)
24 class PostgreSQLDBConnection(object):
25 """Postgresql database connection"""
27 def __init__(self, connection_string = ''):
28 if connection_string == '':
29 # We're retrieving information related to the database in config.ini
30 config = configparser.ConfigParser()
31 config.read((Path.cwd() / 'config') / 'main.cfg')
33 host = config['postgresql']['host']
34 user = config['postgresql']['user']
35 port = config['postgresql']['port']
36 self.dbname = config['postgresql']['dbname']
38 self.connection_string = f"host={host} port={port} dbname={self.dbname} user={user}"
41 self.connection_string = connection_string
46 self.connection = psycopg2.connect(self.connection_string)
47 self.connection.autocommit = True
48 self.cursor = self.connection.cursor()
56 return 'Database connection object'
58 def __exit__(self, exc_type, exc_val, exc_tb):
59 #self.connection.commit()
61 self.connection.close()