Use safe_load for automated loading with err handling

This commit is contained in:
ItsDrike 2020-10-22 21:50:52 +02:00
parent 06a94d87fd
commit 5b30c96e4f
No known key found for this signature in database
GPG key ID: F4E8FF4F6AC7F3B4
2 changed files with 18 additions and 7 deletions

View file

@ -15,12 +15,9 @@ def obtain_packages() -> t.List[Package]:
git_packages = yaml_file["git"]
packages = []
for package in pacman_packages:
packages.append(Package(package))
for package in git_packages:
packages.append(Package(package, git=True))
for package in aur_packages:
packages.append(Package(package, aur=True))
packages += Package.safe_load(pacman_packages)
packages += Package.safe_load(aur_packages, aur=True)
packages += Package.safe_load(git_packages, git=True)
return packages

View file

@ -1,4 +1,7 @@
import typing as t
from src.util.install import Install
from src.util.user import Print
class InvalidPackage(Exception):
@ -34,7 +37,7 @@ class Package:
if self.aur:
if not Install.is_installed("yay"):
raise InvalidPackage(f"Package {self} can't be installed (missing `yay` - AUR installation software)")
raise InvalidPackage(f"Package {self} can't be installed (missing `yay` - AUR installation software), alternatively, you can use git")
Install.yay_install(self.name)
elif self.git:
Install.git_install(self.git_url)
@ -49,3 +52,14 @@ class Package:
elif self.aur:
return f"<Aur package: {self.name}>"
return f"<Package: {self.name}>"
@classmethod
def safe_load(cls, packages: t.List[str], aur: bool = False, git: bool = False) -> t.List["Package"]:
loaded_packages = []
for package in packages:
try:
loaded_packages.append(cls(package, aur=aur, git=git))
except InvalidPackage as e:
Print.warning(e)
return loaded_packages