Skip to content

Commit

Permalink
resources.py refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Obijuan committed Mar 17, 2024
1 parent c6f0c93 commit caff7cd
Showing 1 changed file with 51 additions and 7 deletions.
58 changes: 51 additions & 7 deletions apio/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from collections import OrderedDict
import shutil
import click

from apio import util
from apio.profile import Profile

Expand Down Expand Up @@ -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,
Expand All @@ -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):
Expand Down

0 comments on commit caff7cd

Please sign in to comment.