Updated for errors messages

This commit is contained in:
Dimitris Zlatanidis 2023-04-21 21:41:19 +03:00
parent 41392eead2
commit 87eda0a82d
3 changed files with 19 additions and 31 deletions

View file

@ -6,6 +6,8 @@ import tomli
import platform import platform
from pathlib import Path from pathlib import Path
from dataclasses import dataclass from dataclasses import dataclass
from slpkg.errors import Errors
from slpkg.logging_config import LoggingConfig from slpkg.logging_config import LoggingConfig
@ -15,6 +17,7 @@ class Load:
bold = '\033[1m' bold = '\033[1m'
red = '\x1b[91m' red = '\x1b[91m'
self.errors = Errors()
self.endc: str = '\x1b[0m' self.endc: str = '\x1b[0m'
self.bred: str = f'{bold}{red}' self.bred: str = f'{bold}{red}'
@ -26,13 +29,13 @@ class Load:
with open(config_path_file, 'rb') as conf: with open(config_path_file, 'rb') as conf:
return tomli.load(conf) return tomli.load(conf)
except tomli.TOMLDecodeError as error: except tomli.TOMLDecodeError as error:
raise SystemExit(f"\nslpkg: {self.bred}Error{self.endc}: {error}: in the configuration file " self.errors.raise_toml_error_message(error, toml_file='/etc/slpkg/slpkg.toml')
"'/etc/slpkg/slpkg.toml'\n")
@dataclass @dataclass
class Configs: class Configs:
""" Default configurations. """ """ Default configurations. """
errors = Errors()
color = { color = {
'bold': '\033[1m', 'bold': '\033[1m',
@ -113,11 +116,7 @@ class Configs:
proxy_password: str = config['PROXY_PASSWORD'] proxy_password: str = config['PROXY_PASSWORD']
except KeyError as error: except KeyError as error:
raise SystemExit(f"\n{prog_name}: {color['bold']}{color['red']}Error{color['endc']}: " errors.raise_toml_error_message(error, toml_file='/etc/slpkg/slpkg.toml')
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")
# Creating the paths if not exists # Creating the paths if not exists
paths = [ paths = [

View file

@ -1,18 +1,16 @@
#!/usr/bin/python3 #!/usr/bin/python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from slpkg.configs import Configs
class Errors:
class Errors(Configs):
def __init__(self): def __init__(self):
self.color = self.colour() self.prog_name: str = 'slpkg'
self.bold: str = self.color['bold'] self.bold: str = '\033[1m'
self.red: str = self.color['red'] self.red: str = '\x1b[91m'
self.cyan: str = self.color['cyan'] self.cyan: str = '\x1b[96m'
self.endc: str = self.color['endc'] self.endc: str = '\x1b[0m'
self.bred: str = f'{self.bold}{self.red}' self.bred: str = f'{self.bold}{self.red}'
def raise_error_message(self, message: str) -> None: 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: def raise_toml_error_message(self, error, toml_file) -> None:
""" A general error message for .toml configs files. """ """ 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 " raise SystemExit(f"\n{self.prog_name} {self.bred}Error{self.endc}: {error}: in the configuration file "
f"'{toml_file}'.\n" f"'{toml_file}', edit the file and check for errors.\n"
f"\nIf you have upgraded the '{self.configs.prog_name}' probably you need to run:\n" f"\nIf you have upgraded the '{self.prog_name}' probably you need to run:\n"
f"'mv {toml_file}.new {toml_file}'.\n" f"\n $ {self.cyan}slpkg_new-configs{self.endc}\n")
f"or '{self.cyan}slpkg_new-configs{self.endc}' command.\n")

View file

@ -6,18 +6,14 @@ import tomli
from pathlib import Path from pathlib import Path
from dataclasses import dataclass from dataclasses import dataclass
from slpkg.errors import Errors
from slpkg.configs import Configs from slpkg.configs import Configs
@dataclass @dataclass
class Repositories: class Repositories:
configs = Configs configs = Configs
color = configs.colour() errors = Errors()
bold: str = color['bold']
red: str = color['red']
cyan: str = color['cyan']
endc: str = color['endc']
bred: str = f'{bold}{red}'
repositories_toml_file: Path = Path(configs.etc_path, 'repositories.toml') repositories_toml_file: Path = Path(configs.etc_path, 'repositories.toml')
repositories_path: Path = Path(configs.lib_path, 'repositories') repositories_path: Path = Path(configs.lib_path, 'repositories')
@ -376,11 +372,7 @@ class Repositories:
slint_repo_path: str = slint_repo_mirror[0][7:] slint_repo_path: str = slint_repo_mirror[0][7:]
except (tomli.TOMLDecodeError, KeyError) as error: except (tomli.TOMLDecodeError, KeyError) as error:
raise SystemExit(f'\n{configs.prog_name} {bred}Error{endc}: {error}: in the configuration file ' errors.raise_toml_error_message(error, repositories_toml_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")
# Default sbo repository configs. # Default sbo repository configs.
repo_tag: str = sbo_repo_tag repo_tag: str = sbo_repo_tag