#!/usr/bin/python3 import json import os LAUNCH_DIR_FILE = "/tmp/droiddeck-browser/launch-dir" import sys import time from pathlib import Path def hand_over(url, launch_dir): req = Path(launch_dir) / "stores" / "req" req.mkdir(parents=True, exist_ok=True) rid = "url-%d-%d" % (os.getpid(), int(time.time() * 1000)) tmp = req / (rid + ".json.tmp") tmp.write_text(json.dumps({"op": "open-url", "url": url})) os.rename(tmp, req / (rid + ".json")) def where(url): from urllib.parse import urlsplit try: u = urlsplit(url) return "%s://%s" % (u.scheme, u.hostname or "") except ValueError: return "?" def main(argv): url = argv[0] if argv else "" if url.startswith("steam://openurl/"): url = url[len("steam://openurl/"):] launch_dir = os.environ.get("DD_LAUNCH_DIR", "") if not launch_dir: try: launch_dir = open(LAUNCH_DIR_FILE).read().strip() except OSError: launch_dir = "" print("droiddeck xdg-open: %s" % where(url), file=sys.stderr, flush=True) if url.startswith(("https://", "http://")) and launch_dir: hand_over(url, launch_dir) return 0 here = os.path.dirname(os.path.realpath(sys.argv[0])) for d in os.environ.get("PATH", "").split(os.pathsep): candidate = os.path.join(d, "xdg-open") if os.access(candidate, os.X_OK) and os.path.realpath(candidate) != os.path.realpath(__file__) and os.path.abspath(d) != here: os.execv(candidate, [candidate] + argv) return 1 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))