mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-11 08:54:15 +02:00
[api] improvement: SoftwareManager.get_screenshots returning a generator instead of a list
This commit is contained in:
@@ -7,6 +7,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|||||||
## [0.X.X]
|
## [0.X.X]
|
||||||
|
|
||||||
### Improvements
|
### Improvements
|
||||||
|
- General
|
||||||
|
- minor memory improvements
|
||||||
|
|
||||||
- Arch
|
- Arch
|
||||||
- info dialog:
|
- info dialog:
|
||||||
- displaying the "executable" field if the package is recognized as an application
|
- displaying the "executable" field if the package is recognized as an application
|
||||||
|
|||||||
@@ -355,10 +355,9 @@ class SoftwareManager(ABC):
|
|||||||
def launch(self, pkg: SoftwarePackage):
|
def launch(self, pkg: SoftwarePackage):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
def get_screenshots(self, pkg: SoftwarePackage) -> Generator[str, None, None]:
|
||||||
def get_screenshots(self, pkg: SoftwarePackage) -> List[str]:
|
|
||||||
"""
|
"""
|
||||||
:return: screenshot urls for the given package
|
:return: yields screenshot urls for the given package
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -795,11 +795,9 @@ class AppImageManager(SoftwareManager):
|
|||||||
def cache_to_disk(self, pkg: SoftwarePackage, icon_bytes: Optional[bytes], only_icon: bool):
|
def cache_to_disk(self, pkg: SoftwarePackage, icon_bytes: Optional[bytes], only_icon: bool):
|
||||||
self.serialize_to_disk(pkg, icon_bytes, only_icon)
|
self.serialize_to_disk(pkg, icon_bytes, only_icon)
|
||||||
|
|
||||||
def get_screenshots(self, pkg: AppImage) -> List[str]:
|
def get_screenshots(self, pkg: AppImage) -> Generator[str, None, None]:
|
||||||
if pkg.has_screenshots():
|
if pkg.url_screenshot:
|
||||||
return [pkg.url_screenshot]
|
yield pkg.url_screenshot
|
||||||
|
|
||||||
return []
|
|
||||||
|
|
||||||
def clear_data(self, logs: bool = True):
|
def clear_data(self, logs: bool = True):
|
||||||
for f in glob.glob(f'{APPIMAGE_SHARED_DIR}/*.db'):
|
for f in glob.glob(f'{APPIMAGE_SHARED_DIR}/*.db'):
|
||||||
|
|||||||
@@ -2790,9 +2790,6 @@ class ArchManager(SoftwareManager):
|
|||||||
final_cmd = pkg.command.replace('%U', '')
|
final_cmd = pkg.command.replace('%U', '')
|
||||||
subprocess.Popen(final_cmd, shell=True)
|
subprocess.Popen(final_cmd, shell=True)
|
||||||
|
|
||||||
def get_screenshots(self, pkg: SoftwarePackage) -> List[str]:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def _gen_bool_selector(self, id_: str, label_key: str, tooltip_key: str, value: bool, max_width: int,
|
def _gen_bool_selector(self, id_: str, label_key: str, tooltip_key: str, value: bool, max_width: int,
|
||||||
capitalize_label: bool = True, label_params: Optional[list] = None, tooltip_params: Optional[list] = None) -> SingleSelectComponent:
|
capitalize_label: bool = True, label_params: Optional[list] = None, tooltip_params: Optional[list] = None) -> SingleSelectComponent:
|
||||||
opts = [InputOption(label=self.i18n['yes'].capitalize(), value=True),
|
opts = [InputOption(label=self.i18n['yes'].capitalize(), value=True),
|
||||||
|
|||||||
@@ -5,10 +5,11 @@ from datetime import datetime
|
|||||||
from math import floor
|
from math import floor
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from typing import List, Set, Type, Tuple, Optional
|
from typing import List, Set, Type, Tuple, Optional, Generator
|
||||||
|
|
||||||
from packaging.version import Version
|
from packaging.version import Version
|
||||||
|
|
||||||
|
from bauh.api import user
|
||||||
from bauh.api.abstract.controller import SearchResult, SoftwareManager, ApplicationContext, UpgradeRequirements, \
|
from bauh.api.abstract.controller import SearchResult, SoftwareManager, ApplicationContext, UpgradeRequirements, \
|
||||||
UpgradeRequirement, TransactionResult, SoftwareAction
|
UpgradeRequirement, TransactionResult, SoftwareAction
|
||||||
from bauh.api.abstract.disk import DiskCacheLoader
|
from bauh.api.abstract.disk import DiskCacheLoader
|
||||||
@@ -17,11 +18,11 @@ from bauh.api.abstract.model import PackageHistory, PackageUpdate, SoftwarePacka
|
|||||||
SuggestionPriority, PackageStatus
|
SuggestionPriority, PackageStatus
|
||||||
from bauh.api.abstract.view import MessageType, FormComponent, SingleSelectComponent, InputOption, SelectViewType, \
|
from bauh.api.abstract.view import MessageType, FormComponent, SingleSelectComponent, InputOption, SelectViewType, \
|
||||||
ViewComponent, PanelComponent
|
ViewComponent, PanelComponent
|
||||||
from bauh.api import user
|
|
||||||
from bauh.commons.boot import CreateConfigFile
|
from bauh.commons.boot import CreateConfigFile
|
||||||
from bauh.commons.html import strip_html, bold
|
from bauh.commons.html import strip_html, bold
|
||||||
from bauh.commons.system import ProcessHandler
|
from bauh.commons.system import ProcessHandler
|
||||||
from bauh.gems.flatpak import flatpak, SUGGESTIONS_FILE, CONFIG_FILE, UPDATES_IGNORED_FILE, FLATPAK_CONFIG_DIR, EXPORTS_PATH, \
|
from bauh.gems.flatpak import flatpak, SUGGESTIONS_FILE, CONFIG_FILE, UPDATES_IGNORED_FILE, FLATPAK_CONFIG_DIR, \
|
||||||
|
EXPORTS_PATH, \
|
||||||
get_icon_path, VERSION_1_5, VERSION_1_2
|
get_icon_path, VERSION_1_5, VERSION_1_2
|
||||||
from bauh.gems.flatpak.config import FlatpakConfigManager
|
from bauh.gems.flatpak.config import FlatpakConfigManager
|
||||||
from bauh.gems.flatpak.constants import FLATHUB_API_URL
|
from bauh.gems.flatpak.constants import FLATHUB_API_URL
|
||||||
@@ -574,16 +575,16 @@ class FlatpakManager(SoftwareManager):
|
|||||||
def launch(self, pkg: FlatpakApplication):
|
def launch(self, pkg: FlatpakApplication):
|
||||||
flatpak.run(str(pkg.id))
|
flatpak.run(str(pkg.id))
|
||||||
|
|
||||||
def get_screenshots(self, pkg: SoftwarePackage) -> List[str]:
|
def get_screenshots(self, pkg: FlatpakApplication) -> Generator[str, None, None]:
|
||||||
screenshots_url = '{}/apps/{}'.format(FLATHUB_API_URL, pkg.id)
|
screenshots_url = f'{FLATHUB_API_URL}/apps/{pkg.id}'
|
||||||
urls = []
|
|
||||||
try:
|
try:
|
||||||
res = self.http_client.get_json(screenshots_url)
|
res = self.http_client.get_json(screenshots_url)
|
||||||
|
|
||||||
if res and res.get('screenshots'):
|
if res and res.get('screenshots'):
|
||||||
for s in res['screenshots']:
|
for s in res['screenshots']:
|
||||||
if s.get('imgDesktopUrl'):
|
if s.get('imgDesktopUrl'):
|
||||||
urls.append(s['imgDesktopUrl'])
|
yield s['imgDesktopUrl']
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if e.__class__.__name__ == 'JSONDecodeError':
|
if e.__class__.__name__ == 'JSONDecodeError':
|
||||||
@@ -591,8 +592,6 @@ class FlatpakManager(SoftwareManager):
|
|||||||
else:
|
else:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
return urls
|
|
||||||
|
|
||||||
def get_settings(self, screen_width: int, screen_height: int) -> Optional[ViewComponent]:
|
def get_settings(self, screen_width: int, screen_height: int) -> Optional[ViewComponent]:
|
||||||
if not self.context.root_user:
|
if not self.context.root_user:
|
||||||
fields = []
|
fields = []
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import re
|
|||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from typing import List, Set, Type, Optional, Tuple
|
from typing import List, Set, Type, Optional, Tuple, Generator
|
||||||
|
|
||||||
from bauh.api.abstract.controller import SoftwareManager, SearchResult, ApplicationContext, UpgradeRequirements, \
|
from bauh.api.abstract.controller import SoftwareManager, SearchResult, ApplicationContext, UpgradeRequirements, \
|
||||||
TransactionResult, SoftwareAction
|
TransactionResult, SoftwareAction
|
||||||
@@ -440,8 +440,9 @@ class SnapManager(SoftwareManager):
|
|||||||
self.logger.info(f"Running '{pkg.name}': {cmd}")
|
self.logger.info(f"Running '{pkg.name}': {cmd}")
|
||||||
snap.run(cmd)
|
snap.run(cmd)
|
||||||
|
|
||||||
def get_screenshots(self, pkg: SnapApplication) -> List[str]:
|
def get_screenshots(self, pkg: SnapApplication) -> Generator[str, None, None]:
|
||||||
return pkg.screenshots if pkg.has_screenshots() else []
|
if pkg.screenshots:
|
||||||
|
yield from pkg.screenshots
|
||||||
|
|
||||||
def get_settings(self, screen_width: int, screen_height: int) -> Optional[ViewComponent]:
|
def get_settings(self, screen_width: int, screen_height: int) -> Optional[ViewComponent]:
|
||||||
snap_config = self.configman.get_config()
|
snap_config = self.configman.get_config()
|
||||||
|
|||||||
@@ -1072,9 +1072,6 @@ class WebApplicationManager(SoftwareManager):
|
|||||||
def launch(self, pkg: WebApplication):
|
def launch(self, pkg: WebApplication):
|
||||||
subprocess.Popen(args=[pkg.get_command()], shell=True, env={**os.environ})
|
subprocess.Popen(args=[pkg.get_command()], shell=True, env={**os.environ})
|
||||||
|
|
||||||
def get_screenshots(self, pkg: SoftwarePackage) -> List[str]:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def clear_data(self, logs: bool = True):
|
def clear_data(self, logs: bool = True):
|
||||||
if os.path.exists(ENV_PATH):
|
if os.path.exists(ENV_PATH):
|
||||||
if logs:
|
if logs:
|
||||||
|
|||||||
@@ -520,11 +520,11 @@ class GenericSoftwareManager(SoftwareManager):
|
|||||||
self.logger.info(f'Launching {pkg}')
|
self.logger.info(f'Launching {pkg}')
|
||||||
man.launch(pkg)
|
man.launch(pkg)
|
||||||
|
|
||||||
def get_screenshots(self, pkg: SoftwarePackage):
|
def get_screenshots(self, pkg: SoftwarePackage) -> Generator[str, None, None]:
|
||||||
man = self._get_manager_for(pkg)
|
man = self._get_manager_for(pkg)
|
||||||
|
|
||||||
if man:
|
if man:
|
||||||
return man.get_screenshots(pkg)
|
yield from man.get_screenshots(pkg)
|
||||||
|
|
||||||
def get_working_managers(self):
|
def get_working_managers(self):
|
||||||
return [m for m in self.managers if self._can_work(m)]
|
return [m for m in self.managers if self._can_work(m)]
|
||||||
|
|||||||
@@ -1066,7 +1066,7 @@ class ShowScreenshots(AsyncAction):
|
|||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
if self.pkg:
|
if self.pkg:
|
||||||
self.notify_finished({'pkg': self.pkg, 'screenshots': self.manager.get_screenshots(self.pkg.model)})
|
self.notify_finished({'pkg': self.pkg, 'screenshots': tuple(self.manager.get_screenshots(self.pkg.model))})
|
||||||
|
|
||||||
self.pkg = None
|
self.pkg = None
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user