From 1a4ccfcefe6cbc38bd31d1ef341a17125c2bd23d Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Thu, 24 Oct 2019 17:37:42 -0300 Subject: [PATCH] [flatpak] Formatting the API categories to the same format provided by the other packaging technologies --- CHANGELOG.md | 1 + bauh/gems/flatpak/controller.py | 4 +++- bauh/gems/flatpak/worker.py | 18 ++++++++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59768601..5fba51e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - showing an error popup when **AppImageLauncher** messes up with an application installation - Flatpak: - Runtimes now are categorized as "runtime" + - Formatting the API categories to the same format provided by the other packaging technologies - AUR: - showing a "user-friendly" popup when there are integrity issues with the source-files of a building package - not waiting for the categories file to be retrieved from the cloud during application boot ( reduces boot time ) diff --git a/bauh/gems/flatpak/controller.py b/bauh/gems/flatpak/controller.py index e87ef831..0da44b1c 100644 --- a/bauh/gems/flatpak/controller.py +++ b/bauh/gems/flatpak/controller.py @@ -25,6 +25,7 @@ def __init__(self, context: ApplicationContext): super(FlatpakManager, self).__init__(context=context) self.i18n = context.i18n self.api_cache = context.cache_factory.new() + self.category_cache = context.cache_factory.new() context.disk_loader_factory.map(FlatpakApplication, self.api_cache) self.enabled = True self.http_client = context.http_client @@ -46,7 +47,8 @@ def _map_to_model(self, app_json: dict, installed: bool, disk_loader: DiskCacheL disk_loader.fill(app) # preloading cached disk data if internet: - FlatpakAsyncDataLoader(app=app, api_cache=self.api_cache, manager=self, context=self.context).start() + FlatpakAsyncDataLoader(app=app, api_cache=self.api_cache, manager=self, + context=self.context, category_cache=self.category_cache).start() else: app.fill_cached_data(api_data) diff --git a/bauh/gems/flatpak/worker.py b/bauh/gems/flatpak/worker.py index dcb5e933..cbc9812e 100644 --- a/bauh/gems/flatpak/worker.py +++ b/bauh/gems/flatpak/worker.py @@ -1,3 +1,4 @@ +import re import traceback from threading import Thread @@ -9,10 +10,12 @@ from bauh.gems.flatpak.constants import FLATHUB_API_URL, FLATHUB_URL from bauh.gems.flatpak.model import FlatpakApplication +RE_SPLIT_UPPER = re.compile(r'[A-Z][a-z]*') + class FlatpakAsyncDataLoader(Thread): - def __init__(self, app: FlatpakApplication, manager: SoftwareManager, context: ApplicationContext, api_cache: MemoryCache): + def __init__(self, app: FlatpakApplication, manager: SoftwareManager, context: ApplicationContext, api_cache: MemoryCache, category_cache: MemoryCache): super(FlatpakAsyncDataLoader, self).__init__(daemon=True) self.app = app self.manager = manager @@ -20,6 +23,7 @@ def __init__(self, app: FlatpakApplication, manager: SoftwareManager, context: A self.api_cache = api_cache self.persist = False self.logger = context.logger + self.category_cache = category_cache def run(self): if self.app: @@ -51,7 +55,17 @@ def run(self): self.app.icon_url = FLATHUB_URL + self.app.icon_url if data.get('categories'): - self.app.categories = [c['name'] for c in data['categories']] + cats = [] + for c in data['categories']: + cached = self.category_cache.get(c['name']) + + if not cached: + cached = ' '.join(RE_SPLIT_UPPER.findall(c['name'])).lower() + self.category_cache.add_non_existing(c['name'], cached) + + cats.append(cached) + + self.app.categories = cats loaded_data = self.app.get_data_to_cache()