Beispiel #1
0
    def _map_suggestion(self, suggestion: dict) -> PackageSuggestion:
        app = WebApplication(name=suggestion.get('name'),
                             url=suggestion.get('url'),
                             icon_url=suggestion.get('icon_url'),
                             categories=[suggestion['category']]
                             if suggestion.get('category') else None,
                             preset_options=suggestion.get('options'),
                             save_icon=suggestion.get('save_icon', False))

        app.set_version(suggestion.get('version'))

        description = suggestion.get('description')

        if isinstance(description, dict):
            app.description = description.get(
                self.i18n.current_key, description.get(self.i18n.default_key))
        elif isinstance(description, str):
            app.description = description

        if not app.version and self.env_settings and self.env_settings.get(
                'electron'):
            app.version = self.env_settings['electron']['version']
            app.latest_version = app.version

        app.status = PackageStatus.LOADING_DATA

        Thread(target=self._fill_suggestion, args=(app, ), daemon=True).start()

        return PackageSuggestion(priority=SuggestionPriority(
            suggestion['priority']),
                                 package=app)
Beispiel #2
0
    def list_suggestions(self, limit: int,
                         filter_installed: bool) -> List[PackageSuggestion]:
        res = []

        connection = self._get_db_connection(DB_APPS_PATH)

        if connection:
            file = self.http_client.get(SUGGESTIONS_FILE)

            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")
                try:
                    sugs = [l for l in file.text.split('\n') if l]

                    if filter_installed:
                        installed = {
                            i.name.lower()
                            for i in self.read_installed(
                                disk_loader=None,
                                connection=connection).installed
                        }
                    else:
                        installed = None

                    sugs_map = {}

                    for s in sugs:
                        lsplit = s.split('=')

                        name = lsplit[1].strip()

                        if limit <= 0 or len(sugs_map) < limit:
                            if not installed or not name.lower() in installed:
                                sugs_map[name] = SuggestionPriority(
                                    int(lsplit[0]))
                        else:
                            break

                    cursor = connection.cursor()
                    cursor.execute(
                        query.FIND_APPS_BY_NAME_FULL.format(','.join(
                            ["'{}'".format(s) for s in sugs_map.keys()])))

                    for t in cursor.fetchall():
                        app = AppImage(*t,
                                       i18n=self.i18n,
                                       custom_actions=self.custom_app_actions)
                        res.append(
                            PackageSuggestion(app, sugs_map[app.name.lower()]))
                    self.logger.info("Mapped {} suggestions".format(len(res)))
                except:
                    traceback.print_exc()
                finally:
                    self._close_connection(DB_APPS_PATH, connection)

        return res
Beispiel #3
0
    def _fill_suggestion(self, appid: str, priority: SuggestionPriority, flatpak_version: Version, remote: str,
                         output: List[PackageSuggestion]):
        app_json = flatpak.search(flatpak_version, appid, remote, app_id=True)

        if app_json:
            model = PackageSuggestion(self._map_to_model(app_json[0], False, None)[0], priority)
            self.suggestions_cache.add(appid, model)
            output.append(model)
        else:
            self.logger.warning(f"Could not find Flatpak suggestions '{appid}'")
Beispiel #4
0
    def _fill_suggestion(self, pkg_name: str, priority: SuggestionPriority, out: List[PackageSuggestion]):
        res = self.http_client.get_json(SNAP_API_URL + '/search?q=package_name:{}'.format(pkg_name))

        if res and res['_embedded']['clickindex:package']:
            pkg = res['_embedded']['clickindex:package'][0]
            pkg['rev'] = pkg['revision']
            pkg['name'] = pkg_name

            out.append(PackageSuggestion(self.map_json(pkg, installed=False, disk_loader=None), priority))
        else:
            self.logger.warning("Could not retrieve suggestion '{}'".format(pkg_name))
Beispiel #5
0
    def list_suggestions(self, limit: int,
                         filter_installed: bool) -> List[PackageSuggestion]:
        cli_version = flatpak.get_version()
        res = []

        self.logger.info(
            "Downloading the suggestions file {}".format(SUGGESTIONS_FILE))
        file = self.http_client.get(SUGGESTIONS_FILE)

        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")
            remote_level = self._get_search_remote()
            installed = {
                i.id
                for i in self.read_installed(disk_loader=None).installed
            } if filter_installed else None

            for line in file.text.split('\n'):
                if line:
                    if limit <= 0 or len(res) < limit:
                        sug = line.split('=')
                        appid = sug[1].strip()

                        if installed and appid in installed:
                            continue

                        priority = SuggestionPriority(int(sug[0]))

                        cached_sug = self.suggestions_cache.get(appid)

                        if cached_sug:
                            res.append(cached_sug)
                        else:
                            app_json = flatpak.search(cli_version,
                                                      appid,
                                                      remote_level,
                                                      app_id=True)

                            if app_json:
                                model = PackageSuggestion(
                                    self._map_to_model(app_json[0], False,
                                                       None), priority)
                                self.suggestions_cache.add(appid, model)
                                res.append(model)
                    else:
                        break

            res.sort(key=lambda s: s.priority.value, reverse=True)
        return res
Beispiel #6
0
    def list_suggestions(self, limit: int, filter_installed: bool) -> Optional[List[PackageSuggestion]]:
        if limit == 0:
            return

        connection = self._get_db_connection(DATABASE_APPS_FILE)

        if connection:
            self.suggestions_downloader.taskman = TaskManager()
            suggestions = tuple(self.suggestions_downloader.read())

            if not suggestions:
                self.logger.warning("Could not read AppImage suggestions")
                return
            else:
                self.logger.info("Mapping AppImage suggestions")
                try:
                    if filter_installed:
                        installed = {i.name.lower() for i in self.read_installed(disk_loader=None,
                                                                                 connection=connection).installed}
                    else:
                        installed = None

                    sugs_map = {}

                    for s in suggestions:
                        lsplit = s.split('=')

                        name = lsplit[1].strip()

                        if limit < 0 or len(sugs_map) < limit:
                            if not installed or not name.lower() in installed:
                                sugs_map[name] = SuggestionPriority(int(lsplit[0]))
                        else:
                            break

                    cursor = connection.cursor()
                    cursor.execute(query.FIND_APPS_BY_NAME_FULL.format(','.join([f"'{s}'" for s in sugs_map.keys()])))

                    res = []
                    for t in cursor.fetchall():
                        app = AppImage(*t, i18n=self.i18n)
                        res.append(PackageSuggestion(app, sugs_map[app.name.lower()]))

                    self.logger.info(f"Mapped {len(res)} AppImage suggestions")
                    return res
                except:
                    traceback.print_exc()
                finally:
                    connection.close()
Beispiel #7
0
    def _fill_suggestion(self, name: str, priority: SuggestionPriority,
                         snapd_client: SnapdClient,
                         out: List[PackageSuggestion]):
        res = snapd_client.find_by_name(name)

        if res:
            if len(res) == 1:
                app_json = res[0]
            else:
                jsons_found = [p for p in res if p['name'] == name]
                app_json = jsons_found[0] if jsons_found else None

            if app_json:
                sug = PackageSuggestion(self._map_to_app(app_json, False),
                                        priority)
                self.suggestions_cache.add(name, sug)
                out.append(sug)
                return

        self.logger.warning("Could not retrieve suggestion '{}'".format(name))
Beispiel #8
0
    def list_suggestions(self, limit: int) -> List[PackageSuggestion]:
        res = []

        sugs = [(i, p) for i, p in suggestions.ALL.items()]
        sugs.sort(key=lambda t: t[1].value, reverse=True)

        if limit > 0:
            sugs = sugs[0:limit]

        sug_names = {s[0] for s in sugs}

        api_res = self.aur_client.get_info(sug_names)

        if api_res:
            for pkg in api_res:
                if pkg.get('Name') in sug_names:
                    res.append(
                        PackageSuggestion(self.mapper.map_api_data(pkg, {}),
                                          suggestions.ALL.get(pkg['Name'])))

        return res
Beispiel #9
0
    def list_suggestions(self, limit: int,
                         filter_installed: bool) -> List[PackageSuggestion]:
        self.logger.info(
            "Downloading suggestions file {}".format(SUGGESTIONS_FILE))
        file = self.http_client.get(SUGGESTIONS_FILE)

        if not file or not file.text:
            self.logger.warning(
                "No suggestion could be read from {}".format(SUGGESTIONS_FILE))
        else:
            self.logger.info("Mapping suggestions")
            suggestions = {}

            for l in file.text.split('\n'):
                if l:
                    if limit <= 0 or len(suggestions) < limit:
                        lsplit = l.split('=')
                        name = lsplit[1].strip()

                        if not filter_installed or not pacman.check_installed(
                                name):
                            suggestions[name] = SuggestionPriority(
                                int(lsplit[0]))

            api_res = self.aur_client.get_info(suggestions.keys())

            if api_res:
                res = []
                self.categories_mapper.join()
                for pkg in api_res:
                    if pkg.get('Name') in suggestions:
                        res.append(
                            PackageSuggestion(
                                self.mapper.map_api_data(
                                    pkg, {}, self.categories),
                                suggestions[pkg['Name']]))

                self.logger.info("Mapped {} suggestions".format(
                    len(suggestions)))
                return res
Beispiel #10
0
    def list_suggestions(self, limit: int) -> List[PackageSuggestion]:
        cli_version = flatpak.get_version()
        res = []

        sugs = [(i, p) for i, p in suggestions.ALL.items()]
        sugs.sort(key=lambda t: t[1].value, reverse=True)

        for sug in sugs:

            if limit <= 0 or len(res) < limit:
                app_json = flatpak.search(cli_version, sug[0], app_id=True)

                if app_json:
                    res.append(
                        PackageSuggestion(
                            self._map_to_model(app_json[0], False, None),
                            sug[1]))
            else:
                break

        res.sort(key=lambda s: s.priority.value, reverse=True)
        return res
Beispiel #11
0
    def list_suggestions(self, limit: int, filter_installed: bool) -> Optional[List[PackageSuggestion]]:
        name_priority = dict()

        fill_suggestions = Thread(target=self._fill_suggestions, args=(name_priority,))
        fill_suggestions.start()

        if filter_installed:
            installed = set()
            fill_installed = Thread(target=self._fill_installed_names, args=(installed, ))
            fill_installed.start()
        else:
            installed, fill_installed = None, None

        fill_suggestions.join()

        if fill_installed:
            fill_installed.join()

        if not name_priority:
            self._log.info("No Debian package suggestions found")
            return []

        self._log.info(f"Found {len(name_priority)} Debian package suggestions")

        to_load = tuple(name_priority.keys()) if not installed else {*name_priority.keys()}.difference(installed)

        if not to_load:
            return []

        suggestions = []

        for pkg in self.aptitude.search_by_name(to_load):
            prio = name_priority.get(pkg.name)

            if prio:
                suggestions.append(PackageSuggestion(package=pkg, priority=prio))

        return suggestions
Beispiel #12
0
    def list_suggestions(self, limit: int) -> List[PackageSuggestion]:
        res = []

        connection = self._get_db_connection(DB_APPS_PATH)

        if connection:
            try:
                sugs = [(i, p) for i, p in suggestions.ALL.items()]
                sugs.sort(key=lambda t: t[1].value, reverse=True)

                if limit > 0:
                    sugs = sugs[0:limit]

                cursor = connection.cursor()
                cursor.execute(query.FIND_APPS_BY_NAME_FULL.format(','.join(["'{}'".format(s[0]) for s in sugs])))

                for t in cursor.fetchall():
                    app = AppImage(*t)
                    res.append(PackageSuggestion(app, suggestions.ALL.get(app.name.lower())))
            finally:
                self._close_connection(DB_APPS_PATH, connection)

        return res
Beispiel #13
0
    def list_suggestions(self, limit: int,
                         filter_installed: bool) -> List[PackageSuggestion]:
        res = []

        connection = self._get_db_connection(DATABASE_APPS_FILE)

        if connection:
            suggestions = AppImageSuggestionsDownloader(
                appimage_config=self.configman.get_config(),
                logger=self.logger,
                i18n=self.i18n,
                http_client=self.http_client,
                taskman=TaskManager()).read()

            if not suggestions:
                self.logger.warning("Could not read suggestions")
                return res
            else:
                self.logger.info("Mapping suggestions")
                try:
                    if filter_installed:
                        installed = {
                            i.name.lower()
                            for i in self.read_installed(
                                disk_loader=None,
                                connection=connection).installed
                        }
                    else:
                        installed = None

                    sugs_map = {}

                    for s in suggestions:
                        lsplit = s.split('=')

                        name = lsplit[1].strip()

                        if limit <= 0 or len(sugs_map) < limit:
                            if not installed or not name.lower() in installed:
                                sugs_map[name] = SuggestionPriority(
                                    int(lsplit[0]))
                        else:
                            break

                    cursor = connection.cursor()
                    cursor.execute(
                        query.FIND_APPS_BY_NAME_FULL.format(','.join(
                            ["'{}'".format(s) for s in sugs_map.keys()])))

                    for t in cursor.fetchall():
                        app = AppImage(*t,
                                       i18n=self.i18n,
                                       custom_actions=self.custom_app_actions)
                        res.append(
                            PackageSuggestion(app, sugs_map[app.name.lower()]))
                    self.logger.info("Mapped {} suggestions".format(len(res)))
                except:
                    traceback.print_exc()
                finally:
                    connection.close()

        return res