From 302a404796668d3f1d8393c5bc79e15321552740 Mon Sep 17 00:00:00 2001 From: Dimitris Zlatanidis Date: Fri, 15 Mar 2024 21:15:29 +0200 Subject: [PATCH] Updated for read json --- slpkg/install_data.py | 40 ++++++++++++++++++++-------------------- slpkg/main.py | 4 ++-- slpkg/utilities.py | 17 +++++++++++++++++ 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/slpkg/install_data.py b/slpkg/install_data.py index fe6480f8..e2006106 100644 --- a/slpkg/install_data.py +++ b/slpkg/install_data.py @@ -109,7 +109,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.sbo_repo_path, self.repos.sbo_repo_changelog) self.write_last_update(path_changelog, self.repos.sbo_repo_name) - data_file: Path = Path(self.repos.sbo_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.sbo_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -188,7 +188,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.ponce_repo_path, self.repos.ponce_repo_changelog) self.write_last_update(path_changelog, self.repos.ponce_repo_name) - data_file: Path = Path(self.repos.ponce_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.ponce_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -283,7 +283,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.slack_repo_path, self.repos.slack_repo_changelog) self.write_last_update(path_changelog, self.repos.slack_repo_name) - data_file: Path = Path(self.repos.slack_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.slack_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -378,7 +378,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.slack_extra_repo_path, self.repos.slack_extra_repo_changelog) self.write_last_update(path_changelog, self.repos.slack_extra_repo_name) - data_file: Path = Path(self.repos.slack_extra_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.slack_extra_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -473,7 +473,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.slack_patches_repo_path, self.repos.slack_patches_repo_changelog) self.write_last_update(path_changelog, self.repos.slack_patches_repo_name) - data_file: Path = Path(self.repos.slack_patches_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.slack_patches_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -573,7 +573,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.alien_repo_path, self.repos.alien_repo_changelog) self.write_last_update(path_changelog, self.repos.alien_repo_name) - data_file: Path = Path(self.repos.alien_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.alien_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -668,7 +668,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.multilib_repo_path, self.repos.multilib_repo_changelog) self.write_last_update(path_changelog, self.repos.multilib_repo_name) - data_file: Path = Path(self.repos.multilib_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.multilib_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -763,7 +763,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.restricted_repo_path, self.repos.restricted_repo_changelog) self.write_last_update(path_changelog, self.repos.restricted_repo_name) - data_file: Path = Path(self.repos.restricted_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.restricted_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -859,7 +859,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.gnome_repo_path, self.repos.gnome_repo_changelog) self.write_last_update(path_changelog, self.repos.gnome_repo_name) - data_file: Path = Path(self.repos.gnome_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.gnome_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -954,7 +954,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.msb_repo_path, self.repos.msb_repo_changelog) self.write_last_update(path_changelog, self.repos.msb_repo_name) - data_file: Path = Path(self.repos.msb_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.msb_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -1049,7 +1049,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.csb_repo_path, self.repos.csb_repo_changelog) self.write_last_update(path_changelog, self.repos.csb_repo_name) - data_file: Path = Path(self.repos.csb_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.csb_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -1145,7 +1145,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.conraid_repo_path, self.repos.conraid_repo_changelog) self.write_last_update(path_changelog, self.repos.conraid_repo_name) - data_file: Path = Path(self.repos.conraid_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.conraid_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -1245,7 +1245,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.slackdce_repo_path, self.repos.slackdce_repo_changelog) self.write_last_update(path_changelog, self.repos.conraid_repo_name) - data_file: Path = Path(self.repos.slackdce_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.slackdce_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -1345,7 +1345,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.slackonly_repo_path, self.repos.slackonly_repo_changelog) self.write_last_update(path_changelog, self.repos.slackonly_repo_name) - data_file: Path = Path(self.repos.slackonly_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.slackonly_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -1453,7 +1453,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.salixos_repo_path, self.repos.salixos_repo_changelog) self.write_last_update(path_changelog, self.repos.salixos_repo_name) - data_file: Path = Path(self.repos.salixos_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.salixos_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -1561,7 +1561,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.salixos_extra_repo_path, self.repos.salixos_extra_repo_changelog) self.write_last_update(path_changelog, self.repos.salixos_extra_repo_name) - data_file: Path = Path(self.repos.salixos_extra_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.salixos_extra_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -1672,7 +1672,7 @@ class InstallData(Configs): self.repos.salixos_patches_repo_changelog) self.write_last_update(path_changelog, self.repos.salixos_patches_repo_name) - data_file: Path = Path(self.repos.salixos_patches_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.salixos_patches_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -1780,7 +1780,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.slackel_repo_path, self.repos.slackel_repo_changelog) self.write_last_update(path_changelog, self.repos.slackel_repo_name) - data_file: Path = Path(self.repos.slackel_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.slackel_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -1888,7 +1888,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.slint_repo_path, self.repos.slint_repo_changelog) self.write_last_update(path_changelog, self.repos.slint_repo_name) - data_file: Path = Path(self.repos.slint_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.slint_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() @@ -1983,7 +1983,7 @@ class InstallData(Configs): path_changelog: Path = Path(self.repos.pprkut_repo_path, self.repos.pprkut_repo_changelog) self.write_last_update(path_changelog, self.repos.pprkut_repo_name) - data_file: Path = Path(self.repos.pprkut_repo_path, self.repos.data_json) + data_file: Path = Path(self.repos.pprkut_repo_path, self.repos.json_file) data_file.write_text(json.dumps(data, indent=4)) self.view_done_message() diff --git a/slpkg/main.py b/slpkg/main.py index a17b073e..c1bc8e24 100644 --- a/slpkg/main.py +++ b/slpkg/main.py @@ -303,8 +303,8 @@ class Menu(Configs): if self.repository != '*' and not self.data: print('\rDatabase loading... ', end='') - json_data: Path = Path(f'{self.repos.repositories_path}/{self.repository}', self.repos.data_json) - self.data: dict = json.loads(json_data.read_text()) + json_file: Path = Path(f'{self.repos.repositories_path}/{self.repository}', self.repos.json_file) + self.data: dict = self.utils.read_json_file(json_file) print(f'{self.yellow}{self.ascii.done}{self.endc}') diff --git a/slpkg/utilities.py b/slpkg/utilities.py index f7ea5dcd..422bac00 100644 --- a/slpkg/utilities.py +++ b/slpkg/utilities.py @@ -5,6 +5,7 @@ from __future__ import annotations import os import re import time +import json import shutil import fnmatch import subprocess @@ -192,3 +193,19 @@ class Utilities(Configs): packages.remove(package) break return packages + + def read_json_file(self, file: Path) -> dict: + """ + Read JSON data from the file. + Args: + file: Path file for reading. + Returns: + Dictionary with data. + """ + json_data: dict = {} + try: + json_data: dict = json.loads(file.read_text(encoding='utf-8')) + except FileNotFoundError: + print(f'\n{self.prog_name}: Error file {file} not found.\n') + raise SystemExit(1) + return json_data