diff --git a/slpkg/checks.py b/slpkg/checks.py index 21accf3c..83bf0fcb 100644 --- a/slpkg/checks.py +++ b/slpkg/checks.py @@ -1,8 +1,6 @@ #!/usr/bin/python3 # -*- coding: utf-8 -*- -from pathlib import Path - from slpkg.configs import Configs from slpkg.utilities import Utilities from slpkg.error_messages import Errors @@ -43,11 +41,4 @@ class Check(Configs): if not_found: self.errors.raise_error_message(f'Not found \'{", ".join(not_found)}\' installed packages', - exit_status=1) - - def is_data_file_exist(self): - json_data_file: Path = Path(f'{self.repos.repositories_path}/{self.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) + exit_status=1) \ No newline at end of file diff --git a/slpkg/main.py b/slpkg/main.py index 69b4c708..c453f57d 100644 --- a/slpkg/main.py +++ b/slpkg/main.py @@ -294,16 +294,6 @@ class Menu(Configs): self.check = Check(self.repository) self.choose = Choose(self.repository) - def load_database(self): - if self.repository != '*' and not self.data: - self.check.is_data_file_exist() - print('\rDatabase loading... ', end='') - - 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}') - def check_for_repositories(self) -> None: """ Checks a combination for binaries use repositories only and if repository exists. """ except_options: tuple = ( @@ -444,7 +434,7 @@ class Menu(Configs): command: str = Menu.upgrade.__name__ if len(self.args) == 1: - self.load_database() + self.data: dict = self.utils.load_data(self.repository) upgrade = Upgrade(self.repository, self.data) packages: list = list(upgrade.packages()) @@ -486,7 +476,7 @@ class Menu(Configs): command: str = Menu.build.__name__ if len(self.args) >= 2: - self.load_database() + self.data: dict = self.utils.load_data(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: @@ -512,8 +502,7 @@ class Menu(Configs): command: str = Menu.install.__name__ if len(self.args) >= 2: - self.load_database() - # self.check.is_database_empty() + self.data: dict = self.utils.load_data(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: @@ -537,7 +526,7 @@ class Menu(Configs): command: str = Menu.download.__name__ if len(self.args) >= 2: - self.load_database() + self.data: dict = self.utils.load_data(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: @@ -575,11 +564,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.load_database() + self.data: dict = self.utils.load_data(self.repository) packages: list = self.utils.case_insensitive_pattern_matching(packages, self.data) if self.utils.is_option(self.flag_searches, self.flags): - self.load_database() + self.data: dict = self.utils.load_data(self.repository) packages: list = self.choose.packages(self.data, packages, command) find = FindInstalled(self.flags, packages) @@ -591,7 +580,7 @@ class Menu(Configs): command: str = Menu.view.__name__ if len(self.args) >= 2: - self.load_database() + self.data: dict = self.utils.load_data(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: @@ -618,7 +607,7 @@ class Menu(Configs): packages: list = self.is_file_list_packages() if self.utils.is_option(self.flag_searches, self.flags): - self.load_database() + self.data: dict = self.utils.load_data(self.repository) packages: list = self.choose.packages(self.data, packages, command) pkgs = SearchPackage(self.flags, packages, self.repository) @@ -630,7 +619,7 @@ class Menu(Configs): command: str = Menu.dependees.__name__ if len(self.args) >= 2: - self.load_database() + self.data: dict = self.utils.load_data(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: @@ -650,7 +639,7 @@ class Menu(Configs): command: str = Menu.tracking.__name__ if len(self.args) >= 2: - self.load_database() + self.data: dict = self.utils.load_data(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 edf2b74e..a21e3b63 100644 --- a/slpkg/repo_info.py +++ b/slpkg/repo_info.py @@ -40,8 +40,7 @@ class RepoInfo(Configs): self.view_the_repositories_information() def count_the_packages(self, repository: str) -> int: - json_file: Path = Path(f'{self.repos.repositories_path}/{repository}', self.repos.json_file) - repo_data: dict = self.utils.read_json_file(json_file) + repo_data: dict = self.utils.load_data(repository, message=False) count: int = len(repo_data.keys()) self.total_packages += count return count diff --git a/slpkg/search.py b/slpkg/search.py index f765cbc8..48c07475 100644 --- a/slpkg/search.py +++ b/slpkg/search.py @@ -16,10 +16,10 @@ class SearchPackage(Configs): super(Configs, self).__init__() self.packages: list = packages self.repository: str = repository + self.ascii = AsciiBox() self.utils = Utilities() self.repos = Repositories() - self.ascii = AsciiBox() self.matching: int = 0 self.repo_data: dict = {} @@ -29,25 +29,22 @@ class SearchPackage(Configs): ('-m', '--no-case'), flags) def search(self) -> None: - print('\rDatabase loading... ', end='') - if self.repository == '*': self.search_to_all_repositories() else: - json_file: Path = Path(f'{self.repos.repositories_path}/{self.repository}', self.repos.json_file) - self.repo_data: dict = self.utils.read_json_file(json_file) + self.repo_data: dict = self.utils.load_data(self.repository) self.search_for_the_packages(self.repository) - print(f'{self.yellow}{self.ascii.done}{self.endc}') print(f'The list below shows the repo ' f'packages that contains \'{", ".join([p for p in self.packages])}\':\n') self.summary_of_searching() def search_to_all_repositories(self) -> None: + print('\rDatabase loading... ', end='') + print(f'{self.yellow}{self.ascii.done}{self.endc}') for repo, item in self.repos.repositories.items(): if item['enable']: # Check if the repository is enabled - json_file: Path = Path(f'{self.repos.repositories_path}/{repo}', self.repos.json_file) - self.repo_data: dict = self.utils.read_json_file(json_file) + self.repo_data: dict = self.utils.load_data(repo, message=False) self.search_for_the_packages(repo) def search_for_the_packages(self, repo: str) -> None: diff --git a/slpkg/utilities.py b/slpkg/utilities.py index 5e9f3ae2..6c2a2712 100644 --- a/slpkg/utilities.py +++ b/slpkg/utilities.py @@ -16,6 +16,7 @@ from slpkg.configs import Configs from slpkg.blacklist import Blacklist from slpkg.error_messages import Errors from slpkg.repositories import Repositories +from slpkg.views.asciibox import AsciiBox class Utilities(Configs): @@ -25,6 +26,7 @@ class Utilities(Configs): self.black = Blacklist() self.errors = Errors() self.repos = Repositories() + self.ascii = AsciiBox() def is_package_installed(self, name: str) -> str: """ Returns the installed package binary. """ @@ -41,7 +43,7 @@ class Utilities(Configs): for file in self.log_packages.glob(self.file_pattern): name: str = self.split_package(file.name)['name'] - if not name.startswith('.') and not self.blacklist_pattern(name): + if not name.startswith('.') and name not in self.ignore_packages(name): installed_packages[name] = file.name return installed_packages @@ -155,12 +157,17 @@ class Utilities(Configs): packages.extend(list(data.keys())) return packages - def blacklist_pattern(self, name: str) -> bool: - """ This module provides support for Unix shell-style wildcards. """ - blacklist: list[str] = [] - blacklist.extend(self.black.packages()) - if [black for black in blacklist if fnmatch.fnmatch(name, black)]: - return True + def ignore_packages(self, packages: list) -> list: + """ + Matching packages using regular expression. + Args: + packages: Tha packages to apply the pattern. + Returns: + The matching packages. + """ + pattern: str = '|'.join(self.black.packages()) + matching_packages: list = [pkg for pkg in packages if re.search(pattern, pkg)] + return matching_packages @staticmethod def change_owner_privileges(folder: Path) -> None: @@ -196,3 +203,20 @@ class Utilities(Configs): self.errors.raise_error_message(f'{file} not found.', exit_status=1) raise SystemExit(1) return json_data + + def load_data(self, repository: str, message=True) -> dict: + if repository != '*': + if message: + print('\rDatabase loading... ', end='') + 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) + if message: + print(f'{self.yellow}{self.ascii.done}{self.endc}') + + return data