mirror of
https://gitlab.com/dslackw/slpkg.git
synced 2024-12-27 09:58:10 +01:00
108 lines
3.6 KiB
Python
108 lines
3.6 KiB
Python
#!/usr/bin/python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
from slpkg.configs import Configs
|
|
from slpkg.utilities import Utilities
|
|
from slpkg.repositories import Repositories
|
|
|
|
|
|
class SearchPackage(Configs): # pylint: disable=[R0902]
|
|
"""Search packages from the repositories."""
|
|
|
|
def __init__(self, flags: list, packages: list, data: dict, repository: str):
|
|
super(Configs, self).__init__()
|
|
self.packages: list = packages
|
|
self.data: dict = data
|
|
self.repository: str = repository
|
|
|
|
self.utils = Utilities()
|
|
self.repos = Repositories()
|
|
|
|
self.matching: int = 0
|
|
self.data_dict: dict = {}
|
|
self.repo_data: dict = {}
|
|
|
|
self.option_for_no_case: bool = self.utils.is_option(
|
|
('-m', '--no-case'), flags)
|
|
|
|
self.option_for_pkg_version: bool = self.utils.is_option(
|
|
('-p', '--pkg-version'), flags)
|
|
|
|
def search(self) -> None:
|
|
"""Choose between all and one repository."""
|
|
if self.repository == '*':
|
|
self.search_to_all_repositories()
|
|
else:
|
|
self.repo_data: dict = self.data
|
|
self.search_for_the_packages(self.repository)
|
|
|
|
print('The list below shows the repository packages:\n')
|
|
self.summary_of_searching()
|
|
|
|
def search_to_all_repositories(self) -> None:
|
|
"""Search package name to all enabled repositories."""
|
|
all_data: dict = self.data
|
|
for name, repo in all_data.items():
|
|
self.repo_data: dict = repo
|
|
self.search_for_the_packages(name)
|
|
|
|
def search_for_the_packages(self, repo: str) -> None:
|
|
"""Search for packages and save in a dictionary.
|
|
|
|
Args:
|
|
repo (str): repository name.
|
|
"""
|
|
for package in self.packages:
|
|
for name, data_pkg in sorted(self.repo_data.items()):
|
|
|
|
if package in name or package == '*' or self.is_not_case_sensitive(package, name):
|
|
self.matching += 1
|
|
|
|
self.data_dict[self.matching] = {
|
|
'repository': repo,
|
|
'name': name,
|
|
'version': data_pkg['version']
|
|
}
|
|
|
|
def summary_of_searching(self) -> None:
|
|
"""Print the result."""
|
|
try:
|
|
repo_length: int = max(len(repo['repository']) for repo in self.data_dict.values())
|
|
except ValueError:
|
|
repo_length: int = 1
|
|
|
|
try:
|
|
name_length: int = max(len(name['name']) for name in self.data_dict.values())
|
|
except ValueError:
|
|
name_length: int = 1
|
|
|
|
if self.matching:
|
|
version: str = ''
|
|
repository: str = ''
|
|
for item in self.data_dict.values():
|
|
if self.option_for_pkg_version:
|
|
version: str = item['version']
|
|
if self.repository == '*':
|
|
repository: str = f"{item['repository']:<{repo_length}} : "
|
|
|
|
print(f"{repository}{self.cyan}{item['name']:<{name_length}}{self.endc} "
|
|
f"{self.yellow}{version}{self.endc}")
|
|
|
|
print(f'\n{self.grey}Total found {self.matching} packages.{self.endc}')
|
|
else:
|
|
print('\nDoes not match any package.\n')
|
|
|
|
def is_not_case_sensitive(self, package: str, name: str) -> bool:
|
|
"""Check for case sensitive.
|
|
|
|
Args:
|
|
package (str): Package file.
|
|
name (str): Package name.
|
|
|
|
Returns:
|
|
bool: True or False.
|
|
"""
|
|
if self.option_for_no_case:
|
|
return package.lower() in name.lower()
|
|
return False
|