db_interactor/config.py

96 lines
2.3 KiB
Python

from enum import Enum
import toml
from dataclasses import dataclass
class DatabaseType(Enum):
PSQL = 1
ORCL = 2
@dataclass
class DatabaseConfig:
_host: str
_database_type: DatabaseType | None
_database_name: str
_database_ssid: str
_database_port: str
def __init__(self, config: dict):
self._host = config["HOST"]
type = config["DATABASE_TYPE"]
match type:
case 'PSQL':
self._database_type = DatabaseType.PSQL
case 'ORCL':
self._database_type = DatabaseType.ORCL
case _:
self._database_type = None
self._database_name = config["DATABASE_NAME"]
self._database_ssid = config["DATABASE_SSID"]
self._database_port = config["DATABASE_PORT"]
@property
def host(self) -> str:
return self._host
@property
def type(self) -> DatabaseType:
return self._database_type
@property
def name(self) -> str:
return self._database_name
@property
def ssid(self) -> str:
return self._database_ssid
@property
def port(self) -> str:
return self._database_port
@dataclass
class KeePassConfig:
_path: str
_db_credentials_name: str
_db_credentials_group: str
def __init__(self, config: dict):
self._path: str = config["PATH"]
self._db_credentials_name: str = config["DB_CREDENTIALS_NAME"]
self._db_credentials_group: str = config["DB_CREDENTIALS_GROUP"]
@property
def path(self) -> str:
return self._path
@property
def db_credentials_name(self) -> str:
return self._db_credentials_name
@property
def db_credentials_group(self) -> str:
return self._db_credentials_group
@dataclass
class Config:
_config: dict
_kee_pass_config: KeePassConfig
_database_config: DatabaseConfig
def __init__(self):
with open('config.toml', 'r') as f:
self._config: dict = toml.load(f)
self._kee_pass_config: KeePassConfig = KeePassConfig(self._config['keepass'])
self._database_config: DatabaseConfig = DatabaseConfig(self._config['database'])
@property
def kee_pass(self) -> KeePassConfig:
return self._kee_pass_config
@property
def database(self) -> DatabaseConfig:
return self._database_config