mirror of
https://github.com/aclist/dztui.git
synced 2026-11-19 00:02:04 +01:00
chore: cherry pick changes from test-typehints branch
This commit is contained in:
1 parent
5853e15b8e
commit
8aca23cf76
5 files changed
+57
-14
No files matched your search
+4
-5
@@ -6,10 +6,8 @@ from dataclasses import dataclass
|
||||
from dzgui.const.constants import (
|
||||
APP_NAME_LOWER,
|
||||
DEBUG_LOG,
|
||||
FLATPAK_ROOT_PATH,
|
||||
SYSTEM_LOG,
|
||||
)
|
||||
from dzgui.init.proc import is_sandboxed
|
||||
|
||||
|
||||
@dataclass(slots=True, frozen=True)
|
||||
@@ -53,6 +51,10 @@ def is_writeable(path_str: str) -> bool:
|
||||
|
||||
def get_xdg_paths() -> dict[str, Path]:
|
||||
home = Path.home()
|
||||
if resolved := os.environ.get("SNAP_REAL_HOME"): # Snap
|
||||
home = Path(resolved)
|
||||
elif resolved := os.environ.get("HOME"): # Flatpak
|
||||
home = Path(resolved)
|
||||
xdg_paths = {
|
||||
"XDG_CONFIG_HOME": home / ".config",
|
||||
"XDG_STATE_HOME": home / ".local" / "state",
|
||||
@@ -62,9 +64,6 @@ def get_xdg_paths() -> dict[str, Path]:
|
||||
resolved_paths = {}
|
||||
for path in xdg_paths:
|
||||
real_path = os.environ.get(path)
|
||||
if real_path is not None and is_sandboxed():
|
||||
search_string = FLATPAK_ROOT_PATH + "/"
|
||||
real_path = real_path.replace(search_string, "")
|
||||
if real_path is not None and is_writeable(real_path):
|
||||
new_path = Path(real_path)
|
||||
else:
|
||||
|
||||
@@ -3,13 +3,13 @@ import shutil
|
||||
import subprocess
|
||||
|
||||
from dzgui.const.constants import APP_NAME, FLATPAK_APPID, FLATPAK_CMD, STEAM_CMD
|
||||
from dzgui.init.proc import is_sandboxed
|
||||
from dzgui.init.proc import is_flatpak_sandbox
|
||||
|
||||
logger = logging.getLogger(APP_NAME)
|
||||
|
||||
|
||||
def has_flatpak_client() -> bool:
|
||||
if is_sandboxed():
|
||||
if is_flatpak_sandbox():
|
||||
return True
|
||||
try:
|
||||
res = subprocess.run(
|
||||
|
||||
+27
-6
@@ -27,19 +27,40 @@ def is_steam_running(cmd: str) -> bool:
|
||||
raise TypeError("Not a valid Steam client selection")
|
||||
|
||||
|
||||
def is_sandboxed() -> bool:
|
||||
def is_snap_sandbox() -> bool:
|
||||
return bool(os.environ.get("SNAP"))
|
||||
|
||||
|
||||
def is_flatpak_sandbox() -> bool:
|
||||
return bool(os.environ.get("FLATPAK_ID"))
|
||||
|
||||
|
||||
def is_sandboxed() -> bool:
|
||||
if is_snap_sandbox() or is_flatpak_sandbox():
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def get_flatpak_output() -> list[str] | None:
|
||||
try:
|
||||
proc = subprocess.check_output(
|
||||
[FLATPAK_CMD, "ps", "--columns=application"], text=True
|
||||
)
|
||||
lines = proc.splitlines()
|
||||
return lines
|
||||
except Exception as e:
|
||||
logger.debug(e)
|
||||
return None
|
||||
|
||||
|
||||
def is_flatpak_steam_running() -> bool:
|
||||
if is_sandboxed():
|
||||
if is_flatpak_sandbox():
|
||||
return True
|
||||
if has_cmd(FLATPAK_CMD) is False:
|
||||
return False
|
||||
proc = subprocess.check_output(
|
||||
[FLATPAK_CMD, "ps", "--columns=application"], text=True
|
||||
)
|
||||
lines = proc.splitlines()
|
||||
lines = get_flatpak_output()
|
||||
if lines is None:
|
||||
return False
|
||||
if FLATPAK_APPID in lines:
|
||||
return True
|
||||
return False
|
||||
|
||||
@@ -3,7 +3,7 @@ title_intro = "Welcome"
|
||||
blurb_intro = """
|
||||
This wizard will help you set up some common config options before launching the application.
|
||||
"""
|
||||
error_sandbox = "Setup cannot be run from inside Flatpak sandbox.\nPlease launch the application directly."
|
||||
error_sandbox = "Setup cannot be run from inside a sandboxed version of Steam (Snap/Flatpak).\nPlease launch the application directly first."
|
||||
welcome = "Welcome to {0}!"
|
||||
|
||||
### SteamPathPage
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import pytest
|
||||
import tempfile
|
||||
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from dzgui.config.xdg import get_xdg_paths
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from _pytest.monkeypatch import MonkeyPatch
|
||||
|
||||
|
||||
@pytest.mark.parametrize("env", ["SNAP_REAL_HOME", "HOME"])
|
||||
def test_sandbox_home_resolution(monkeypatch: "MonkeyPatch", env: str) -> None:
|
||||
with tempfile.NamedTemporaryFile(delete=False) as f:
|
||||
monkeypatch.setenv(env, f.name)
|
||||
paths = get_xdg_paths()
|
||||
assert paths["XDG_CONFIG_HOME"] == Path(f.name).joinpath(".config/dzgui")
|
||||
|
||||
|
||||
def test_home_fallback() -> None:
|
||||
paths = get_xdg_paths()
|
||||
assert paths["XDG_CONFIG_HOME"] == Path.home().joinpath(".config/dzgui")
|
||||
Reference in new issue
Block a user