diff --git a/ChangeLog.txt b/ChangeLog.txt index e14d1d8e..1fb87d02 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,3 +1,7 @@ +4.4.1 - 28/12/2022 +Added: +- configs command to read and edit configuration file + 4.4.0 - 23/12/2022 Added: - New command to tracking the dependencies diff --git a/man/slpkg.1 b/man/slpkg.1 index 93e0aef7..3e409ed4 100644 --- a/man/slpkg.1 +++ b/man/slpkg.1 @@ -4,7 +4,7 @@ slpkg - [OPTIONS] [COMMAND] .SH SYNAPSES .P -slpkg [-h|-v] [update] [upgrade] [check-updates] [clean-logs] [clean-tmp] [-b, build] [-i, install] [-d, download] +slpkg [-h|-v] [update] [upgrade] [check-updates] [configs] [clean-logs] [clean-tmp] [-b, build] [-i, install] [-d, download] [-r, remove] [-f, find] [-w, view] [-s, search] [-e, dependees] [-t, tracking] --yes, --jobs, --resolve-off, --reinstall, --skip-installed, --full-reverse, --search .SH DESCRIPTION @@ -32,6 +32,11 @@ check-updates Check if there is any news on the SlackBuild's ChangeLog.txt file. .RE .P +configs +.RS +Edit the configuration /etc/slpkg/slpkg.toml file. +.RE +.P clean-logs .RS Cleans dependencies log tracking. After that procedure you should remove dependencies by hand. diff --git a/slpkg/configs.py b/slpkg/configs.py index 18efcb80..5b477def 100644 --- a/slpkg/configs.py +++ b/slpkg/configs.py @@ -57,10 +57,10 @@ class Configs: # Overwrite with user configuration. config_file: str = f'{etc_path}/{prog_name}.toml' if os.path.isfile(config_file): - with open(config_file, 'rb') as conf: - configs = tomli.load(conf) - try: + with open(config_file, 'rb') as conf: + configs = tomli.load(conf) + config = configs['configs'] # OS architecture by default @@ -96,8 +96,8 @@ class Configs: # Dialog utility dialog: str = config['dialog'] - except KeyError as error: - print(f"Error: {error}: in the configuration file " + except (KeyError, tomli.TOMLDecodeError) as error: + raise SystemExit(f"\nError: {error}: in the configuration file " "'/etc/slpkg/slpkg.toml'\n") # Creating the paths if not exists diff --git a/slpkg/dialog_box.py b/slpkg/dialog_box.py index 9e976b4a..658208bc 100644 --- a/slpkg/dialog_box.py +++ b/slpkg/dialog_box.py @@ -1,6 +1,8 @@ #!/usr/bin/python3 # -*- coding: utf-8 -*- +import os +import tomli import locale from dialog import Dialog @@ -28,3 +30,47 @@ class DialogBox: tags = packages return code, tags + + def form_configs(self): + """ Read and write the configuration file. """ + elements = [] + config_file: str = f'{self.configs.etc_path}/{self.configs.prog_name}.toml' + + if os.path.isfile(config_file): + # Load the toml config file. + try: + with open(config_file, 'rb') as conf: + configs = tomli.load(conf) + except (KeyError, tomli.TOMLDecodeError) as error: + raise SystemExit(f"\nError: {error}: in the configuration file " + "'/etc/slpkg/slpkg.toml'\n") + + # Creating the elements for the dialog form. + for i, (key, value) in enumerate(configs['configs'].items(), start=1): + if value is True: + value = 'true' + elif value is False: + value = 'false' + elements += [ + (key, i, 1, value, i, 17, 47, 200, '0x0') + ] + text = f'Edit the configuration file: {config_file}.' + title = ' Configuration File ' + code, tags = self.d.mixedform(text=text, title=title, elements=elements, + height=30, width=70) + os.system('clear') + + if code == 'ok': + # Read the original config file. + with open(config_file, 'r') as toml_file: + orig_config = toml_file.readlines() + + # Write the new values to the config file. + with open(config_file, 'w') as patch_toml: + for line in orig_config: + for key, value in zip(configs['configs'].keys(), tags): + if line.lstrip().startswith(key): + line = f' {key} = "{value}"\n' + if line.lstrip().startswith(('colors =', 'dialog =')): + line = line.replace('"', '') + patch_toml.write(line) diff --git a/slpkg/main.py b/slpkg/main.py index 035fb324..f8a03751 100644 --- a/slpkg/main.py +++ b/slpkg/main.py @@ -185,6 +185,35 @@ class Argparse: raise SystemExit() self.usage.help(1) + def edit_configs(self): + if len(self.args) == 1 and not self.flags: + self.dialog.form_configs() + raise SystemExit() + self.usage.help(1) + + def clean_logs(self): + if [f for f in self.flags if f not in [self.flag_yes]]: + self.usage.help(1) + + if len(self.args) == 1: + self.check.database() + + logs = CleanLogsDependencies(self.flags) + logs.clean() + raise SystemExit() + self.usage.help(1) + + def clean_tmp(self): + if len(self.args) == 1 and not self.flags: + path = self.configs.tmp_path + tmp_slpkg = self.configs.tmp_slpkg + folder = self.configs.prog_name + + self.utils.remove_folder_if_exists(path, folder) + self.utils.create_folder(tmp_slpkg, 'build') + raise SystemExit() + self.usage.help(1) + def build(self): if [f for f in self.flags if f not in [self.flag_yes, self.flag_jobs, @@ -360,29 +389,6 @@ class Argparse: raise SystemExit() self.usage.help(1) - def clean_logs(self): - if [f for f in self.flags if f not in [self.flag_yes]]: - self.usage.help(1) - - if len(self.args) == 1: - self.check.database() - - logs = CleanLogsDependencies(self.flags) - logs.clean() - raise SystemExit() - self.usage.help(1) - - def clean_tmp(self): - if len(self.args) == 1 and not self.flags: - path = self.configs.tmp_path - tmp_slpkg = self.configs.tmp_slpkg - folder = self.configs.prog_name - - self.utils.remove_folder_if_exists(path, folder) - self.utils.create_folder(tmp_slpkg, 'build') - raise SystemExit() - self.usage.help(1) - def main(): args = sys.argv @@ -398,6 +404,7 @@ def main(): 'update': argparse.update, 'upgrade': argparse.upgrade, 'check-updates': argparse.check_updates, + 'configs': argparse.edit_configs, 'clean-logs': argparse.clean_logs, 'clean-tmp': argparse.clean_tmp, 'build': argparse.build, diff --git a/slpkg/utilities.py b/slpkg/utilities.py index eb6db319..4d7e2b3c 100644 --- a/slpkg/utilities.py +++ b/slpkg/utilities.py @@ -3,6 +3,7 @@ import os +import tomli import shutil import tarfile @@ -61,3 +62,4 @@ class Utilities: tag = ''.join(package[len(name + version + arch + build) + 4:].split('-')) return [name, version, arch, build, tag] + diff --git a/slpkg/views/cli_menu.py b/slpkg/views/cli_menu.py index 9e39d341..a07c3552 100644 --- a/slpkg/views/cli_menu.py +++ b/slpkg/views/cli_menu.py @@ -23,7 +23,8 @@ class Usage: f'Usage: {Configs.prog_name} [{self.yellow}OPTIONS{self.endc}] [{self.cyan}COMMAND{self.endc}] \n' f'\n slpkg [{self.yellow}OPTIONS{self.endc}] [--yes, --jobs, --resolve-off, --reinstall]\n' f' slpkg [{self.yellow}OPTIONS{self.endc}] [--skip-installed, --full-reverse, --search]\n' - f' slpkg [{self.cyan}COMMAND{self.endc}] [update, upgrade, check-updates, clean-logs, clean-tmp]\n' + f' slpkg [{self.cyan}COMMAND{self.endc}] [update, upgrade, check-updates, configs]\n' + f' slpkg [{self.cyan}COMMAND{self.endc}] [clean-logs, clean-tmp]\n' f' slpkg [{self.cyan}COMMAND{self.endc}] [-b, build, -i, install, -d, download, -r, remove] \n' f' slpkg [{self.cyan}COMMAND{self.endc}] [-f, find, -w, view, -s, search, -e, dependees] \n' f' slpkg [{self.cyan}COMMAND{self.endc}] [-t, tracking] \n' @@ -42,6 +43,7 @@ class Usage: f' {self.red}update{self.endc} Update the package lists.\n' f' {self.cyan}upgrade{self.endc} Upgrade all the packages.\n' f' {self.cyan}check-updates{self.endc} Check for news on ChangeLog.txt.\n' + f' {self.cyan}configs{self.endc} Edit the configuration file.\n' f' {self.cyan}clean-logs{self.endc} Clean dependencies log tracking.\n' f' {self.cyan}clean-tmp{self.endc} Delete all the downloaded sources.\n' f' {self.cyan}-b, build{self.endc} Build only the packages.\n'