From bf3591ef630358a6990ac36e4a25403ea425075e Mon Sep 17 00:00:00 2001 From: Dimitris Zlatanidis Date: Thu, 16 Mar 2023 22:01:46 +0200 Subject: [PATCH] Updated for error messages --- slpkg/blacklist.py | 2 +- slpkg/checks.py | 9 +++------ slpkg/configs.py | 4 ++-- slpkg/downloader.py | 12 +++--------- slpkg/form_configs.py | 14 +++++--------- slpkg/utilities.py | 11 ++++++++--- 6 files changed, 22 insertions(+), 30 deletions(-) diff --git a/slpkg/blacklist.py b/slpkg/blacklist.py index 51be5202..d3048283 100644 --- a/slpkg/blacklist.py +++ b/slpkg/blacklist.py @@ -31,7 +31,7 @@ class Blacklist(Configs): with open(self.blacklist_file_toml, 'rb') as black: return tomli.load(black)['BLACKLIST']['PACKAGES'] except (tomli.TOMLDecodeError, KeyError) as error: - raise SystemExit(f"\n[{self.bred}Error{self.endc}]: {error}: in the configuration file " + raise SystemExit(f"\n{self.prog_name} {self.bred}Error{self.endc}: {error}: in the configuration file " f"'/etc/slpkg/blacklist.toml'.\n" f"\nIf you have upgraded the '{self.prog_name}' probably you need to run:\n" f"'mv {self.etc_path}/blacklist.toml.new {self.etc_path}/blacklist.toml'.\n" diff --git a/slpkg/checks.py b/slpkg/checks.py index ac2af2ab..797353b0 100644 --- a/slpkg/checks.py +++ b/slpkg/checks.py @@ -43,8 +43,7 @@ class Check(Configs): not_packages.append(sbo) if not_packages: - raise SystemExit(f"\n[{self.bred}Error{self.endc}]: Packages " - f"'{self.cyan}{', '.join(not_packages)}{self.endc}' does not exists.\n") + self.utils.raise_error_message(f"Packages '{', '.join(not_packages)}' does not exists") def is_package_unsupported(self, slackbuilds: list) -> None: """ Checking for unsupported slackbuilds. """ @@ -52,8 +51,7 @@ class Check(Configs): sources = SBoQueries(sbo).sources() if 'UNSUPPORTED' in sources: - raise SystemExit(f"\n[{self.bred}Error{self.endc}]: Package " - f"'{self.cyan}{sbo}{self.endc}' unsupported by arch.\n") + self.utils.raise_error_message(f"Package '{sbo}' unsupported by arch") def is_installed(self, slackbuilds: list, file_pattern: str) -> None: """ Checking for installed packages. """ @@ -65,8 +63,7 @@ class Check(Configs): not_found.append(sbo) if not_found: - raise SystemExit(f'\n[{self.bred}Error{self.endc}]: Not found \'{", ".join(not_found)}\' ' - 'installed packages.\n') + self.utils.raise_error_message(f'Not found \'{", ".join(not_found)}\' installed packages') def is_blacklist(self, slackbuilds: list) -> None: """ Checking if the packages are blacklisted. """ diff --git a/slpkg/configs.py b/slpkg/configs.py index c30fffb7..8313697c 100644 --- a/slpkg/configs.py +++ b/slpkg/configs.py @@ -25,7 +25,7 @@ class Load: with open(config_path_file, 'rb') as conf: return tomli.load(conf) except tomli.TOMLDecodeError as error: - raise SystemExit(f"\n[{self.bred}Error{self.endc}]: {error}: in the configuration file " + raise SystemExit(f"\nslpkg: {self.bred}Error{self.endc}: {error}: in the configuration file " "'/etc/slpkg/slpkg.toml'\n") @@ -216,7 +216,7 @@ class Configs: spinner_color: str = config['SPINNER_COLOR'] except KeyError as error: - raise SystemExit(f"\n[{color['bold']}{color['red']}Error{color['endc']}]: " + 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" diff --git a/slpkg/downloader.py b/slpkg/downloader.py index a10e2326..9f2a30c5 100644 --- a/slpkg/downloader.py +++ b/slpkg/downloader.py @@ -18,14 +18,8 @@ class Downloader(Configs): self.urls: list = urls self.flags: list = flags - self.color = self.colour() self.utils = Utilities() - self.bold: str = self.color['bold'] - self.cyan: str = self.color['cyan'] - self.red: str = self.color['red'] - self.endc: str = self.color['endc'] - self.bred: str = f'{self.bold}{self.red}' self.flag_parallel: list = ['-P', '--parallel'] def download(self): @@ -46,6 +40,7 @@ class Downloader(Configs): def tools(self, url: str) -> None: """ Downloader tools wget, curl and lftp. """ + command: str = '' filename: str = url.split('/')[-1] if self.downloader == 'wget': @@ -58,7 +53,7 @@ class Downloader(Configs): command: str = f'lftp {self.lftp_get_options} {url} -o {self.path}' else: - raise SystemExit(f"{self.red}Error:{self.endc} Downloader '{self.downloader}' not supported.\n") + self.utils.raise_error_message(f"Downloader '{self.downloader}' not supported") self.utils.process(command) self.check_if_downloaded(url) @@ -70,5 +65,4 @@ class Downloader(Configs): path_file = Path(self.path, file) if not path_file.exists(): - raise SystemExit(f"\n[{self.bred}FAILED{self.endc}]: Download the '{self.cyan}{file}{self.endc}' " - f"file.\n") + self.utils.raise_error_message(f"Download the '{file}' file") diff --git a/slpkg/form_configs.py b/slpkg/form_configs.py index 4e02f114..648a88da 100644 --- a/slpkg/form_configs.py +++ b/slpkg/form_configs.py @@ -4,8 +4,9 @@ import os from pathlib import Path -from slpkg.configs import Configs from slpkg.configs import Load +from slpkg.configs import Configs +from slpkg.utilities import Utilities from slpkg.dialog_box import DialogBox @@ -15,21 +16,16 @@ class FormConfigs(Configs): super(Configs).__init__() self.load_configs = Load() self.dialogbox = DialogBox() - self.color = self.colour() + self.utils = Utilities() self.orig_configs: list = [] self.config_file = Path(self.etc_path, f'{self.prog_name}.toml') - self.bold: str = self.color['bold'] - self.red: str = self.color['red'] - self.endc: str = self.color['endc'] - self.bred: str = f'{self.bold}{self.red}' - def is_dialog_enabled(self): """ Checking if the dialog box is enabled by the user. """ if not self.dialog: - raise SystemExit(f"\n[{self.bred}Error{self.endc}]: You should enable the dialog " - "in the '/etc/slpkg/slpkg.toml' file.\n") + self.utils.raise_error_message(f"You should enable the dialog " + f"in the '{self.etc_path}/{self.prog_name}.toml' file") def edit(self) -> None: """ Read and write the configuration file. """ diff --git a/slpkg/utilities.py b/slpkg/utilities.py index 65e2f137..a4fd1ce7 100644 --- a/slpkg/utilities.py +++ b/slpkg/utilities.py @@ -23,9 +23,12 @@ class Utilities: self.color = self.colors() self.black = Blacklist() + self.bold: str = self.color['bold'] self.yellow: str = self.color['yellow'] self.cyan: str = self.color['cyan'] self.endc: str = self.color['endc'] + self.red: str = self.color['red'] + self.bred: str = f'{self.bold}{self.red}' def is_package_installed(self, name: str, pattern: str) -> str: """ Returns the installed package name. """ @@ -143,8 +146,7 @@ class Utilities: """ Checking for flags. """ return [f for f in flag if f in flags] - @staticmethod - def read_packages_from_file(file: Path) -> Generator: + def read_packages_from_file(self, file: Path) -> Generator: """ Reads packages from file and split these to list. """ try: @@ -159,7 +161,7 @@ class Utilities: yield package except FileNotFoundError as err: - raise SystemExit(f'Error: {err}') + self.raise_error_message(str(err)) @staticmethod def read_file(file: Union[str, Path]) -> list: @@ -176,3 +178,6 @@ class Utilities: raise SystemExit(output) except KeyboardInterrupt: raise SystemExit(1) + + def raise_error_message(self, message: str) -> None: + raise SystemExit(f"\n{self.configs.prog_name}: {self.bred}Error{self.endc}: {message}.\n")