diff --git a/bauh/gems/snap/controller.py b/bauh/gems/snap/controller.py index 1e30b67b..907ab6ce 100644 --- a/bauh/gems/snap/controller.py +++ b/bauh/gems/snap/controller.py @@ -13,7 +13,7 @@ from bauh.commons.system import SystemProcess, ProcessHandler from bauh.gems.snap import snap, suggestions from bauh.gems.snap.constants import SNAP_API_URL from bauh.gems.snap.model import SnapApplication -from bauh.gems.snap.worker import SnapAsyncDataLoader +from bauh.gems.snap.worker import SnapAsyncDataLoader, CategoriesDownloader class SnapManager(SoftwareManager): @@ -27,6 +27,8 @@ class SnapManager(SoftwareManager): self.http_client = context.http_client self.logger = context.logger self.ubuntu_distro = context.distro == 'ubuntu' + self.categories = {} + self.categories_downloader = CategoriesDownloader(self.http_client, self.logger) def map_json(self, app_json: dict, installed: bool, disk_loader: DiskCacheLoader, internet: bool = True) -> SnapApplication: app = SnapApplication(publisher=app_json.get('publisher'), @@ -42,6 +44,8 @@ class SnapManager(SoftwareManager): if app.publisher: app.publisher = app.publisher.replace('*', '') + app.categories = self.categories.get(app.name) + app.installed = installed api_data = self.api_cache.get(app_json['name']) @@ -139,7 +143,7 @@ class SnapManager(SoftwareManager): return ProcessHandler(watcher).handle(SystemProcess(subproc=snap.refresh_and_stream(pkg.name, root_password))) def prepare(self): - pass + self.categories = self.categories_downloader.get_categories() def list_updates(self, internet_available: bool) -> List[PackageUpdate]: pass diff --git a/bauh/gems/snap/model.py b/bauh/gems/snap/model.py index 1f4236c2..711d67e4 100644 --- a/bauh/gems/snap/model.py +++ b/bauh/gems/snap/model.py @@ -28,6 +28,9 @@ class SnapApplication(SoftwarePackage): self.confinement = confinement self.has_apps_field = has_apps_field + def supports_disk_cache(self): + return self.installed + def has_history(self): return False @@ -49,13 +52,6 @@ class SnapApplication(SoftwarePackage): def is_application(self) -> bool: return not self.installed or ((self.has_apps_field is None or self.has_apps_field) and self.name.lower() not in KNOWN_RUNTIME_NAMES) - def _name_starts_with(self, words: set): - for word in words: - if self.name.startswith(word): - return True - - return False - def get_disk_cache_path(self): return super(SnapApplication, self).get_disk_cache_path() + '/installed/' + self.name @@ -63,18 +59,16 @@ class SnapApplication(SoftwarePackage): return { "icon_url": self.icon_url, 'confinement': self.confinement, - 'description': self.description + 'description': self.description, + 'categories': self.categories } def fill_cached_data(self, data: dict): if data: - for base_attr in ('icon_url', 'description'): + for base_attr in ('icon_url', 'description', 'confinement', 'categories'): if data.get(base_attr): setattr(self, base_attr, data[base_attr]) - if data.get('confinement'): - self.confinement = data['confinement'] - def can_be_run(self) -> bool: return self.installed and self.is_application() diff --git a/bauh/gems/snap/worker.py b/bauh/gems/snap/worker.py index 72dd76c2..2e397888 100644 --- a/bauh/gems/snap/worker.py +++ b/bauh/gems/snap/worker.py @@ -1,10 +1,14 @@ +import logging +import os import traceback from threading import Thread +from typing import Dict, List from bauh.api.abstract.cache import MemoryCache from bauh.api.abstract.context import ApplicationContext from bauh.api.abstract.controller import SoftwareManager from bauh.api.abstract.model import PackageStatus +from bauh.api.http import HttpClient from bauh.gems.snap import snap from bauh.gems.snap.constants import SNAP_API_URL from bauh.gems.snap.model import SnapApplication @@ -68,3 +72,29 @@ class SnapAsyncDataLoader(Thread): if self.persist: self.manager.cache_to_disk(pkg=self.app, icon_bytes=None, only_icon=False) + + +class CategoriesDownloader: + + URL_CATEGORIES_FILE = 'https://raw.githubusercontent.com/vinifmor/bauh-files/master/snap/categories.txt' + + def __init__(self, http_client: HttpClient, logger: logging.Logger): + self.http_client = http_client + self.logger = logger + + def get_categories(self) -> Dict[str, List[str]]: + self.logger.info('Downloading Snap category definitions from {}'.format(self.URL_CATEGORIES_FILE)) + + res = self.http_client.get(self.URL_CATEGORIES_FILE, headers={'Authorization': 'token {}'.format(os.getenv('GITHUB_TOKEN'))}) + + if res: + categories_map = {} + for l in res.text.split('\n'): + if l: + data = l.split('=') + categories_map[data[0]] = [c.strip() for c in data[1].split(',') if c] + + self.logger.info('Loaded categories for {} Snap applications'.format(len(categories_map))) + return categories_map + else: + self.logger.info('Could not download {}'.format(self.URL_CATEGORIES_FILE)) diff --git a/bauh/view/resources/locale/es b/bauh/view/resources/locale/es index 0c29be83..b6f5a9c0 100644 --- a/bauh/view/resources/locale/es +++ b/bauh/view/resources/locale/es @@ -146,4 +146,21 @@ movies=películas movie=película library=biblioteca books=libros -applications=aplicativos \ No newline at end of file +applications=aplicativos +social=social +productivity=productividad +utilities=utilidades +photo=foto +video=vídeo +entertainment=entretenimiento +art=arte +design=diseño +reference=referencia +personalisation=personalización +education=educación +health=salud +fitness=fitness +science=ciencias +news=noticias +weather=tiempo +finance=finanzas \ No newline at end of file diff --git a/bauh/view/resources/locale/pt b/bauh/view/resources/locale/pt index 15330092..3cfcdcf7 100644 --- a/bauh/view/resources/locale/pt +++ b/bauh/view/resources/locale/pt @@ -148,4 +148,21 @@ movie=filme media=mídia library=biblioteca books=livros -applications=aplicativos \ No newline at end of file +applications=aplicativos +social=social +productivity=produtividade +utilities=utilidades +photo=foto +video=vídeo +entertainment=entretenimento +art=arte +design=desenho +reference=referência +personalisation=personalização +education=educação +health=saúde +fitness=fitness +science=ciências +news=notícias +weather=tempo +finance=finanças