From 495755f278601e2d05c630bcaaceb653d1126c77 Mon Sep 17 00:00:00 2001 From: kramo <93832451+kra-mo@users.noreply.github.com> Date: Sat, 1 Jul 2023 10:44:15 +0200 Subject: [PATCH] More reliance on GLib --- src/importer/sources/flatpak_source.py | 84 +++++++++++--------------- 1 file changed, 35 insertions(+), 49 deletions(-) diff --git a/src/importer/sources/flatpak_source.py b/src/importer/sources/flatpak_source.py index c2391a0..8dd7a14 100644 --- a/src/importer/sources/flatpak_source.py +++ b/src/importer/sources/flatpak_source.py @@ -17,12 +17,10 @@ # # SPDX-License-Identifier: GPL-3.0-or-later -import re -import subprocess from pathlib import Path from time import time -from gi.repository import Gtk +from gi.repository import GLib, Gtk from src import shared from src.game import Game @@ -41,62 +39,42 @@ class FlatpakSourceIterator(SourceIterator): icon_theme = Gtk.IconTheme.new() icon_theme.add_search_path(str(self.source.data_location["icons"])) - try: - process = subprocess.run( - ("flatpak-spawn", "--host", "flatpak", "list", "--columns=application"), - capture_output=True, - encoding="utf-8", - check=True, - ) - flatpak_ids = process.stdout.split("\n") - - to_remove = ( - {"hu.kramo.Cartridges"} - if shared.schema.get_boolean("flatpak-import-launchers") - else { - "hu.kramo.Cartridges", - "com.valvesoftware.Steam", - "net.lutris.Lutris", - "com.heroicgameslauncher.hgl", - "com.usebottles.Bottles", - "io.itch.itch", - } - ) - - for item in to_remove: - if item in flatpak_ids: - flatpak_ids.remove(item) - - except subprocess.CalledProcessError: - return + blacklist = ( + {"hu.kramo.Cartridges"} + if shared.schema.get_boolean("flatpak-import-launchers") + else { + "hu.kramo.Cartridges", + "com.valvesoftware.Steam", + "net.lutris.Lutris", + "com.heroicgameslauncher.hgl", + "com.usebottles.Bottles", + "io.itch.itch", + } + ) for entry in (self.source.data_location["applications"]).iterdir(): - flatpak_id = entry.stem + keyfile = GLib.KeyFile.new() - if flatpak_id not in flatpak_ids: - continue + try: + keyfile.load_from_file(str(entry), 0) - with entry.open("r", encoding="utf-8") as open_file: - string = open_file.read() + if "Game" not in keyfile.get_string_list("Desktop Entry", "Categories"): + continue - desktop_values = {"Name": None, "Icon": None, "Categories": None} - for key in desktop_values: - if regex := re.findall(f"{key}=(.*)\n", string): - desktop_values[key] = regex[0] + if ( + flatpak_id := keyfile.get_string("Desktop Entry", "X-Flatpak") + ) in blacklist: + continue - if not desktop_values["Name"]: - continue + name = keyfile.get_string("Desktop Entry", "Name") - if not desktop_values["Categories"]: - continue - - if not "Game" in desktop_values["Categories"].split(";"): + except GLib.GError: continue values = { "source": self.source.id, "added": added_time, - "name": desktop_values["Name"], + "name": name, "game_id": self.source.game_id_format.format(game_id=flatpak_id), "executable": self.source.executable_format.format( flatpak_id=flatpak_id @@ -105,10 +83,16 @@ class FlatpakSourceIterator(SourceIterator): game = Game(values) additional_data = {} - if icon_name := desktop_values["Icon"]: + + try: if ( icon_path := icon_theme.lookup_icon( - icon_name, None, 512, 1, shared.win.get_direction(), 0 + keyfile.get_string("Desktop Entry", "Icon"), + None, + 512, + 1, + shared.win.get_direction(), + 0, ) .get_file() .get_path() @@ -116,6 +100,8 @@ class FlatpakSourceIterator(SourceIterator): additional_data = {"local_icon_path": Path(icon_path)} else: pass + except GLib.GError: + pass # Produce game yield (game, additional_data)