From 16586a7d2886876c5bdd697dbf5203a2425e50ea Mon Sep 17 00:00:00 2001 From: Vinicius Moreira Date: Fri, 25 Oct 2019 11:22:23 -0300 Subject: [PATCH] [flatpak] fix: category formatting | sorting categories --- CHANGELOG.md | 1 + bauh/gems/flatpak/worker.py | 20 ++++++++++++++++---- bauh/view/qt/commons.py | 3 ++- bauh/view/qt/window.py | 17 ++++++++++------- tests/gems/flatpak/__init__.py | 0 tests/gems/flatpak/worker_test.py | 20 ++++++++++++++++++++ 6 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 tests/gems/flatpak/__init__.py create mode 100644 tests/gems/flatpak/worker_test.py diff --git a/CHANGELOG.md b/CHANGELOG.md index acd24e20..7f72eb08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - 3 password attempts for root authentication - not changing the table applied filters after a uninstall - cleaning the progress bar substatus after each upgrade +- sorted categories - AppImage: - showing an error popup when **AppImageLauncher** messes up with an application installation - Flatpak: diff --git a/bauh/gems/flatpak/worker.py b/bauh/gems/flatpak/worker.py index cdf162ed..5f581146 100644 --- a/bauh/gems/flatpak/worker.py +++ b/bauh/gems/flatpak/worker.py @@ -1,5 +1,5 @@ -import re import traceback +from io import StringIO from threading import Thread from bauh.api.abstract.cache import MemoryCache @@ -10,8 +10,6 @@ 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): @@ -25,6 +23,20 @@ def __init__(self, app: FlatpakApplication, manager: SoftwareManager, context: A self.logger = context.logger self.category_cache = category_cache + @staticmethod + def format_category(category: str) -> str: + word = StringIO() + last_l = None + for idx, l in enumerate(category): + if idx != 0 and last_l != ' ' and l.isupper() and idx + 1 < len(category) and category[idx + 1].islower(): + word.write(' ') + + last_l = l.lower() + word.write(last_l) + + word.seek(0) + return word.read() + def run(self): if self.app: self.app.status = PackageStatus.LOADING_DATA @@ -60,7 +72,7 @@ def run(self): cached = self.category_cache.get(c['name']) if not cached: - cached = ' '.join(RE_SPLIT_UPPER.findall(c['name'])).lower() if not c['name'].isupper() else c['name'].lower() + cached = self.format_category(c['name']) self.category_cache.add_non_existing(c['name'], cached) cats.append(cached) diff --git a/bauh/view/qt/commons.py b/bauh/view/qt/commons.py index 6ba6f90b..4d652109 100644 --- a/bauh/view/qt/commons.py +++ b/bauh/view/qt/commons.py @@ -31,7 +31,8 @@ def update_info(pkgv: PackageView, pkgs_info: dict): pkgs_info['updates'] += 1 if pkgv.model.categories: - pkgs_info['categories'].update([s.lower() for s in pkgv.model.categories]) + for c in pkgv.model.categories: + pkgs_info['categories'].add(c.lower()) pkgs_info['pkgs'].append(pkgv) pkgs_info['not_installed'] += 1 if not pkgv.model.installed else 0 diff --git a/bauh/view/qt/window.py b/bauh/view/qt/window.py index f5fe6bee..a4272147 100755 --- a/bauh/view/qt/window.py +++ b/bauh/view/qt/window.py @@ -417,7 +417,8 @@ def _reload_categories(self): for p in self.pkgs_available: if p.model.categories: - categories.update(p.model.categories) + for c in p.model.categories: + categories.add(c.lower()) if categories: self._update_categories(categories, keep_selected=True) @@ -727,7 +728,7 @@ def _update_categories(self, categories: Set[str] = None, keep_selected: bool = if categories is None: self.ref_combo_categories.setVisible(self.combo_categories.count() > 1) else: - keeping_selected = keep_selected and categories and self.category_filter in {c.lower() for c in categories} + keeping_selected = keep_selected and categories and self.category_filter in categories if not keeping_selected: self.category_filter = self.any_category_filter @@ -738,13 +739,15 @@ def _update_categories(self, categories: Set[str] = None, keep_selected: bool = self.combo_categories.removeItem(1) selected_cat = -1 - for idx, c in enumerate(categories): - lower_cat = c.lower() - i18n_cat = self.i18n.get(lower_cat) + cat_list = list(categories) + cat_list.sort() + + for idx, c in enumerate(cat_list): + i18n_cat = self.i18n.get(c) cat_label = i18n_cat if i18n_cat else c - self.combo_categories.addItem(cat_label.capitalize(), lower_cat) + self.combo_categories.addItem(cat_label.capitalize(), c) - if keeping_selected and lower_cat == self.category_filter: + if keeping_selected and c == self.category_filter: selected_cat = idx + 1 self.combo_categories.blockSignals(True) diff --git a/tests/gems/flatpak/__init__.py b/tests/gems/flatpak/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/gems/flatpak/worker_test.py b/tests/gems/flatpak/worker_test.py new file mode 100644 index 00000000..cdc7886d --- /dev/null +++ b/tests/gems/flatpak/worker_test.py @@ -0,0 +1,20 @@ +from unittest import TestCase + +from bauh.gems.flatpak.worker import FlatpakAsyncDataLoader + + +class FlatpakAsyncDataLoaderTest(TestCase): + + def test_format_category(self): + self.assertEqual('irc client', FlatpakAsyncDataLoader.format_category('IRCClient')) + self.assertEqual('text editor', FlatpakAsyncDataLoader.format_category('TextEditor')) + self.assertEqual('text editor', FlatpakAsyncDataLoader.format_category('Text Editor')) + self.assertEqual('text editor', FlatpakAsyncDataLoader.format_category('text editor')) + self.assertEqual('text editor', FlatpakAsyncDataLoader.format_category('Text editor')) + self.assertEqual('text editor', FlatpakAsyncDataLoader.format_category('text Editor')) + self.assertEqual('text editor', FlatpakAsyncDataLoader.format_category('textEditor')) + self.assertEqual('ide', FlatpakAsyncDataLoader.format_category('IDE')) + self.assertEqual('faster irc client', FlatpakAsyncDataLoader.format_category('Faster IRCClient')) + self.assertEqual('3d graphics', FlatpakAsyncDataLoader.format_category('3DGraphics')) + self.assertEqual('32d graphics', FlatpakAsyncDataLoader.format_category('32DGraphics')) + self.assertEqual('d32 graphics', FlatpakAsyncDataLoader.format_category('D32Graphics'))