Updated read data

This commit is contained in:
Dimitris Zlatanidis 2024-03-21 21:58:50 +02:00
parent ebccc377cb
commit 8f04a671bb

View file

@ -1,6 +1,7 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import json
from pathlib import Path
from slpkg.configs import Configs
@ -25,16 +26,10 @@ class LoadData(Configs):
for repo, item in self.repos.repositories.items():
if item['enable']: # Check if the repository is enabled
json_data_file: Path = Path(f'{self.repos.repositories_path}/{repo}', self.repos.json_file)
data[repo] = self.utils.read_json_file(json_data_file)
data[repo] = self.read_data_file(json_data_file)
else:
json_data_file: Path = Path(f'{self.repos.repositories_path}/{repository}', self.repos.json_file)
if not json_data_file.is_file():
print("\nNeed to update the database first, please run:\n")
print(f"{'':>2} $ slpkg update\n")
raise SystemExit(1)
data: dict = self.utils.read_json_file(json_data_file)
data: dict = self.read_data_file(json_data_file)
blacklist: tuple = self.black.packages()
if blacklist:
@ -46,6 +41,25 @@ class LoadData(Configs):
print(f'{self.bgreen}{self.ascii.done}{self.endc}')
return data
@staticmethod
def read_data_file(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\nFile {file} not found!')
print('\nNeed to update the database first, please run:\n')
print(f"{'':>2} $ slpkg update\n")
raise SystemExit(1)
return json_data
def _remove_blacklist_from_all_repos(self, data: dict) -> dict:
# Remove blacklist packages from keys.
for name, repo in data.items():