Skip to content

Commit

Permalink
[processing] completely restore support for script execution from editor
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Feb 5, 2018
1 parent 263702e commit 724390c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 61 deletions.
60 changes: 5 additions & 55 deletions python/plugins/processing/script/ScriptEditorDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import os
import codecs
import inspect

from qgis.PyQt import uic
from qgis.PyQt.QtCore import Qt
Expand Down Expand Up @@ -117,40 +118,6 @@ def __init__(self, filePath=None, parent=None):
self.needUpdate = False
self.setHasChanged(False)

#self.snippets = {}
#path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "script", "snippets.py")
#with codecs.open(path, "r", encoding="utf-8") as f:
# lines = f.readlines()
#snippetlines = []
#name = None
#for line in lines:
# if line.startswith("##"):
# if snippetlines:
# self.snippets[name] = "".join(snippetlines)
# name = line[2:]
# snippetlines = []
# else:
# snippetlines.append(line)
#if snippetlines:
# self.snippets[name] = "".join(snippetlines)
#if self.snippets:
# self.btnSnippets.setVisible(False)

#if self.alg is not None:
# pass
# self.filename = self.alg.descriptionFile
# self.editor.setText(self.alg.script)
#else:
# self.filename = None

#def showSnippets(self, evt):
# popupmenu = QMenu()
# for name, snippet in list(self.snippets.items()):
# action = QAction(self.tr(name), self.btnSnippets)
# action.triggered[()].connect(lambda snippet=snippet: self.editor.insert(snippet))
# popupmenu.addAction(action)
# popupmenu.exec_(QCursor.pos())

def closeEvent(self, event):
if self.hasChanged:
ret = QMessageBox.question(self,
Expand Down Expand Up @@ -226,36 +193,19 @@ def saveScript(self, saveAs):
return
self.needUpdate = True
self.setHasChanged(False)
#else:
# self.filePath = None

def setHasChanged(self, hasChanged):
self.hasChanged = hasChanged
self.actionSaveScript.setEnabled(hasChanged)

def runAlgorithm(self):
#~ if self.filePath is None or self.hasChanged:
#~ QMessageBox.warning(self,
#~ self.tr("Unsaved changes"),
#~ self.tr("There are unsaved changes in script. "
#~ "Please save it and try again.")
#~ )
#~ return

#~ algName = os.path.splitext(os.path.basename(self.filePath))[0]
#~ alg = ScriptUtils.loadAlgorithm(algName, self.filePath)
#~ alg.setProvider(QgsApplication.processingRegistry().providerById("script"))
#~ print("ALG", alg)

d = {}
#print(globals())
#print(locals())
exec(self.editor.text(), d)
#print(d)
#print(d.keys())
#print(d["SpatialIndex"])
alg = d["SpatialIndex"]()

className = d["__all__"][0]
alg = d[className]()
alg.setProvider(QgsApplication.processingRegistry().providerById("script"))
alg.initAlgorithm()

dlg = alg.createCustomParametersWidget(self)
if not dlg:
Expand Down
9 changes: 3 additions & 6 deletions python/plugins/processing/script/ScriptUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,9 @@ def loadAlgorithm(moduleName, filePath):
spec = importlib.util.spec_from_file_location(moduleName, filePath)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
for x in dir(module):
obj = getattr(module, x)
if inspect.isclass(obj):
print(obj)
if inspect.isclass(obj) and issubclass(obj, QgsProcessingAlgorithm) and obj.__name__ == moduleName:
return obj()
className = module.__all__[0]
obj = getattr(module, className)
return obj()
except ImportError as e:
QgsMessageLog.logMessage("Could not import script algorithm '{}' from '{}'\n{}".format(moduleName, filePath, str(e)),
"Processing",
Expand Down

0 comments on commit 724390c

Please sign in to comment.