From 7726ee0bcf28a1e1165c8ff3db9bb1cd51de0cc4 Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Fri, 25 Feb 2022 15:45:52 -0300 Subject: [PATCH] [appimage] refactoring: more constants refactored as on demand class attributes --- bauh/gems/appimage/model.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/bauh/gems/appimage/model.py b/bauh/gems/appimage/model.py index ba964326..828f7752 100644 --- a/bauh/gems/appimage/model.py +++ b/bauh/gems/appimage/model.py @@ -1,5 +1,6 @@ import re from io import StringIO +from re import Pattern from typing import Optional, Iterable, Tuple from bauh.api.abstract.model import SoftwarePackage, CustomSoftwareAction @@ -7,15 +8,12 @@ from bauh.commons import resource from bauh.gems.appimage import ROOT_DIR, INSTALLATION_DIR from bauh.view.util.translation import I18n -RE_MANY_SPACES = re.compile(r'\s+') - -CACHED_ATTRS = {'name', 'description', 'version', 'url_download', 'author', 'license', 'source', - 'icon_path', 'github', 'categories', 'imported', 'install_dir', 'symlink'} - class AppImage(SoftwarePackage): __actions_local_installation: Optional[Tuple[CustomSoftwareAction, ...]] = None + __cached_attrs: Optional[Tuple[str, ...]] = None + __re_many_spaces: Optional[Pattern] = None @classmethod def actions_local_installation(cls) -> Tuple[CustomSoftwareAction, ...]: @@ -30,6 +28,21 @@ class AppImage(SoftwarePackage): return cls.__actions_local_installation + @classmethod + def cached_attrs(cls) -> Tuple[str, ...]: + if cls.__cached_attrs is None: + cls.__cached_attrs = ('name', 'description', 'version', 'url_download', 'author', 'license', 'source', + 'icon_path', 'github', 'categories', 'imported', 'install_dir', 'symlink') + + return cls.__cached_attrs + + @classmethod + def re_many_spaces(cls) -> Pattern: + if cls.__re_many_spaces is None: + cls.__re_many_spaces = re.compile(r'\s+') + + return cls.__re_many_spaces + def __init__(self, name: str = None, description: str = None, github: str = None, source: str = None, version: str = None, url_download: str = None, url_icon: str = None, url_screenshot: str = None, license: str = None, author: str = None, categories=None, icon_path: str = None, installed: bool = False, @@ -84,7 +97,7 @@ class AppImage(SoftwarePackage): def get_data_to_cache(self) -> dict: data = {} - for a in CACHED_ATTRS: + for a in self.cached_attrs(): val = getattr(self, a) if val: data[a] = val @@ -92,7 +105,7 @@ class AppImage(SoftwarePackage): return data def fill_cached_data(self, data: dict): - for a in CACHED_ATTRS: + for a in self.cached_attrs(): val = data.get(a) if val: @@ -141,7 +154,7 @@ class AppImage(SoftwarePackage): def get_clean_name(self) -> Optional[str]: if self.name: - return RE_MANY_SPACES.sub('-', self.name.lower().strip()) + return self.re_many_spaces().sub('-', self.name.lower().strip()) def to_desktop_entry(self) -> str: de = StringIO()