Skip to content

Commit

Permalink
[flatpak] fix: category formatting | sorting categories
Browse files Browse the repository at this point in the history
  • Loading branch information
vinifmor committed Oct 25, 2019
1 parent 669206e commit 16586a7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 16 additions & 4 deletions bauh/gems/flatpak/worker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re
import traceback
from io import StringIO
from threading import Thread

from bauh.api.abstract.cache import MemoryCache
Expand All @@ -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):

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion bauh/view/qt/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions bauh/view/qt/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
Empty file added tests/gems/flatpak/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions tests/gems/flatpak/worker_test.py
Original file line number Diff line number Diff line change
@@ -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'))

0 comments on commit 16586a7

Please sign in to comment.