From 14206762cfc9db4848f4518e6995d8c9dd0a682e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Moreira?= Date: Wed, 18 Dec 2019 13:18:06 -0300 Subject: [PATCH] [wgem] using the suggestions custom icon to install the application --- bauh/gems/web/controller.py | 47 ++++++++++++++++++++++++++++++++++++- bauh/gems/web/model.py | 3 ++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/bauh/gems/web/controller.py b/bauh/gems/web/controller.py index a83c5d4a..c0d7bf9d 100644 --- a/bauh/gems/web/controller.py +++ b/bauh/gems/web/controller.py @@ -465,6 +465,27 @@ class WebApplicationManager(SoftwareManager): confirmation_label=self.i18n['continue'].capitalize(), deny_label=self.i18n['cancel'].capitalize()) + def _download_suggestion_icon(self, pkg: WebApplication, app_dir: str) -> Tuple[str, bytes]: + try: + if self.http_client.exists(pkg.icon_url): + icon_path = '{}/{}'.format(app_dir, pkg.icon_url.split('/')[-1]) + + try: + res = self.http_client.get(pkg.icon_url) + if not res: + self.logger.info('Could not download the icon {}'.format(pkg.icon_url)) + else: + return icon_path, res.content + except: + self.logger.error("An exception has happened when downloading {}".format(pkg.icon_url)) + traceback.print_exc() + else: + self.logger.warning('Could no retrieve the icon {} defined for the suggestion {}'.format(pkg.icon_url, pkg.name)) + except: + self.logger.warning('An exception happened when trying to retrieve the icon {} for the suggestion {}'.format(pkg.icon_url, + pkg.name)) + traceback.print_exc() + def install(self, pkg: WebApplication, root_password: str, watcher: ProcessWatcher) -> bool: continue_install, install_options = self._ask_install_options(pkg, watcher) @@ -504,6 +525,23 @@ class WebApplicationManager(SoftwareManager): watcher.print('Fix found for {}'.format(pkg.url)) install_options.append('--inject={}'.format(fix_path)) + # if a custom icon is defined for an app suggestion: + icon_path, icon_bytes = None, None + if pkg.is_suggestion and pkg.icon_url and not {o for o in install_options if o.startswith('--icon')}: + download = self._download_suggestion_icon(pkg, app_dir) + + if download and download[1]: + icon_path, icon_bytes = download[0], download[1] + pkg.custom_icon = icon_path + + # writting the icon in a temporary folder to be used by the nativefier process + temp_icon_path = '/tmp/bauh/web/{}'.format(pkg.icon_url.split('/')[-1]) + install_options.append('--icon={}'.format(temp_icon_path)) + + self.logger.info("Writting a temp suggestion icon at {}".format(temp_icon_path)) + with open(temp_icon_path, 'wb+') as f: + f.write(icon_bytes) + watcher.change_substatus(self.i18n['web.install.substatus.call_nativefier'].format(bold('nativefier'))) electron_version = next((c for c in env_components if c.id == 'electron')).version @@ -540,6 +578,12 @@ class WebApplicationManager(SoftwareManager): with open(fix_path, 'w+') as f: f.write(fix) + # persisting the custom suggestion icon in the defitive directory + if icon_bytes: + self.logger.info("Writting the final custom suggestion icon at {}".format(icon_path)) + with open(icon_path, 'wb+') as f: + f.write(icon_bytes) + pkg.installation_dir = app_dir version_path = '{}/version'.format(app_dir) @@ -660,7 +704,8 @@ class WebApplicationManager(SoftwareManager): url=suggestion.get('url'), icon_url=suggestion.get('icon_url'), categories=[suggestion['category']] if suggestion.get('category') else None, - preset_options=suggestion.get('options')) + preset_options=suggestion.get('options'), + is_suggestion=True) app.set_version(suggestion.get('version')) diff --git a/bauh/gems/web/model.py b/bauh/gems/web/model.py index 87b80aaa..3d2933c6 100644 --- a/bauh/gems/web/model.py +++ b/bauh/gems/web/model.py @@ -10,7 +10,7 @@ class WebApplication(SoftwarePackage): def __init__(self, id: str = None, url: str = None, name: str = None, description: str = None, icon_url: str = None, installation_dir: str = None, desktop_entry: str = None, installed: bool = False, version: str = None, - categories: List[str] = None, custom_icon: str = None, preset_options: List[str] = None): + categories: List[str] = None, custom_icon: str = None, preset_options: List[str] = None, is_suggestion: bool = False): super(WebApplication, self).__init__(id=id if id else url, name=name, description=description, icon_url=icon_url, installed=installed, version=version, categories=categories) @@ -19,6 +19,7 @@ class WebApplication(SoftwarePackage): self.desktop_entry = desktop_entry self.set_custom_icon(custom_icon) self.preset_options = preset_options + self.is_suggestion = is_suggestion def set_version(self, version: str): self.version = str(version) if version else None