Files
Athena-Deck/integrations/hermes-unraid/desktop/install-screen-browser.py
T

69 lines
2.7 KiB
Python

#!/opt/hermes/.venv/bin/python
"""Persist only XFCE's WebBrowser selection for existing local Hermes profiles."""
import configparser
import json
import re
import sys
from pathlib import Path
sys.path.insert(0, "/opt/hermes")
import hermes_yaml as yaml
BASE = Path("/opt/data")
LAUNCHER = BASE / ".local/bin/hermes-screen-browser"
HELPER = "hermes-screen-browser"
SOURCE = Path(__file__).with_name("screen-browser.py")
def main():
LAUNCHER.parent.mkdir(parents=True, exist_ok=True)
LAUNCHER.write_bytes(SOURCE.read_bytes())
LAUNCHER.chmod(0o755)
changed = []
for home in [BASE, *sorted((BASE / "profiles").iterdir())]:
cfg_file = home / "config.yaml"
if not cfg_file.is_file() or home.is_symlink():
continue
cfg = yaml.safe_load(cfg_file.read_text()) or {}
terminal = cfg.get("terminal", {}) or {}
desktop = cfg.get("bot_desktop", {}) or {}
if terminal.get("backend", "local") != "local":
continue
if desktop.get("placement", "gateway") not in (None, "gateway"):
continue
config = home / "bot-desktop/xdg"
helper_dir = config / ".local-share/xfce4/helpers"
helper_dir.mkdir(parents=True, exist_ok=True)
(helper_dir / (HELPER + ".desktop")).write_text(
"[Desktop Entry]\nVersion=1.0\nType=X-XFCE-Helper\n"
"Name=Hermes Screen Browser\nIcon=internet-web-browser\n"
"StartupNotify=false\nX-XFCE-Category=WebBrowser\n"
f"X-XFCE-Binaries={LAUNCHER};\n"
'X-XFCE-Commands=%B;\nX-XFCE-CommandsWithParameter=%B "%s";\n'
)
rc = config / "xfce4/helpers.rc"
rc.parent.mkdir(parents=True, exist_ok=True)
old = rc.read_text() if rc.exists() else ""
if re.search(r"^WebBrowser=", old, re.M):
new = re.sub(r"^WebBrowser=.*$", "WebBrowser=" + HELPER, old, flags=re.M)
else:
new = old.rstrip() + "\nWebBrowser=" + HELPER + "\n"
rc.write_text(new.lstrip("\n"))
mime = config / "mimeapps.list"
ini = configparser.ConfigParser(interpolation=None)
ini.optionxform = str
if mime.exists():
ini.read(mime)
if not ini.has_section("Default Applications"):
ini.add_section("Default Applications")
for mime_type in ("x-scheme-handler/http", "x-scheme-handler/https", "text/html"):
ini.set("Default Applications", mime_type, "xfce4-web-browser.desktop")
with mime.open("w") as fh:
ini.write(fh, space_around_delimiters=False)
changed.append("default" if home == BASE else home.name)
print(json.dumps({"configured_local_profiles": changed, "launcher": str(LAUNCHER)}))
if __name__ == "__main__":
main()