1
|
1 |
|
from configparser import ConfigParser |
2
|
1 |
|
from typing import Dict, Optional, Union |
3
|
|
|
|
4
|
1 |
|
from pystratum_backend.StratumStyle import StratumStyle |
5
|
|
|
|
6
|
1 |
|
from pystratum_pgsql.PgSqlDefaultConnector import PgSqlDefaultConnector |
7
|
1 |
|
from pystratum_pgsql.PgSqlMetadataDataLayer import PgSqlMetadataDataLayer |
8
|
|
|
|
9
|
|
|
|
10
|
1 |
|
class PgSqlWorker: |
11
|
|
|
""" |
12
|
|
|
Class for connecting to PostgreSQL instances and reading PostgreSQL specific connection parameters from |
13
|
|
|
configuration files. |
14
|
|
|
""" |
15
|
|
|
|
16
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
17
|
1 |
|
def __init__(self, io: StratumStyle, config: ConfigParser): |
18
|
|
|
""" |
19
|
|
|
Object constructor. |
20
|
|
|
|
21
|
|
|
:param PyStratumStyle io: The output decorator. |
22
|
|
|
""" |
23
|
|
|
|
24
|
1 |
|
self._io: StratumStyle = io |
25
|
|
|
""" |
26
|
|
|
The output decorator. |
27
|
|
|
""" |
28
|
|
|
|
29
|
1 |
|
self._config: ConfigParser = config |
30
|
|
|
""" |
31
|
|
|
The configuration object. |
32
|
|
|
""" |
33
|
|
|
|
34
|
1 |
|
self._dl = PgSqlMetadataDataLayer(io, PgSqlDefaultConnector(self.__read_configuration_file())) |
35
|
1 |
|
""" |
36
|
|
|
The metadata layer. |
37
|
|
|
""" |
38
|
|
|
|
39
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
40
|
1 |
|
def connect(self) -> None: |
41
|
|
|
""" |
42
|
|
|
Connects to the PostgreSQL instance. |
43
|
|
|
""" |
44
|
1 |
|
self._dl.connect() |
45
|
|
|
|
46
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
47
|
1 |
|
def disconnect(self) -> None: |
48
|
|
|
""" |
49
|
|
|
Disconnects from the PostgreSQL instance. |
50
|
|
|
""" |
51
|
1 |
|
self._dl.disconnect() |
52
|
|
|
|
53
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
54
|
1 |
|
def __read_configuration_file(self) -> Dict[str, Union[str, int]]: |
55
|
|
|
""" |
56
|
|
|
Reads connections parameters from the configuration file. |
57
|
|
|
""" |
58
|
1 |
|
return {'host': self.__get_option(self._config, 'database', 'host', fallback='localhost'), |
59
|
|
|
'user': self.__get_option(self._config, 'database', 'user'), |
60
|
|
|
'password': self.__get_option(self._config, 'database', 'password'), |
61
|
|
|
'database': self.__get_option(self._config, 'database', 'database'), |
62
|
|
|
'schema': self.__get_option(self._config, 'database', 'schema'), |
63
|
|
|
'port': int(self.__get_option(self._config, 'database', 'port', fallback='5432'))} |
64
|
|
|
|
65
|
|
|
# ------------------------------------------------------------------------------------------------------------------ |
66
|
1 |
|
@staticmethod |
67
|
1 |
|
def __get_option(config: ConfigParser, |
68
|
|
|
section: str, |
69
|
|
|
option: str, |
70
|
|
|
fallback: Optional[str] = None) -> str: |
71
|
|
|
""" |
72
|
|
|
Reads an option for a configuration file. |
73
|
|
|
|
74
|
|
|
:param configparser.ConfigParser config: The main config file. |
75
|
|
|
:param str section: The name of the section op the option. |
76
|
|
|
:param str option: The name of the option. |
77
|
|
|
:param str|None fallback: The fallback value of the option if it is not set in either configuration files. |
78
|
|
|
|
79
|
|
|
:rtype: str |
80
|
|
|
|
81
|
|
|
:raise KeyError: |
82
|
|
|
""" |
83
|
1 |
|
return_value = config.get(section, option, fallback=fallback) |
84
|
|
|
|
85
|
1 |
|
if fallback is None and return_value is None: |
86
|
|
|
raise KeyError("Option '{0!s}' is not found in section '{1!s}'.".format(option, section)) |
87
|
|
|
|
88
|
1 |
|
return return_value |
89
|
|
|
|
90
|
|
|
# ---------------------------------------------------------------------------------------------------------------------- |
91
|
|
|
|