forked from FanFicDev/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlite_oil.py
51 lines (38 loc) · 974 Bytes
/
lite_oil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from typing import Dict, Optional
import os
import psycopg2
__conns: Dict[str, 'psycopg2.connection'] = {}
def getConnectionString() -> str:
connParms: Dict[str, Optional[str]] = {
'dbname': 'hermes', # TODO
'user': None,
'password': None,
'host': None,
'port': None,
}
for key in connParms:
envKey = f'OIL_DB_{key.upper()}'
if envKey not in os.environ:
continue
connParms[key] = os.environ[envKey]
connStr = ' '.join(
[f'{k}={v}' for k, v in connParms.items() if v is not None]
)
return connStr
def getConnection(subDB: str) -> 'psycopg2.connection':
global __conns
if subDB in __conns:
return __conns[subDB]
conn = psycopg2.connect(getConnectionString())
__conns[subDB] = conn
return conn
def commit() -> None:
global __conns
for subDB in __conns:
__conns[subDB].commit()
def shutdown() -> None:
global __conns
for subDB in list(__conns.keys()):
__conns[subDB].commit()
__conns[subDB].close()
del __conns[subDB]