Moved schemas to shared module (#94)

This commit is contained in:
Geoffrey Coulaud
2023-05-21 16:34:22 +02:00
committed by GitHub
parent d6cda0c562
commit ab21e8b38b
12 changed files with 58 additions and 36 deletions

View File

@@ -26,6 +26,7 @@ from time import time
from gi.repository import Adw, Gio, Gtk from gi.repository import Adw, Gio, Gtk
from . import shared
from .game_cover import GameCover from .game_cover import GameCover
@@ -78,7 +79,7 @@ class Game(Gtk.Box):
self.cover_button.connect("clicked", self.main_button_clicked, False) self.cover_button.connect("clicked", self.main_button_clicked, False)
self.play_button.connect("clicked", self.main_button_clicked, True) self.play_button.connect("clicked", self.main_button_clicked, True)
self.win.schema.connect("changed", self.schema_changed) shared.schema.connect("changed", self.schema_changed)
def update(self): def update(self):
if self.get_parent(): if self.get_parent():
@@ -253,7 +254,7 @@ class Game(Gtk.Box):
self.play_revealer.set_reveal_child(not state) self.play_revealer.set_reveal_child(not state)
def main_button_clicked(self, _widget, button): def main_button_clicked(self, _widget, button):
if self.win.schema.get_boolean("cover-launches-game") ^ button: if shared.schema.get_boolean("cover-launches-game") ^ button:
self.launch() self.launch()
else: else:
self.win.show_details_view(self) self.win.show_details_view(self)
@@ -261,7 +262,7 @@ class Game(Gtk.Box):
def set_play_icon(self): def set_play_icon(self):
self.play_button.set_icon_name( self.play_button.set_icon_name(
"help-about-symbolic" "help-about-symbolic"
if self.win.schema.get_boolean("cover-launches-game") if shared.schema.get_boolean("cover-launches-game")
else "media-playback-start-symbolic" else "media-playback-start-symbolic"
) )

View File

@@ -22,13 +22,14 @@ from time import time
import yaml import yaml
from . import shared
from .check_install import check_install from .check_install import check_install
def bottles_installed(win, path=None): def bottles_installed(win, path=None):
location_key = "bottles-location" location_key = "bottles-location"
bottles_dir = ( bottles_dir = (
path if path else Path(win.schema.get_string(location_key)).expanduser() path if path else Path(shared.schema.get_string(location_key)).expanduser()
) )
check = "library.yml" check = "library.yml"
@@ -42,7 +43,7 @@ def bottles_installed(win, path=None):
) )
) )
bottles_dir = check_install(check, locations, (win.schema, location_key)) bottles_dir = check_install(check, locations, (shared.schema, location_key))
return bottles_dir return bottles_dir

View File

@@ -23,13 +23,14 @@ from hashlib import sha256
from pathlib import Path from pathlib import Path
from time import time from time import time
from . import shared
from .check_install import check_install from .check_install import check_install
def heroic_installed(win, path=None): def heroic_installed(win, path=None):
location_key = "heroic-location" location_key = "heroic-location"
heroic_dir = ( heroic_dir = (
path if path else Path(win.schema.get_string(location_key)).expanduser() path if path else Path(shared.schema.get_string(location_key)).expanduser()
) )
check = "config.json" check = "config.json"
@@ -46,7 +47,7 @@ def heroic_installed(win, path=None):
if os.name == "nt" and not path: if os.name == "nt" and not path:
locations += (Path(os.getenv("appdata")) / "heroic",) locations += (Path(os.getenv("appdata")) / "heroic",)
heroic_dir = check_install(check, locations, (win.schema, location_key)) heroic_dir = check_install(check, locations, (shared.schema, location_key))
return heroic_dir return heroic_dir
@@ -60,7 +61,7 @@ def heroic_importer(win):
importer = win.importer importer = win.importer
# Import Epic games # Import Epic games
if not win.schema.get_boolean("heroic-import-epic"): if not shared.schema.get_boolean("heroic-import-epic"):
pass pass
elif (heroic_dir / "store_cache" / "legendary_library.json").exists(): elif (heroic_dir / "store_cache" / "legendary_library.json").exists():
library = json.load( library = json.load(
@@ -113,7 +114,7 @@ def heroic_importer(win):
pass pass
# Import GOG games # Import GOG games
if not win.schema.get_boolean("heroic-import-gog"): if not shared.schema.get_boolean("heroic-import-gog"):
pass pass
elif (heroic_dir / "gog_store" / "installed.json").exists() and ( elif (heroic_dir / "gog_store" / "installed.json").exists() and (
heroic_dir / "store_cache" / "gog_library.json" heroic_dir / "store_cache" / "gog_library.json"
@@ -163,7 +164,7 @@ def heroic_importer(win):
importer.save_game(values, image_path if image_path.exists() else None) importer.save_game(values, image_path if image_path.exists() else None)
# Import sideloaded games # Import sideloaded games
if not win.schema.get_boolean("heroic-import-sideload"): if not shared.schema.get_boolean("heroic-import-sideload"):
pass pass
elif (heroic_dir / "sideload_apps" / "library.json").exists(): elif (heroic_dir / "sideload_apps" / "library.json").exists():
library = json.load((heroic_dir / "sideload_apps" / "library.json").open()) library = json.load((heroic_dir / "sideload_apps" / "library.json").open())

View File

@@ -26,6 +26,7 @@ from time import time
import requests import requests
from gi.repository import GdkPixbuf, Gio from gi.repository import GdkPixbuf, Gio
from . import shared
from .check_install import check_install from .check_install import check_install
from .save_cover import resize_cover from .save_cover import resize_cover
@@ -120,7 +121,9 @@ def get_games_async(win, rows, importer):
def itch_installed(win, path=None): def itch_installed(win, path=None):
location_key = "itch-location" location_key = "itch-location"
itch_dir = path if path else Path(win.schema.get_string(location_key)).expanduser() itch_dir = (
path if path else Path(shared.schema.get_string(location_key)).expanduser()
)
check = Path("db") / "butler.db" check = Path("db") / "butler.db"
if not (itch_dir / check).is_file(): if not (itch_dir / check).is_file():
@@ -136,7 +139,7 @@ def itch_installed(win, path=None):
if os.name == "nt" and not path: if os.name == "nt" and not path:
locations += (Path(os.getenv("appdata")) / "itch",) locations += (Path(os.getenv("appdata")) / "itch",)
itch_dir = check_install(check, locations, (win.schema, location_key)) itch_dir = check_install(check, locations, (shared.schema, location_key))
return itch_dir return itch_dir

View File

@@ -22,13 +22,14 @@ from shutil import copyfile
from sqlite3 import connect from sqlite3 import connect
from time import time from time import time
from . import shared
from .check_install import check_install from .check_install import check_install
def lutris_installed(win, path=None): def lutris_installed(win, path=None):
location_key = "lutris-location" location_key = "lutris-location"
lutris_dir = ( lutris_dir = (
path if path else Path(win.schema.get_string(location_key)).expanduser() path if path else Path(shared.schema.get_string(location_key)).expanduser()
) )
check = "pga.db" check = "pga.db"
@@ -42,14 +43,14 @@ def lutris_installed(win, path=None):
) )
) )
lutris_dir = check_install(check, locations, (win.schema, location_key)) lutris_dir = check_install(check, locations, (shared.schema, location_key))
return lutris_dir return lutris_dir
def lutris_cache_exists(win, path=None): def lutris_cache_exists(win, path=None):
cache_key = "lutris-cache-location" cache_key = "lutris-cache-location"
cache_dir = path if path else Path(win.schema.get_string(cache_key)).expanduser() cache_dir = path if path else Path(shared.schema.get_string(cache_key)).expanduser()
cache_check = "coverart" cache_check = "coverart"
if not (cache_dir / cache_check).exists(): if not (cache_dir / cache_check).exists():
@@ -62,7 +63,9 @@ def lutris_cache_exists(win, path=None):
) )
) )
cache_dir = check_install(cache_check, cache_locations, (win.schema, cache_key)) cache_dir = check_install(
cache_check, cache_locations, (shared.schema, cache_key)
)
return cache_dir return cache_dir
@@ -105,7 +108,7 @@ def lutris_importer(win):
# No need to unlink temp files as they disappear when the connection is closed # No need to unlink temp files as they disappear when the connection is closed
database_tmp_path.unlink(missing_ok=True) database_tmp_path.unlink(missing_ok=True)
if not win.schema.get_boolean("lutris-import-steam"): if not shared.schema.get_boolean("lutris-import-steam"):
rows = [row for row in rows if not row[3] == "steam"] rows = [row for row in rows if not row[3] == "steam"]
current_time = int(time()) current_time = int(time())

View File

@@ -25,6 +25,7 @@ from time import time
import requests import requests
from gi.repository import Gio from gi.repository import Gio
from . import shared
from .check_install import check_install from .check_install import check_install
@@ -124,7 +125,7 @@ def get_games_async(win, appmanifests, steam_dir, importer):
def steam_installed(win, path=None): def steam_installed(win, path=None):
location_key = "steam-location" location_key = "steam-location"
steam_dir = Path(win.schema.get_string(location_key)).expanduser() steam_dir = Path(shared.schema.get_string(location_key)).expanduser()
check = "steamapps" check = "steamapps"
if not (steam_dir / check).is_file(): if not (steam_dir / check).is_file():
@@ -143,7 +144,9 @@ def steam_installed(win, path=None):
if os.name == "nt": if os.name == "nt":
locations += (Path(os.getenv("programfiles(x86)")) / "Steam",) locations += (Path(os.getenv("programfiles(x86)")) / "Steam",)
steam_dir = check_install(check, locations, (win.schema, location_key), subdirs) steam_dir = check_install(
check, locations, (shared.schema, location_key), subdirs
)
return steam_dir return steam_dir

View File

@@ -27,6 +27,7 @@ gi.require_version("Adw", "1")
# pylint: disable=wrong-import-position # pylint: disable=wrong-import-position
from gi.repository import Adw, Gio, GLib, Gtk from gi.repository import Adw, Gio, GLib, Gtk
from . import shared
from .bottles_importer import bottles_importer from .bottles_importer import bottles_importer
from .details_window import DetailsWindow from .details_window import DetailsWindow
from .heroic_importer import heroic_importer from .heroic_importer import heroic_importer
@@ -53,14 +54,13 @@ class CartridgesApplication(Adw.Application):
self.win = CartridgesWindow(application=self) self.win = CartridgesWindow(application=self)
# Save window geometry # Save window geometry
state_settings = Gio.Settings(schema_id="hu.kramo.Cartridges.State") shared.state_schema.bind(
state_settings.bind(
"width", self.win, "default-width", Gio.SettingsBindFlags.DEFAULT "width", self.win, "default-width", Gio.SettingsBindFlags.DEFAULT
) )
state_settings.bind( shared.state_schema.bind(
"height", self.win, "default-height", Gio.SettingsBindFlags.DEFAULT "height", self.win, "default-height", Gio.SettingsBindFlags.DEFAULT
) )
state_settings.bind( shared.state_schema.bind(
"is-maximized", self.win, "maximized", Gio.SettingsBindFlags.DEFAULT "is-maximized", self.win, "maximized", Gio.SettingsBindFlags.DEFAULT
) )
@@ -98,7 +98,7 @@ class CartridgesApplication(Adw.Application):
) )
sort_action.connect("activate", self.win.on_sort_action) sort_action.connect("activate", self.win.on_sort_action)
self.win.add_action(sort_action) self.win.add_action(sort_action)
self.win.on_sort_action(sort_action, state_settings.get_value("sort-mode")) self.win.on_sort_action(sort_action, shared.state_schema.get_value("sort-mode"))
self.win.present() self.win.present()

View File

@@ -24,6 +24,7 @@ cartridges_sources = [
'details_window.py', 'details_window.py',
'game.py', 'game.py',
'game_cover.py', 'game_cover.py',
'shared.py',
'importers/steam_importer.py', 'importers/steam_importer.py',
'importers/lutris_importer.py', 'importers/lutris_importer.py',
'importers/heroic_importer.py', 'importers/heroic_importer.py',

View File

@@ -24,6 +24,7 @@ from pathlib import Path
from gi.repository import Adw, Gio, GLib, Gtk from gi.repository import Adw, Gio, GLib, Gtk
# pylint: disable=unused-import # pylint: disable=unused-import
from . import shared
from .bottles_importer import bottles_installed from .bottles_importer import bottles_installed
from .create_dialog import create_dialog from .create_dialog import create_dialog
from .heroic_importer import heroic_installed from .heroic_importer import heroic_installed
@@ -89,7 +90,6 @@ class PreferencesWindow(Adw.PreferencesWindow):
def __init__(self, win, **kwargs): def __init__(self, win, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
self.schema = win.schema
self.win = win self.win = win
self.file_chooser = Gtk.FileDialog() self.file_chooser = Gtk.FileDialog()
self.set_transient_for(win) self.set_transient_for(win)
@@ -156,9 +156,9 @@ class PreferencesWindow(Adw.PreferencesWindow):
# SteamGridDB # SteamGridDB
def sgdb_key_changed(*_args): def sgdb_key_changed(*_args):
self.schema.set_string("sgdb-key", self.sgdb_key_entry_row.get_text()) shared.schema.set_string("sgdb-key", self.sgdb_key_entry_row.get_text())
self.sgdb_key_entry_row.set_text(self.schema.get_string("sgdb-key")) self.sgdb_key_entry_row.set_text(shared.schema.get_string("sgdb-key"))
self.sgdb_key_entry_row.connect("changed", sgdb_key_changed) self.sgdb_key_entry_row.connect("changed", sgdb_key_changed)
self.sgdb_key_group.set_description( self.sgdb_key_group.set_description(
@@ -171,7 +171,7 @@ class PreferencesWindow(Adw.PreferencesWindow):
def set_sgdb_sensitive(widget): def set_sgdb_sensitive(widget):
if not widget.get_text(): if not widget.get_text():
self.win.schema.set_boolean("sgdb", False) shared.schema.set_boolean("sgdb", False)
self.sgdb_switch_row.set_sensitive(widget.get_text()) self.sgdb_switch_row.set_sensitive(widget.get_text())
@@ -218,7 +218,7 @@ class PreferencesWindow(Adw.PreferencesWindow):
def bind_switches(self, settings): def bind_switches(self, settings):
for setting in settings: for setting in settings:
self.schema.bind( shared.schema.bind(
setting, setting,
self.get_switch(setting), self.get_switch(setting),
"active", "active",
@@ -259,7 +259,9 @@ class PreferencesWindow(Adw.PreferencesWindow):
re.sub( re.sub(
"/run/user/\\d*/doc/......../", "/run/user/\\d*/doc/......../",
"", "",
str(Path(win.schema.get_string(f"{source_id}-location")).expanduser()), str(
Path(shared.schema.get_string(f"{source_id}-location")).expanduser()
),
) )
) )
@@ -292,7 +294,7 @@ class PreferencesWindow(Adw.PreferencesWindow):
self.set_subtitle(win, source_id) self.set_subtitle(win, source_id)
win.schema.bind( shared.schema.bind(
source_id, source_id,
getattr(win, f"{source_id}_expander_row"), getattr(win, f"{source_id}_expander_row"),
"enable-expansion", "enable-expansion",

4
src/shared.py Normal file
View File

@@ -0,0 +1,4 @@
from gi.repository import Gio
schema = Gio.Settings.new("hu.kramo.Cartridges")
state_schema = Gio.Settings.new("hu.kramo.Cartridges.State")

View File

@@ -24,6 +24,8 @@ from shutil import copyfile
from gi.repository import Gio from gi.repository import Gio
from PIL import Image, ImageSequence from PIL import Image, ImageSequence
from . import shared
def resize_cover(win, cover_path=None, pixbuf=None): def resize_cover(win, cover_path=None, pixbuf=None):
if not cover_path and not pixbuf: if not cover_path and not pixbuf:
@@ -56,7 +58,7 @@ def resize_cover(win, cover_path=None, pixbuf=None):
image.resize(win.image_size).save( image.resize(win.image_size).save(
tmp_path, tmp_path,
compression="tiff_adobe_deflate" compression="tiff_adobe_deflate"
if win.schema.get_boolean("high-quality-images") if shared.schema.get_boolean("high-quality-images")
else "webp", else "webp",
) )

View File

@@ -3,6 +3,7 @@ from pathlib import Path
import requests import requests
from gi.repository import Gio from gi.repository import Gio
from . import shared
from .create_dialog import create_dialog from .create_dialog import create_dialog
from .save_cover import save_cover, resize_cover from .save_cover import save_cover, resize_cover
@@ -31,9 +32,9 @@ class SGDBSave:
if ( if (
not ( not (
self.win.schema.get_boolean("sgdb") shared.schema.get_boolean("sgdb")
and ( and (
(self.win.schema.get_boolean("sgdb-prefer")) (shared.schema.get_boolean("sgdb-prefer"))
or not ( or not (
(self.win.covers_dir / f"{game.game_id}.gif").is_file() (self.win.covers_dir / f"{game.game_id}.gif").is_file()
or (self.win.covers_dir / f"{game.game_id}.tiff").is_file() or (self.win.covers_dir / f"{game.game_id}.tiff").is_file()
@@ -46,7 +47,7 @@ class SGDBSave:
return return
url = "https://www.steamgriddb.com/api/v2/" url = "https://www.steamgriddb.com/api/v2/"
headers = {"Authorization": f'Bearer {self.win.schema.get_string("sgdb-key")}'} headers = {"Authorization": f'Bearer {shared.schema.get_string("sgdb-key")}'}
try: try:
search_result = requests.get( search_result = requests.get(
@@ -68,7 +69,7 @@ class SGDBSave:
response = None response = None
try: try:
if self.win.schema.get_boolean("sgdb-animated"): if shared.schema.get_boolean("sgdb-animated"):
try: try:
grid = requests.get( grid = requests.get(
f'{url}grids/game/{search_result.json()["data"][0]["id"]}?dimensions=600x900&types=animated', f'{url}grids/game/{search_result.json()["data"][0]["id"]}?dimensions=600x900&types=animated',