From 7cbd745af1c455cdb475001279c69f35ff66c67e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Moreira?= Date: Fri, 20 Dec 2019 17:45:32 -0300 Subject: [PATCH] [snap] suggestions retrieved from bauh-files --- CHANGELOG.md | 1 + bauh/gems/snap/__init__.py | 1 + bauh/gems/snap/controller.py | 48 ++++++++++++++---------- bauh/gems/snap/resources/suggestions.txt | 10 +++++ bauh/gems/snap/suggestions.py | 13 ------- bauh/gems/web/__init__.py | 2 +- bauh/view/core/controller.py | 2 +- 7 files changed, 42 insertions(+), 35 deletions(-) create mode 100644 bauh/gems/snap/resources/suggestions.txt delete mode 100644 bauh/gems/snap/suggestions.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b96da46..449c05ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,7 @@ updates: - AUR: - an exception happens when retrieving matches from the cached AUR index - not using the optimized compilation settings if the custom makepkg file is not found during the installation process +- minor fixes ## [0.7.5] 2019-12-20 ### Fixes diff --git a/bauh/gems/snap/__init__.py b/bauh/gems/snap/__init__.py index bd219398..c084fb7d 100644 --- a/bauh/gems/snap/__init__.py +++ b/bauh/gems/snap/__init__.py @@ -6,3 +6,4 @@ ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) SNAP_CACHE_PATH = CACHE_PATH + '/snap' CATEGORIES_FILE_PATH = SNAP_CACHE_PATH + '/categories.txt' URL_CATEGORIES_FILE = 'https://raw.githubusercontent.com/vinifmor/bauh-files/master/snap/categories.txt' +SUGGESTIONS_FILE = 'https://raw.githubusercontent.com/vinifmor/bauh-files/master/snap/suggestions.txt' diff --git a/bauh/gems/snap/controller.py b/bauh/gems/snap/controller.py index 608740ff..65936fec 100644 --- a/bauh/gems/snap/controller.py +++ b/bauh/gems/snap/controller.py @@ -13,7 +13,7 @@ from bauh.api.abstract.view import SingleSelectComponent, SelectViewType, InputO from bauh.commons.category import CategoriesDownloader from bauh.commons.html import bold from bauh.commons.system import SystemProcess, ProcessHandler, new_root_subprocess -from bauh.gems.snap import snap, suggestions, URL_CATEGORIES_FILE, SNAP_CACHE_PATH, CATEGORIES_FILE_PATH +from bauh.gems.snap import snap, URL_CATEGORIES_FILE, SNAP_CACHE_PATH, CATEGORIES_FILE_PATH, SUGGESTIONS_FILE from bauh.gems.snap.constants import SNAP_API_URL from bauh.gems.snap.model import SnapApplication from bauh.gems.snap.worker import SnapAsyncDataLoader @@ -239,31 +239,39 @@ class SnapManager(SoftwareManager): res = [] if snap.is_snapd_running(): - sugs = [(i, p) for i, p in suggestions.ALL.items()] - sugs.sort(key=lambda t: t[1].value, reverse=True) + self.logger.info('Downloading suggestions file {}'.format(SUGGESTIONS_FILE)) + file = self.http_client.get(SUGGESTIONS_FILE) - threads = [] - self.categories_downloader.join() + if not file or not file.text: + self.logger.warning("No suggestion found in {}".format(SUGGESTIONS_FILE)) + return res + else: + self.logger.info('Mapping suggestions') + self.categories_downloader.join() - for sug in sugs: + suggestions, threads = [], [] - if limit <= 0 or len(res) < limit: - cached_sug = self.suggestions_cache.get(sug[0]) + for l in file.text.split('\n'): + if l: + lsplit = l.strip().split('=') - if cached_sug: - res.append(cached_sug) - else: - t = Thread(target=self._fill_suggestion, args=(sug[0], sug[1], res)) - t.start() - threads.append(t) - time.sleep(0.001) # to avoid being blocked - else: - break + if limit <= 0 or len(suggestions) < limit: + cached_sug = self.suggestions_cache.get(lsplit[1]) - for t in threads: - t.join() + if cached_sug: + res.append(cached_sug) + else: + t = Thread(target=self._fill_suggestion, args=(lsplit[1], SuggestionPriority(int(lsplit[0])), res)) + t.start() + threads.append(t) + time.sleep(0.001) # to avoid being blocked + else: + break - res.sort(key=lambda s: s.priority.value, reverse=True) + for t in threads: + t.join() + + res.sort(key=lambda s: s.priority.value, reverse=True) return res def is_default_enabled(self) -> bool: diff --git a/bauh/gems/snap/resources/suggestions.txt b/bauh/gems/snap/resources/suggestions.txt new file mode 100644 index 00000000..ccfad2f8 --- /dev/null +++ b/bauh/gems/snap/resources/suggestions.txt @@ -0,0 +1,10 @@ +3=skype +2=drawing +2=obs-studio +2=eclispe +1=krita +1=audacity +0=supertuxkart +0=retroarch +0=chromium +0=matroska-tools diff --git a/bauh/gems/snap/suggestions.py b/bauh/gems/snap/suggestions.py deleted file mode 100644 index f96e9c9d..00000000 --- a/bauh/gems/snap/suggestions.py +++ /dev/null @@ -1,13 +0,0 @@ -from bauh.api.abstract.model import SuggestionPriority - -ALL = { - 'whatsdesk': SuggestionPriority.TOP, - 'slack': SuggestionPriority.HIGH, - 'discord': SuggestionPriority.HIGH, - 'yakyak': SuggestionPriority.MEDIUM, - 'instagraph': SuggestionPriority.MEDIUM, - 'gimp': SuggestionPriority.MEDIUM, - 'pycharm-professional': SuggestionPriority.LOW, - 'eclipse': SuggestionPriority.LOW, - 'supertuxkart': SuggestionPriority.LOW -} \ No newline at end of file diff --git a/bauh/gems/web/__init__.py b/bauh/gems/web/__init__.py index 436c063d..800fb765 100644 --- a/bauh/gems/web/__init__.py +++ b/bauh/gems/web/__init__.py @@ -23,7 +23,7 @@ URL_SUGGESTIONS = "https://raw.githubusercontent.com/vinifmor/bauh-files/master/ UA_CHROME = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36' TEMP_PATH = '/tmp/bauh/web' SEARCH_INDEX_FILE = '{}/index.yml'.format(TEMP_PATH) -SUGGESTIONS_CACHE_FILE = '{}/suggestions.yml'.format(TEMP_PATH) +SUGGESTIONS_CACHE_FILE = '{}/suggestions.txt'.format(TEMP_PATH) CONFIG_FILE = '{}/web.yml'.format(CONFIG_PATH) NATIVEFIER_URL = 'https://github.com/jiahaog/nativefier/archive/v7.7.0.tar.gz' diff --git a/bauh/view/core/controller.py b/bauh/view/core/controller.py index 786edb6b..e6dff286 100755 --- a/bauh/view/core/controller.py +++ b/bauh/view/core/controller.py @@ -336,7 +336,7 @@ class GenericSoftwareManager(SoftwareManager): self.logger.info(man.__class__.__name__ + ' took {0:.2f} seconds'.format(mtf - mti)) if man_sugs: - if len(man_sugs) > limit: + if 0 < limit < len(man_sugs): man_sugs = man_sugs[0:limit] suggestions.extend(man_sugs)