mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-08 00:05:09 -04:00
Fixes #49191 make "__file__" available to scripts
This commit is contained in:
parent
08c7d66e30
commit
a1a43e5362
@ -324,8 +324,8 @@ class Editor(QgsCodeEditorPython):
|
||||
filename = self.createTempFile()
|
||||
deleteTempFile = True
|
||||
|
||||
self.pythonconsole.shell.runCommand("exec(Path('{0}').read_text())"
|
||||
.format(filename.replace("\\", "/")))
|
||||
self.pythonconsole.shell.runFile(filename)
|
||||
|
||||
if deleteTempFile:
|
||||
Path(filename).unlink()
|
||||
|
||||
|
@ -81,10 +81,11 @@ class PythonInterpreter(QgsCodeInterpreter, code.InteractiveInterpreter):
|
||||
except ModuleNotFoundError:
|
||||
pass
|
||||
|
||||
def execCommandImpl(self, cmd):
|
||||
def execCommandImpl(self, cmd, show_input=True):
|
||||
res = self.currentState()
|
||||
|
||||
self.writeCMD(cmd)
|
||||
if show_input:
|
||||
self.writeCMD(cmd)
|
||||
import webbrowser
|
||||
version = 'master' if 'master' in Qgis.QGIS_VERSION.lower() else \
|
||||
re.findall(r'^\d.[0-9]*', Qgis.QGIS_VERSION)[0]
|
||||
@ -291,3 +292,19 @@ class ShellScintilla(QgsCodeEditorPython):
|
||||
def write(self, txt):
|
||||
if sys.stderr:
|
||||
sys.stderr.write(txt)
|
||||
|
||||
def runFile(self, filename):
|
||||
filename = filename.replace("\\", "/")
|
||||
dirname = os.path.dirname(filename)
|
||||
|
||||
# Append the directory of the file to the path and set __file__ to the filename
|
||||
self._interpreter.execCommandImpl("sys.path.append('{0}')".format(dirname), False)
|
||||
self._interpreter.execCommandImpl("__file__ = '{0}'".format(filename), False)
|
||||
|
||||
try:
|
||||
# Run the file
|
||||
self.runCommand("exec(Path('{0}').read_text())".format(filename))
|
||||
finally:
|
||||
# Remove the directory from the path and delete the __file__ variable
|
||||
self._interpreter.execCommandImpl("del __file__", False)
|
||||
self._interpreter.execCommandImpl("sys.path.remove('{0}')".format(dirname), False)
|
||||
|
Loading…
x
Reference in New Issue
Block a user