[arch] refactoring: instantiating cached attributes on demand

This commit is contained in:
Vinicius Moreira
2022-02-25 15:16:54 -03:00
parent df8f406d4f
commit 4b4d7744de

View File

@@ -1,12 +1,10 @@
from typing import List, Set, Optional, Iterable from typing import List, Set, Optional, Iterable, Tuple
from bauh.api.abstract.model import SoftwarePackage, CustomSoftwareAction from bauh.api.abstract.model import SoftwarePackage, CustomSoftwareAction
from bauh.commons import resource from bauh.commons import resource
from bauh.gems.arch import ROOT_DIR, ARCH_CACHE_DIR from bauh.gems.arch import ROOT_DIR, ARCH_CACHE_DIR
from bauh.view.util.translation import I18n from bauh.view.util.translation import I18n
CACHED_ATTRS = {'command', 'icon_path', 'repository', 'maintainer', 'desktop_entry', 'categories', 'last_modified', 'commit'}
class ArchPackage(SoftwarePackage): class ArchPackage(SoftwarePackage):
@@ -15,6 +13,7 @@ class ArchPackage(SoftwarePackage):
__action_disable_pkgbuild_edition: Optional[CustomSoftwareAction] = None __action_disable_pkgbuild_edition: Optional[CustomSoftwareAction] = None
__action_reinstall: Optional[CustomSoftwareAction] = None __action_reinstall: Optional[CustomSoftwareAction] = None
__action_ignore_rebuild_check: Optional[CustomSoftwareAction] = None __action_ignore_rebuild_check: Optional[CustomSoftwareAction] = None
__cached_attrs: Optional[Tuple[str, ...]] = None
@classmethod @classmethod
def action_allow_rebuild_check(cls) -> CustomSoftwareAction: def action_allow_rebuild_check(cls) -> CustomSoftwareAction:
@@ -81,6 +80,14 @@ class ArchPackage(SoftwarePackage):
return cls.__action_ignore_rebuild_check return cls.__action_ignore_rebuild_check
@classmethod
def cached_attrs(cls) -> Tuple[str, ...]:
if cls.__cached_attrs is None:
cls.__cached_attrs = ('command', 'icon_path', 'repository', 'maintainer', 'desktop_entry', 'categories',
'last_modified', 'commit')
return cls.__cached_attrs
def __init__(self, name: str = None, version: str = None, latest_version: str = None, description: str = None, def __init__(self, name: str = None, version: str = None, latest_version: str = None, description: str = None,
package_base: str = None, votes: int = None, popularity: float = None, package_base: str = None, votes: int = None, popularity: float = None,
first_submitted: Optional[int] = None, last_modified: Optional[int] = None, first_submitted: Optional[int] = None, last_modified: Optional[int] = None,
@@ -169,7 +176,7 @@ class ArchPackage(SoftwarePackage):
cache = {} cache = {}
# required attrs to cache # required attrs to cache
for a in CACHED_ATTRS: for a in self.cached_attrs():
val = getattr(self, a) val = getattr(self, a)
if val: if val:
@@ -179,7 +186,7 @@ class ArchPackage(SoftwarePackage):
def fill_cached_data(self, data: dict): def fill_cached_data(self, data: dict):
if data: if data:
for a in CACHED_ATTRS: for a in self.cached_attrs():
val = data.get(a) val = data.get(a)
if val: if val:
setattr(self, a, val) setattr(self, a, val)