From e821febc9c697688bab5a3b0ed5783fd9eaf1aef Mon Sep 17 00:00:00 2001 From: Dimitris Zlatanidis Date: Mon, 24 Apr 2023 20:32:48 +0300 Subject: [PATCH] Updated for load configs --- slpkg/configs.py | 32 +++++++++----------------------- slpkg/dialog_configs.py | 2 -- 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/slpkg/configs.py b/slpkg/configs.py index 04878868..05ebff1d 100644 --- a/slpkg/configs.py +++ b/slpkg/configs.py @@ -11,23 +11,6 @@ from slpkg.errors_messages import Errors from slpkg.logging_config import LoggingConfig -class Load: - - def __init__(self): - self.errors = Errors() - - def config_file(self, path: Path, file: str) -> dict: # type: ignore - try: - """ Load the configs from the file. """ - config_path_file = Path(path, f'{file}.toml') - if config_path_file.exists(): - with open(config_path_file, 'rb') as conf: - return tomli.load(conf) - except tomli.TOMLDecodeError as error: - pass - self.errors.raise_toml_error_message(error, toml_file='/etc/slpkg/slpkg.toml') - - @dataclass class Configs: """ Default configurations. """ @@ -68,11 +51,14 @@ class Configs: proxy_username: str = '' proxy_password: str = '' - load = Load() - configs = load.config_file(etc_path, prog_name) + try: + config_path_file = Path(etc_path, f'{prog_name}.toml') - if configs: - try: + if config_path_file.exists(): + with open(config_path_file, 'rb') as conf: + configs = tomli.load(conf) + + if configs: config = configs['CONFIGS'] os_arch: str = config['OS_ARCH'] @@ -99,8 +85,8 @@ class Configs: proxy_username: str = config['PROXY_USERNAME'] proxy_password: str = config['PROXY_PASSWORD'] - except KeyError as error: - errors.raise_toml_error_message(error, toml_file='/etc/slpkg/slpkg.toml') + except (KeyError, tomli.TOMLDecodeError) as error: + errors.raise_toml_error_message(error, toml_file='/etc/slpkg/slpkg.toml') # Creating the paths if not exists paths = [ diff --git a/slpkg/dialog_configs.py b/slpkg/dialog_configs.py index b01dbf32..2ce0574e 100644 --- a/slpkg/dialog_configs.py +++ b/slpkg/dialog_configs.py @@ -4,7 +4,6 @@ import os from pathlib import Path -from slpkg.configs import Load from slpkg.configs import Configs from slpkg.utilities import Utilities from slpkg.dialog_box import DialogBox @@ -15,7 +14,6 @@ class FormConfigs(Configs): def __init__(self): super(Configs).__init__() - self.load_configs = Load() self.dialogbox = DialogBox() self.errors = Errors() self.utils = Utilities()