Ejemplo n.º 1
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
Ejemplo n.º 2
0
def parse(suggestions_str: str, logger: Optional[Logger] = None, type_: Optional[str] = None,
          splitter: str = '=') \
        -> Dict[str, SuggestionPriority]:
    output = dict()

    for line in suggestions_str.split('\n'):
        clean_line = line.strip()

        if clean_line:
            line_split = clean_line.split(splitter, 1)

            if len(line_split) == 2:
                prio_str, name = line_split[0].strip(), line_split[1].strip()

                if prio_str and name:
                    try:
                        prio = int(line_split[0])
                    except ValueError:
                        if logger:
                            logger.warning(
                                f"Could not parse {type_ + ' ' if type_ else ''}suggestion: {line}"
                            )
                        continue

                    output[line_split[1]] = SuggestionPriority(prio)

    return output
Ejemplo n.º 3
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)
Ejemplo n.º 4
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
Ejemplo n.º 5
0
    def list_suggestions(self, limit: int,
                         filter_installed: bool) -> List[PackageSuggestion]:
        res = []

        if snapd.is_running():
            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 found in {}".format(SUGGESTIONS_FILE))
                return res
            else:
                self.logger.info('Mapping suggestions')

                suggestions, threads = [], []
                snapd_client = SnapdClient(self.logger)
                installed = {
                    s['name'].lower()
                    for s in snapd_client.list_all_snaps()
                }

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

                            if not installed or name not in installed:
                                cached_sug = self.suggestions_cache.get(name)

                                if cached_sug:
                                    res.append(cached_sug)
                                else:
                                    t = Thread(target=self._fill_suggestion,
                                               args=(name,
                                                     SuggestionPriority(
                                                         int(sug[0])),
                                                     snapd_client, res))
                                    t.start()
                                    threads.append(t)
                                    time.sleep(0.001)  # to avoid being blocked
                        else:
                            break

                for t in threads:
                    t.join()

                res.sort(key=lambda s: s.priority.value, reverse=True)
        return res
Ejemplo n.º 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()
Ejemplo n.º 7
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
Ejemplo n.º 8
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