Fix python script editor "open in external editor" action

When a system has a EDITOR environment variable set to either
an invalid editor, or an editor which requires a terminal (eg
nano/vim), then fallback to the QDesktopServices approach
to opening the script

Avoids this button doing nothing.
This commit is contained in:
Nyall Dawson 2024-06-06 09:50:57 +10:00
parent c685a9aa77
commit 4a4c624e3e

View File

@ -18,6 +18,7 @@ email : lrssvtml (at) gmail (dot) com
Some portions of code were taken from https://code.google.com/p/pydee/
"""
import os
import subprocess
from qgis.PyQt.QtCore import Qt, QTimer, QCoreApplication, QSize, QByteArray, QFileInfo, QUrl, QDir
from qgis.PyQt.QtWidgets import QToolBar, QToolButton, QWidget, QSplitter, QTreeWidget, QAction, QFileDialog, QCheckBox, QSizePolicy, QMenu, QGridLayout, QApplication, QShortcut
@ -588,10 +589,21 @@ class PythonConsoleWidget(QWidget):
def openScriptFileExtEditor(self):
tabWidget = self.tabEditorWidget.currentWidget()
path = tabWidget.path
import subprocess
try:
subprocess.Popen([os.environ['EDITOR'], path])
except KeyError:
editor_command = os.environ.get('EDITOR')
if editor_command:
child = subprocess.Popen([os.environ['EDITOR'], path])
try:
# let's see if the EDITOR drops out immediately....
child.wait(0.01)
rc = child.poll()
if rc:
# editor failed, use backup approach
QDesktopServices.openUrl(QUrl.fromLocalFile(path))
except subprocess.TimeoutExpired:
# looks like EDITOR started up successfully, all is good
pass
else:
QDesktopServices.openUrl(QUrl.fromLocalFile(path))
def openScriptFile(self):