From 52ebc7809506e19beb135f53419a8bb9571c24e3 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 25 Nov 2023 13:09:12 +0530 Subject: [PATCH] DRYer --- src/calibre/gui2/__init__.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/calibre/gui2/__init__.py b/src/calibre/gui2/__init__.py index 5779c7b036..497f999425 100644 --- a/src/calibre/gui2/__init__.py +++ b/src/calibre/gui2/__init__.py @@ -1451,22 +1451,25 @@ def open_url(qurl): import shlex opener = shlex.split(spec) break + + def run_cmd(cmd): + import subprocess + subprocess.Popen(cmd, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + with sanitize_env_vars(): if opener: - import subprocess cmd = [x.replace('%u', qurl.toString()) for x in opener] if DEBUG: print('Running opener:', cmd) - subprocess.Popen(cmd, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + run_cmd(cmd) else: # Qt 5 requires QApplication to be constructed before trying to use # QDesktopServices::openUrl() ensure_app() cmd = ['xdg-open', qurl.toLocalFile() if qurl.isLocalFile() else qurl.toString(QUrl.ComponentFormattingOption.FullyEncoded)] if isfrozen and QApplication.instance().platformName() == "wayland": - import subprocess # See https://bugreports.qt.io/browse/QTBUG-119438 - subprocess.Popen(cmd, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + run_cmd(cmd) ok = True else: ok = QDesktopServices.openUrl(qurl) @@ -1476,10 +1479,9 @@ def open_url(qurl): # xdg activation token system Wayland imposes. print('QDesktopServices::openUrl() failed for url:', qurl, file=sys.stderr) if islinux: - import subprocess if DEBUG: - print('Running opener:', cmd) - subprocess.Popen(cmd, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + print('Opening with xdg-open:', cmd) + run_cmd(cmd) def safe_open_url(qurl):