From f3c4bcbc71713579a9299766e33ecbdc2dc16ea2 Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Fri, 25 Feb 2022 14:45:11 -0300 Subject: [PATCH] [snap] refactoring: custom actions moved to SnapApplication and are instantiated on demand --- CHANGELOG.md | 1 + bauh/gems/snap/controller.py | 25 +++---------------------- bauh/gems/snap/model.py | 31 +++++++++++++++++++++++++++---- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac04b89e..f034b27f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Improvements - General - minor memory improvements + - code refactoring - Arch - info dialog: diff --git a/bauh/gems/snap/controller.py b/bauh/gems/snap/controller.py index 87a6a99c..48d39be5 100644 --- a/bauh/gems/snap/controller.py +++ b/bauh/gems/snap/controller.py @@ -9,18 +9,17 @@ from bauh.api.abstract.controller import SoftwareManager, SearchResult, Applicat from bauh.api.abstract.disk import DiskCacheLoader from bauh.api.abstract.handler import ProcessWatcher, TaskManager from bauh.api.abstract.model import SoftwarePackage, PackageHistory, PackageUpdate, PackageSuggestion, \ - SuggestionPriority, CustomSoftwareAction, PackageStatus + SuggestionPriority, PackageStatus from bauh.api.abstract.view import SingleSelectComponent, SelectViewType, InputOption, ViewComponent, PanelComponent, \ FormComponent, TextInputComponent from bauh.api.exception import NoInternetException -from bauh.commons import resource from bauh.commons.boot import CreateConfigFile from bauh.commons.category import CategoriesDownloader from bauh.commons.html import bold from bauh.commons.system import SystemProcess, ProcessHandler, new_root_subprocess, get_human_size_str from bauh.commons.view_utils import new_select from bauh.gems.snap import snap, URL_CATEGORIES_FILE, CATEGORIES_FILE_PATH, SUGGESTIONS_FILE, \ - get_icon_path, snapd, ROOT_DIR + get_icon_path, snapd from bauh.gems.snap.config import SnapConfigManager from bauh.gems.snap.model import SnapApplication from bauh.gems.snap.snapd import SnapdClient @@ -43,23 +42,6 @@ class SnapManager(SoftwareManager): self.suggestions_cache = context.cache_factory.new() self.info_path = None self.configman = SnapConfigManager() - self.custom_actions = ( - CustomSoftwareAction(i18n_status_key='snap.action.refresh.status', - i18n_label_key='snap.action.refresh.label', - i18n_description_key='snap.action.refresh.desc', - icon_path=resource.get_path('img/refresh.svg', ROOT_DIR), - manager_method='refresh', - requires_root=True, - i18n_confirm_key='snap.action.refresh.confirm'), - CustomSoftwareAction(i18n_status_key='snap.action.channel.status', - i18n_label_key='snap.action.channel.label', - i18n_confirm_key='snap.action.channel.confirm', - i18n_description_key='snap.action.channel.desc', - icon_path=resource.get_path('img/refresh.svg', ROOT_DIR), - manager_method='change_channel', - requires_root=True, - requires_confirmation=False) - ) def _fill_categories(self, app: SnapApplication): categories = self.categories.get(app.name.lower()) @@ -369,8 +351,7 @@ class SnapManager(SoftwareManager): confinement=app_json.get('confinement'), app_type=app_json.get('type'), app=is_application, - installed_size=app_json.get('installed-size'), - extra_actions=self.custom_actions) + installed_size=app_json.get('installed-size')) if disk_loader and app.installed: disk_loader.fill(app) diff --git a/bauh/gems/snap/model.py b/bauh/gems/snap/model.py index 75a5a73b..25a4f5ba 100644 --- a/bauh/gems/snap/model.py +++ b/bauh/gems/snap/model.py @@ -1,4 +1,4 @@ -from typing import Optional, Set, Iterable +from typing import Optional, Set, Iterable, Tuple from bauh.api.abstract.model import SoftwarePackage, CustomSoftwareAction from bauh.commons import resource @@ -7,10 +7,34 @@ from bauh.gems.snap import ROOT_DIR class SnapApplication(SoftwarePackage): + __actions: Optional[Tuple[CustomSoftwareAction, CustomSoftwareAction]] = None + + @classmethod + def actions(cls) -> Tuple[CustomSoftwareAction, CustomSoftwareAction]: + if cls.__actions is None: + cls.__actions = ( + CustomSoftwareAction(i18n_status_key='snap.action.refresh.status', + i18n_label_key='snap.action.refresh.label', + i18n_description_key='snap.action.refresh.desc', + icon_path=resource.get_path('img/refresh.svg', ROOT_DIR), + manager_method='refresh', + requires_root=True, + i18n_confirm_key='snap.action.refresh.confirm'), + CustomSoftwareAction(i18n_status_key='snap.action.channel.status', + i18n_label_key='snap.action.channel.label', + i18n_confirm_key='snap.action.channel.confirm', + i18n_description_key='snap.action.channel.desc', + icon_path=resource.get_path('img/refresh.svg', ROOT_DIR), + manager_method='change_channel', + requires_root=True, + requires_confirmation=False) + ) + + return cls.__actions + def __init__(self, id: str = None, name: str = None, version: str = None, latest_version: str = None, description: str = None, publisher: str = None, rev: str = None, notes: str = None, confinement: str = None, verified_publisher: bool = False, - extra_actions: Optional[Iterable[CustomSoftwareAction]] = None, screenshots: Optional[Set[str]] = None, license: Optional[str] = None, installed: bool = False, @@ -31,7 +55,6 @@ class SnapApplication(SoftwarePackage): self.notes = notes self.confinement = confinement self.verified_publisher = verified_publisher - self.extra_actions = extra_actions self.screenshots = screenshots self.download_size = download_size self.developer = developer @@ -94,7 +117,7 @@ class SnapApplication(SoftwarePackage): def get_custom_actions(self) -> Optional[Iterable[CustomSoftwareAction]]: if self.installed: - return self.extra_actions + return self.actions() def supports_backup(self) -> bool: return True