diff --git a/slpkg/configs.py b/slpkg/configs.py index 001fc260..ca35a535 100644 --- a/slpkg/configs.py +++ b/slpkg/configs.py @@ -6,6 +6,8 @@ import tomli import platform from pathlib import Path from dataclasses import dataclass + +from slpkg.errors import Errors from slpkg.logging_config import LoggingConfig @@ -15,6 +17,7 @@ class Load: bold = '\033[1m' red = '\x1b[91m' + self.errors = Errors() self.endc: str = '\x1b[0m' self.bred: str = f'{bold}{red}' @@ -26,13 +29,13 @@ class Load: with open(config_path_file, 'rb') as conf: return tomli.load(conf) except tomli.TOMLDecodeError as error: - raise SystemExit(f"\nslpkg: {self.bred}Error{self.endc}: {error}: in the configuration file " - "'/etc/slpkg/slpkg.toml'\n") + self.errors.raise_toml_error_message(error, toml_file='/etc/slpkg/slpkg.toml') @dataclass class Configs: """ Default configurations. """ + errors = Errors() color = { 'bold': '\033[1m', @@ -113,11 +116,7 @@ class Configs: proxy_password: str = config['PROXY_PASSWORD'] except KeyError as error: - raise SystemExit(f"\n{prog_name}: {color['bold']}{color['red']}Error{color['endc']}: " - f"{error}: in the configuration file '/etc/slpkg/slpkg.toml'.\n" - f"\nIf you have upgraded the '{prog_name}' probably you need to run:\n" - f"'mv {etc_path}/{prog_name}.toml.new {etc_path}/{prog_name}.toml'.\n" - f"or '{color['cyan']}slpkg_new-configs{color['endc']}' command.\n") + errors.raise_toml_error_message(error, toml_file='/etc/slpkg/slpkg.toml') # Creating the paths if not exists paths = [ diff --git a/slpkg/errors.py b/slpkg/errors.py index 2f9e32c5..ad6a8fac 100644 --- a/slpkg/errors.py +++ b/slpkg/errors.py @@ -1,18 +1,16 @@ #!/usr/bin/python3 # -*- coding: utf-8 -*- -from slpkg.configs import Configs - -class Errors(Configs): +class Errors: def __init__(self): - self.color = self.colour() - self.bold: str = self.color['bold'] - self.red: str = self.color['red'] - self.cyan: str = self.color['cyan'] - self.endc: str = self.color['endc'] + self.prog_name: str = 'slpkg' + self.bold: str = '\033[1m' + self.red: str = '\x1b[91m' + self.cyan: str = '\x1b[96m' + self.endc: str = '\x1b[0m' self.bred: str = f'{self.bold}{self.red}' def raise_error_message(self, message: str) -> None: @@ -21,8 +19,7 @@ class Errors(Configs): def raise_toml_error_message(self, error, toml_file) -> None: """ A general error message for .toml configs files. """ - raise SystemExit(f"\n{self.configs.prog_name} {self.bred}Error{self.endc}: {error}: in the configuration file " - f"'{toml_file}'.\n" - f"\nIf you have upgraded the '{self.configs.prog_name}' probably you need to run:\n" - f"'mv {toml_file}.new {toml_file}'.\n" - f"or '{self.cyan}slpkg_new-configs{self.endc}' command.\n") + raise SystemExit(f"\n{self.prog_name} {self.bred}Error{self.endc}: {error}: in the configuration file " + f"'{toml_file}', edit the file and check for errors.\n" + f"\nIf you have upgraded the '{self.prog_name}' probably you need to run:\n" + f"\n $ {self.cyan}slpkg_new-configs{self.endc}\n") diff --git a/slpkg/repositories.py b/slpkg/repositories.py index 4659bd86..6cad5f03 100644 --- a/slpkg/repositories.py +++ b/slpkg/repositories.py @@ -6,18 +6,14 @@ import tomli from pathlib import Path from dataclasses import dataclass +from slpkg.errors import Errors from slpkg.configs import Configs @dataclass class Repositories: configs = Configs - color = configs.colour() - bold: str = color['bold'] - red: str = color['red'] - cyan: str = color['cyan'] - endc: str = color['endc'] - bred: str = f'{bold}{red}' + errors = Errors() repositories_toml_file: Path = Path(configs.etc_path, 'repositories.toml') repositories_path: Path = Path(configs.lib_path, 'repositories') @@ -376,11 +372,7 @@ class Repositories: slint_repo_path: str = slint_repo_mirror[0][7:] except (tomli.TOMLDecodeError, KeyError) as error: - raise SystemExit(f'\n{configs.prog_name} {bred}Error{endc}: {error}: in the configuration file ' - f"'{repositories_toml_file}'.\n" - f"\nIf you have upgraded the '{configs.prog_name}' probably you need to run:\n" - f"'mv {repositories_toml_file}.new {repositories_toml_file}'.\n" - f"or '{cyan}slpkg_new-configs{endc}' command.\n") + errors.raise_toml_error_message(error, repositories_toml_file) # Default sbo repository configs. repo_tag: str = sbo_repo_tag