From caff7cdb209db9a6e086a2e4a40d4cc0f743ecc7 Mon Sep 17 00:00:00 2001 From: Obijuan Date: Sun, 17 Mar 2024 12:44:19 +0100 Subject: [PATCH] resources.py refactoring --- apio/resources.py | 58 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/apio/resources.py b/apio/resources.py index 69f417df..818e4f67 100644 --- a/apio/resources.py +++ b/apio/resources.py @@ -11,7 +11,6 @@ from collections import OrderedDict import shutil import click - from apio import util from apio.profile import Profile @@ -173,31 +172,75 @@ def _load_resource(name: str) -> dict: def get_package_release_name(self, package: str) -> str: """return the package name""" - # -- TODO: Check for apio system errors! - return self.packages[package]["release"]["package_name"] + try: + package_name = self.packages[package]["release"]["package_name"] + + # -- This error should never ocurr + except KeyError as excp: + click.secho(f"Apio System Error! Invalid key: {excp}", fg="red") + click.secho( + "Module: resources.py. Function: get_package_release_name()", + fg="red", + ) - def get_packages(self): - """DOC: TODO""" + # -- Abort! + sys.exit(1) - # Classify packages + except TypeError as excp: + + click.secho(f"Apio System Error! {excp}", fg="red") + click.secho( + "Module: resources.py. Function: get_package_release_name()", + fg="red", + ) + + # -- Abort! + sys.exit(1) + + # -- Return the name + return package_name + + def get_packages(self) -> tuple[list, list]: + """Get all the packages, classified in installed and + not installed + * OUTPUT: + - A tuple of two lists: Installed and not installed packages + """ + + # -- Classify the packages in two lists installed_packages = [] notinstalled_packages = [] + # -- Go though all the apio packages for package in self.packages: + + # -- Collect information about the package data = { "name": package, "version": None, - "description": self.packages.get(package).get("description"), + "description": self.packages[package]["description"], } + + # -- Check if this package is installed if package in self.profile.packages: + + # package_name = self.get_package_release_name(package) + data["version"] = self.profile.get_package_version( package, self.get_package_release_name(package) ) installed_packages += [data] + + # -- The package is not installed else: notinstalled_packages += [data] + # -- Check the installed packages and update + # -- its information for package in self.profile.packages: + + # -- The package is not known! + # -- Strange case if package not in self.packages: data = { "name": package, @@ -206,6 +249,7 @@ def get_packages(self): } installed_packages += [data] + # -- Return the packages, classified return installed_packages, notinstalled_packages def list_packages(self, installed=True, notinstalled=True):