-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
63 lines (51 loc) · 1.84 KB
/
Copy pathcommon.py
File metadata and controls
63 lines (51 loc) · 1.84 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
from configparser import ConfigParser
from sqlalchemy import create_engine
def get_sql_engine(config_file):
"""
Function parse configuration file and returns sqlalchemy engine for
connecting to postgres database
Parameters
----------
config_file: str
Path to config file with the following structure:
[Postgres]
username = <USERNAME>
password = <PASSWORD>
host = <HOST>
port = <PORT>
database = <DATABASE_NAME>
Return
------
sqlalchemy.Engine instance
"""
CONNECTION_OPTIONS_LIST = [
'host',
'port',
'database',
'username',
'password'
]
cfg_parser = ConfigParser()
read_configs = cfg_parser.read(config_file)
if not read_configs:
raise FileNotFoundError(f"No such file: {config_file}")
if cfg_parser.has_section('Postgres'):
if not set(CONNECTION_OPTIONS_LIST).issubset(
cfg_parser.options('Postgres')):
raise ValueError(
"Connection options not found in config file in"
" Postgres section. Check structure of config_example.ini"
)
else:
pg_username = cfg_parser['Postgres']['Username']
pg_password = cfg_parser['Postgres']['Password']
pg_host = cfg_parser['Postgres']['Host']
pg_port = cfg_parser['Postgres']['Port']
pg_database = cfg_parser['Postgres']['Database']
return create_engine(
f'postgresql+psycopg2://{pg_username}:{pg_password}@'
f'{pg_host}:{pg_port}/{pg_database}'
)
else:
raise ValueError("Postgres section not found in config file. Check "
"structure of config in config_example.ini")