|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import importlib |
| 16 | +from configparser import ConfigParser, NoOptionError, NoSectionError |
16 | 17 | from dataclasses import dataclass |
| 18 | +from pathlib import Path |
17 | 19 | from typing import Any, Dict, Optional |
18 | 20 |
|
19 | | -try: |
20 | | - import ConfigParser |
21 | | -except: |
22 | | - import configparser as ConfigParser |
23 | | - |
24 | 21 | from robot.api import logger |
25 | 22 |
|
26 | 23 |
|
@@ -81,6 +78,35 @@ def __iter__(self): |
81 | 78 | return iter(self._connections.values()) |
82 | 79 |
|
83 | 80 |
|
| 81 | +class ConfigReader: |
| 82 | + def __init__(self, config_file: Optional[str], alias: str): |
| 83 | + if config_file is None: |
| 84 | + config_file = "./resources/db.cfg" |
| 85 | + self.alias = alias |
| 86 | + self.config = self._load_config(config_file) |
| 87 | + |
| 88 | + @staticmethod |
| 89 | + def _load_config(config_file: str) -> Optional[ConfigParser]: |
| 90 | + config_path = Path(config_file) |
| 91 | + if not config_path.exists(): |
| 92 | + return None |
| 93 | + config = ConfigParser() |
| 94 | + config.read([config_path]) |
| 95 | + return config |
| 96 | + |
| 97 | + def get(self, param: str) -> str: |
| 98 | + if self.config is None: |
| 99 | + raise ValueError(f"Required '{param}' parameter was not provided in keyword arguments.") from None |
| 100 | + try: |
| 101 | + return self.config.get(self.alias, param) |
| 102 | + except NoSectionError: |
| 103 | + raise ValueError(f"Configuration file does not have [{self.alias}] section.") from None |
| 104 | + except NoOptionError: |
| 105 | + raise ValueError( |
| 106 | + f"Required '{param}' parameter missing in both keyword arguments and configuration file." |
| 107 | + ) from None |
| 108 | + |
| 109 | + |
84 | 110 | class ConnectionManager: |
85 | 111 | """ |
86 | 112 | Connection Manager handles the connection & disconnection to the database. |
@@ -153,18 +179,14 @@ def connect_to_database( |
153 | 179 | | # uses explicit `dbapiModuleName` and `dbName` but uses the `dbUsername` and `dbPassword` in './resources/db.cfg' | |
154 | 180 | | Connect To Database | psycopg2 | my_db_test | |
155 | 181 | """ |
156 | | - |
157 | | - if dbConfigFile is None: |
158 | | - dbConfigFile = "./resources/db.cfg" |
159 | | - config = ConfigParser.ConfigParser() |
160 | | - config.read([dbConfigFile]) |
161 | | - |
162 | | - dbapiModuleName = dbapiModuleName or config.get(alias, "dbapiModuleName") |
163 | | - dbName = dbName or config.get(alias, "dbName") |
164 | | - dbUsername = dbUsername or config.get(alias, "dbUsername") |
165 | | - dbPassword = dbPassword if dbPassword is not None else config.get(alias, "dbPassword") |
166 | | - dbHost = dbHost or config.get(alias, "dbHost") or "localhost" |
167 | | - dbPort = int(dbPort or config.get(alias, "dbPort")) |
| 182 | + config = ConfigReader(dbConfigFile, alias) |
| 183 | + |
| 184 | + dbapiModuleName = dbapiModuleName or config.get("dbapiModuleName") |
| 185 | + dbName = dbName or config.get("dbName") |
| 186 | + dbUsername = dbUsername or config.get("dbUsername") |
| 187 | + dbPassword = dbPassword if dbPassword is not None else config.get("dbPassword") |
| 188 | + dbHost = dbHost or config.get("dbHost") or "localhost" |
| 189 | + dbPort = int(dbPort if dbPort is not None else config.get("dbPort")) |
168 | 190 |
|
169 | 191 | if dbapiModuleName == "excel" or dbapiModuleName == "excelrw": |
170 | 192 | db_api_module_name = "pyodbc" |
|
0 commit comments