[snap] refactoring: custom actions moved to SnapApplication and are instantiated on demand

This commit is contained in:
Vinicius Moreira
2022-02-25 14:45:11 -03:00
parent 077aa6ecff
commit f3c4bcbc71
3 changed files with 31 additions and 26 deletions

View File

@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Improvements ### Improvements
- General - General
- minor memory improvements - minor memory improvements
- code refactoring
- Arch - Arch
- info dialog: - info dialog:

View File

@@ -9,18 +9,17 @@ from bauh.api.abstract.controller import SoftwareManager, SearchResult, Applicat
from bauh.api.abstract.disk import DiskCacheLoader from bauh.api.abstract.disk import DiskCacheLoader
from bauh.api.abstract.handler import ProcessWatcher, TaskManager from bauh.api.abstract.handler import ProcessWatcher, TaskManager
from bauh.api.abstract.model import SoftwarePackage, PackageHistory, PackageUpdate, PackageSuggestion, \ 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, \ from bauh.api.abstract.view import SingleSelectComponent, SelectViewType, InputOption, ViewComponent, PanelComponent, \
FormComponent, TextInputComponent FormComponent, TextInputComponent
from bauh.api.exception import NoInternetException from bauh.api.exception import NoInternetException
from bauh.commons import resource
from bauh.commons.boot import CreateConfigFile from bauh.commons.boot import CreateConfigFile
from bauh.commons.category import CategoriesDownloader from bauh.commons.category import CategoriesDownloader
from bauh.commons.html import bold from bauh.commons.html import bold
from bauh.commons.system import SystemProcess, ProcessHandler, new_root_subprocess, get_human_size_str from bauh.commons.system import SystemProcess, ProcessHandler, new_root_subprocess, get_human_size_str
from bauh.commons.view_utils import new_select from bauh.commons.view_utils import new_select
from bauh.gems.snap import snap, URL_CATEGORIES_FILE, CATEGORIES_FILE_PATH, SUGGESTIONS_FILE, \ 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.config import SnapConfigManager
from bauh.gems.snap.model import SnapApplication from bauh.gems.snap.model import SnapApplication
from bauh.gems.snap.snapd import SnapdClient from bauh.gems.snap.snapd import SnapdClient
@@ -43,23 +42,6 @@ class SnapManager(SoftwareManager):
self.suggestions_cache = context.cache_factory.new() self.suggestions_cache = context.cache_factory.new()
self.info_path = None self.info_path = None
self.configman = SnapConfigManager() 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): def _fill_categories(self, app: SnapApplication):
categories = self.categories.get(app.name.lower()) categories = self.categories.get(app.name.lower())
@@ -369,8 +351,7 @@ class SnapManager(SoftwareManager):
confinement=app_json.get('confinement'), confinement=app_json.get('confinement'),
app_type=app_json.get('type'), app_type=app_json.get('type'),
app=is_application, app=is_application,
installed_size=app_json.get('installed-size'), installed_size=app_json.get('installed-size'))
extra_actions=self.custom_actions)
if disk_loader and app.installed: if disk_loader and app.installed:
disk_loader.fill(app) disk_loader.fill(app)

View File

@@ -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.api.abstract.model import SoftwarePackage, CustomSoftwareAction
from bauh.commons import resource from bauh.commons import resource
@@ -7,10 +7,34 @@ from bauh.gems.snap import ROOT_DIR
class SnapApplication(SoftwarePackage): 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, 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, description: str = None, publisher: str = None, rev: str = None, notes: str = None,
confinement: str = None, verified_publisher: bool = False, confinement: str = None, verified_publisher: bool = False,
extra_actions: Optional[Iterable[CustomSoftwareAction]] = None,
screenshots: Optional[Set[str]] = None, screenshots: Optional[Set[str]] = None,
license: Optional[str] = None, license: Optional[str] = None,
installed: bool = False, installed: bool = False,
@@ -31,7 +55,6 @@ class SnapApplication(SoftwarePackage):
self.notes = notes self.notes = notes
self.confinement = confinement self.confinement = confinement
self.verified_publisher = verified_publisher self.verified_publisher = verified_publisher
self.extra_actions = extra_actions
self.screenshots = screenshots self.screenshots = screenshots
self.download_size = download_size self.download_size = download_size
self.developer = developer self.developer = developer
@@ -94,7 +117,7 @@ class SnapApplication(SoftwarePackage):
def get_custom_actions(self) -> Optional[Iterable[CustomSoftwareAction]]: def get_custom_actions(self) -> Optional[Iterable[CustomSoftwareAction]]:
if self.installed: if self.installed:
return self.extra_actions return self.actions()
def supports_backup(self) -> bool: def supports_backup(self) -> bool:
return True return True