diff --git a/slpkg/load_data.py b/slpkg/load_data.py new file mode 100644 index 00000000..996e78cb --- /dev/null +++ b/slpkg/load_data.py @@ -0,0 +1,82 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- + +from pathlib import Path + +from slpkg.configs import Configs +from slpkg.utilities import Utilities +from slpkg.blacklist import Blacklist +from slpkg.views.asciibox import AsciiBox +from slpkg.repositories import Repositories + + +class LoadData(Configs): + + def __init__(self): + self.repos = Repositories() + self.utils = Utilities() + self.black = Blacklist() + self.ascii = AsciiBox() + + def load(self, repository: str) -> dict: + print('\rDatabase loading... ', end='') + data: dict = {} + if repository == '*': + 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) + 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) + + blacklist: tuple = self.black.packages() + if blacklist: + if repository == '*': + self._remove_blacklist_from_all_repos(data) + else: + self._remove_blacklist_from_a_repo(data) + + print(f'{self.yellow}{self.ascii.done}{self.endc}') + return data + + def _remove_blacklist_from_all_repos(self, data: dict) -> dict: + # Remove blacklist packages from keys. + for name, repo in data.items(): + blacklist_packages: list = self.utils.ignore_packages(list(data[name].keys())) + for pkg in blacklist_packages: + if pkg in data[name].keys(): + del data[name][pkg] + + # Remove blacklist packages from dependencies (values). + for name, repo in data.items(): + blacklist_packages: list = self.utils.ignore_packages(list(data[name].keys())) + for pkg, dep in repo.items(): + deps: list = dep['requires'] + for blk in blacklist_packages: + if blk in deps: + deps.remove(blk) + data[name][pkg]['requires'] = deps + return data + + def _remove_blacklist_from_a_repo(self, data: dict) -> dict: + blacklist_packages: list = self.utils.ignore_packages(list(data.keys())) + # Remove blacklist packages from keys. + for pkg in blacklist_packages: + if pkg in data.keys(): + del data[pkg] + + # Remove blacklist packages from dependencies (values). + for pkg, dep in data.items(): + deps: list = dep['requires'] + for blk in blacklist_packages: + if blk in deps: + deps.remove(blk) + data[pkg]['requires'] = deps + return data diff --git a/slpkg/main.py b/slpkg/main.py index ba3b811b..5a9129d4 100644 --- a/slpkg/main.py +++ b/slpkg/main.py @@ -8,6 +8,7 @@ from slpkg.checks import Check from slpkg.upgrade import Upgrade from slpkg.configs import Configs from slpkg.tracking import Tracking +from slpkg.load_data import LoadData from slpkg.repo_info import RepoInfo from slpkg.dependees import Dependees from slpkg.utilities import Utilities @@ -41,6 +42,7 @@ class Menu(Configs): self.usage = Usage() self.repos = Repositories() self.ascii = AsciiBox() + self.load_data = LoadData() self.repository: str = self.repos.default_repository @@ -442,14 +444,14 @@ class Menu(Configs): if len(self.args) == 1: if self.utils.is_option((self.flag_check, self.flag_short_check), self.flags): - self.data: dict = self.utils.load_data(self.repository) + self.data: dict = self.load_data.load(self.repository) upgrade = Upgrade(self.repository, self.data) upgrade.check_packages() else: if self.repository == '*': self.repository = self.repos.default_repository - self.data: dict = self.utils.load_data(self.repository) + self.data: dict = self.load_data.load(self.repository) upgrade = Upgrade(self.repository, self.data) packages: list = list(upgrade.packages()) @@ -483,7 +485,7 @@ class Menu(Configs): command: str = Menu.build.__name__ if len(self.args) >= 2: - self.data: dict = self.utils.load_data(self.repository) + self.data: dict = self.load_data.load(self.repository) packages: list = self.is_file_list_packages() if self.utils.is_option(self.flag_no_cases, self.flags) or not self.case_sensitive: @@ -509,7 +511,7 @@ class Menu(Configs): command: str = Menu.install.__name__ if len(self.args) >= 2: - self.data: dict = self.utils.load_data(self.repository) + self.data: dict = self.load_data.load(self.repository) packages: list = self.is_file_list_packages() if self.utils.is_option(self.flag_no_cases, self.flags) or not self.case_sensitive: @@ -533,7 +535,7 @@ class Menu(Configs): command: str = Menu.download.__name__ if len(self.args) >= 2: - self.data: dict = self.utils.load_data(self.repository) + self.data: dict = self.load_data.load(self.repository) packages: list = self.is_file_list_packages() if self.utils.is_option(self.flag_no_cases, self.flags) or not self.case_sensitive: @@ -571,11 +573,11 @@ class Menu(Configs): packages: list = self.is_file_list_packages() if self.utils.is_option(self.flag_no_cases, self.flags) or not self.case_sensitive: - self.data: dict = self.utils.load_data(self.repository) + self.data: dict = self.load_data.load(self.repository) packages: list = self.utils.case_insensitive_pattern_matching(packages, self.data) if self.utils.is_option(self.flag_searches, self.flags): - self.data: dict = self.utils.load_data(self.repository) + self.data: dict = self.load_data.load(self.repository) packages: list = self.choose.packages(self.data, packages, command) find = FindInstalled(self.flags, packages) @@ -587,7 +589,7 @@ class Menu(Configs): command: str = Menu.view.__name__ if len(self.args) >= 2: - self.data: dict = self.utils.load_data(self.repository) + self.data: dict = self.load_data.load(self.repository) packages: list = self.is_file_list_packages() if self.utils.is_option(self.flag_no_cases, self.flags) or not self.case_sensitive: @@ -609,7 +611,7 @@ class Menu(Configs): def search(self) -> None: command: str = Menu.search.__name__ - self.data: dict = self.utils.load_data(self.repository) + self.data: dict = self.load_data.load(self.repository) if len(self.args) >= 2: packages: list = self.is_file_list_packages() @@ -626,7 +628,7 @@ class Menu(Configs): command: str = Menu.dependees.__name__ if len(self.args) >= 2: - self.data: dict = self.utils.load_data(self.repository) + self.data: dict = self.load_data.load(self.repository) packages: list = self.is_file_list_packages() if self.utils.is_option(self.flag_no_cases, self.flags) or not self.case_sensitive: @@ -646,7 +648,7 @@ class Menu(Configs): command: str = Menu.tracking.__name__ if len(self.args) >= 2: - self.data: dict = self.utils.load_data(self.repository) + self.data: dict = self.load_data.load(self.repository) packages: list = self.is_file_list_packages() if self.utils.is_option(self.flag_no_cases, self.flags) or not self.case_sensitive: diff --git a/slpkg/repo_info.py b/slpkg/repo_info.py index ece42405..237931ff 100644 --- a/slpkg/repo_info.py +++ b/slpkg/repo_info.py @@ -5,6 +5,7 @@ import shutil from pathlib import Path from slpkg.configs import Configs +from slpkg.load_data import LoadData from slpkg.utilities import Utilities from slpkg.repositories import Repositories @@ -16,6 +17,7 @@ class RepoInfo(Configs): self.flags: list = flags self.repository: str = repository + self.load_data = LoadData() self.utils = Utilities() self.repos = Repositories() self.columns, self.rows = shutil.get_terminal_size() @@ -34,9 +36,9 @@ class RepoInfo(Configs): def info(self) -> None: """ Prints information about repositories. """ if self.option_for_repository: - self.repo_data: dict = self.utils.load_data(self.repository) + self.repo_data: dict = self.load_data.load(self.repository) else: - self.repo_data: dict = self.utils.load_data('*') + self.repo_data: dict = self.load_data.load('*') self.view_the_title() diff --git a/slpkg/utilities.py b/slpkg/utilities.py index a76a679f..85ae8851 100644 --- a/slpkg/utilities.py +++ b/slpkg/utilities.py @@ -232,66 +232,3 @@ class Utilities(Configs): pattern: str = '|'.join(blacklist) matching_packages: list = [pkg for pkg in packages if re.search(pattern, pkg)] return matching_packages - - def load_data(self, repository: str) -> dict: - print('\rDatabase loading... ', end='') - data: dict = {} - if repository == '*': - 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.read_json_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.read_json_file(json_data_file) - - blacklist: tuple = self.black.packages() - if blacklist: - if repository == '*': - self._remove_blacklist_from_all_repos(data) - else: - self._remove_blacklist_from_a_repo(data) - - print(f'{self.yellow}{self.ascii.done}{self.endc}') - return data - - def _remove_blacklist_from_all_repos(self, data: dict) -> dict: - # Remove blacklist packages from keys. - for name, repo in data.items(): - blacklist_packages: list = self.ignore_packages(list(data[name].keys())) - for pkg in blacklist_packages: - if pkg in data[name].keys(): - del data[name][pkg] - - # Remove blacklist packages from dependencies (values). - for name, repo in data.items(): - blacklist_packages: list = self.ignore_packages(list(data[name].keys())) - for pkg, dep in repo.items(): - deps: list = dep['requires'] - for blk in blacklist_packages: - if blk in deps: - deps.remove(blk) - data[name][pkg]['requires'] = deps - return data - - def _remove_blacklist_from_a_repo(self, data: dict) -> dict: - blacklist_packages: list = self.ignore_packages(list(data.keys())) - # Remove blacklist packages from keys. - for pkg in blacklist_packages: - if pkg in data.keys(): - del data[pkg] - - # Remove blacklist packages from dependencies (values). - for pkg, dep in data.items(): - deps: list = dep['requires'] - for blk in blacklist_packages: - if blk in deps: - deps.remove(blk) - data[pkg]['requires'] = deps - return data