Skip to content

Commit 0105a6f

Browse files
committed
[sextante] R scripts can now be run from the script editor
1 parent ead1d9f commit 0105a6f

File tree

4 files changed

+51
-24
lines changed

4 files changed

+51
-24
lines changed

python/plugins/sextante/r/EditRScriptAction.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def isEnabled(self):
3737

3838
def execute(self):
3939
dlg = EditRScriptDialog(self.alg)
40+
dlg.show()
4041
dlg.exec_()
4142
if dlg.update:
4243
self.toolbox.updateTree()

python/plugins/sextante/r/EditRScriptDialog.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
* *
1717
***************************************************************************
1818
"""
19+
from sextante.gui.ParametersDialog import ParametersDialog
20+
from sextante.core.QGisLayers import QGisLayers
21+
from sextante.modeler.Providers import Providers
1922

2023
__author__ = 'Victor Olaya'
2124
__date__ = 'August 2012'
@@ -39,11 +42,11 @@ def __init__(self, alg):
3942
else:
4043
self.filename = None
4144
QtGui.QDialog.__init__(self)
42-
self.setModal(True)
45+
self.setModal(False)
4346
self.setupUi()
4447
self.update = False
4548
self.help = None
46-
49+
4750
def setupUi(self):
4851
self.resize(600,400)
4952
self.setWindowTitle("Edit script")
@@ -62,9 +65,13 @@ def setupUi(self):
6265
self.saveButton = QtGui.QPushButton()
6366
self.saveButton.setText("Save")
6467
self.buttonBox.addButton(self.saveButton, QtGui.QDialogButtonBox.ActionRole)
68+
self.runButton = QtGui.QPushButton()
69+
self.runButton.setText("Run")
70+
self.buttonBox.addButton(self.runButton, QtGui.QDialogButtonBox.ActionRole)
6571
self.closeButton = QtGui.QPushButton()
6672
self.closeButton.setText("Close")
6773
self.buttonBox.addButton(self.closeButton, QtGui.QDialogButtonBox.ActionRole)
74+
QObject.connect(self.runButton, QtCore.SIGNAL("clicked()"), self.runAlgorithm)
6875
QObject.connect(self.saveButton, QtCore.SIGNAL("clicked()"), self.saveAlgorithm)
6976
QObject.connect(self.closeButton, QtCore.SIGNAL("clicked()"), self.cancelPressed)
7077
layout.addWidget(self.text)
@@ -75,7 +82,7 @@ def setupUi(self):
7582

7683
def editHelp(self):
7784
if self.alg is None:
78-
alg = RAlgorithm(None, unicode(self.text.toPlainText()))
85+
alg = RAlgorithm(None, unicode(self.text.toPlainText()))
7986
else:
8087
alg = self.alg
8188
dlg = HelpEditionDialog(alg)
@@ -86,6 +93,24 @@ def editHelp(self):
8693
self.help = dlg.descriptions
8794

8895

96+
def runAlgorithm(self):
97+
alg = RAlgorithm(None, unicode(self.text.toPlainText()))
98+
alg.provider = Providers.providers['r']
99+
dlg = alg.getCustomParametersDialog()
100+
if not dlg:
101+
dlg = ParametersDialog(alg)
102+
canvas = QGisLayers.iface.mapCanvas()
103+
prevMapTool = canvas.mapTool()
104+
dlg.show()
105+
dlg.exec_()
106+
if canvas.mapTool()!=prevMapTool:
107+
try:
108+
canvas.mapTool().reset()
109+
except:
110+
pass
111+
canvas.setMapTool(prevMapTool)
112+
113+
89114
def saveAlgorithm(self):
90115
if self.filename is None:
91116
self.filename = str(QtGui.QFileDialog.getSaveFileName(self, "Save Script", RUtils.RScriptsFolder(), "SEXTANTE R script (*.rsx)"))

python/plugins/sextante/r/RAlgorithm.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,31 +75,30 @@ def getIcon(self):
7575
return QtGui.QIcon(os.path.dirname(__file__) + "/../images/r.png")
7676

7777
def defineCharacteristicsFromScript(self):
78-
lines = self.script.split("\n")
79-
self.silentOutputs = []
78+
lines = self.script.split("\n")
8079
self.name = "[Unnamed algorithm]"
8180
self.group = "User R scripts"
82-
for line in lines:
83-
if line.startswith("##"):
84-
try:
85-
self.processParameterLine(line.strip("\n"))
86-
except:
87-
pass
88-
89-
def defineCharacteristicsFromFile(self):
81+
self.parseDescription(iter(lines))
82+
83+
84+
def defineCharacteristicsFromFile(self):
85+
filename = os.path.basename(self.descriptionFile)
86+
self.name = filename[:filename.rfind(".")].replace("_", " ")
87+
self.group = "User R scripts"
88+
with open(self.descriptionFile, 'r') as f:
89+
lines = [line.strip() for line in f]
90+
self.parseDescription(iter(lines))
91+
92+
def parseDescription(self, lines):
9093
self.script = ""
9194
self.commands=[]
9295
self.showPlots = False
9396
self.showConsoleOutput = False
9497
self.useRasterPackage = True
9598
self.passFileNames = False
9699
self.verboseCommands = []
97-
filename = os.path.basename(self.descriptionFile)
98-
self.name = filename[:filename.rfind(".")].replace("_", " ")
99-
self.group = "User R scripts"
100-
ender = 0
101-
lines = open(self.descriptionFile)
102-
line = lines.readline().strip("\n").strip("\r")
100+
ender = 0
101+
line = lines.next().strip("\n").strip("\r")
103102
while ender < 10:
104103
if line.startswith("##"):
105104
try:
@@ -119,8 +118,10 @@ def defineCharacteristicsFromFile(self):
119118
ender=0
120119
self.commands.append(line)
121120
self.script += line + "\n"
122-
line = lines.readline().strip("\n").strip("\r")
123-
lines.close()
121+
try:
122+
line = lines.next().strip("\n").strip("\r")
123+
except:
124+
break
124125

125126
def getVerboseCommands(self):
126127
return self.verboseCommands
@@ -280,7 +281,7 @@ def getImportCommands(self):
280281
commands.append(
281282
'tryCatch(find.package("rgdal"), error=function(e) install.packages("rgdal", dependencies=TRUE, lib="%s"))' % rLibDir)
282283
commands.append("library(\"rgdal\")");
283-
if self.useRasterPackage or self.passFileNames:
284+
if not self.useRasterPackage or self.passFileNames:
284285
commands.append(
285286
'tryCatch(find.package("raster"), error=function(e) install.packages("raster", dependencies=TRUE, lib="%s"))' % rLibDir)
286287
commands.append("library(\"raster\")");
@@ -376,7 +377,7 @@ def getRCommands(self):
376377
return self.commands
377378

378379
def helpFile(self):
379-
helpfile = self.descriptionFile + ".help"
380+
helpfile = unicode(self.descriptionFile) + ".help"
380381
if os.path.exists(helpfile):
381382
h2h = Help2Html()
382383
return h2h.getHtmlFile(self, helpfile)

python/plugins/sextante/r/RAlgorithmProvider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def loadFromFolder(self, folder):
8989
except WrongScriptException,e:
9090
SextanteLog.addToLog(SextanteLog.LOG_ERROR,e.msg)
9191
except Exception, e:
92-
SextanteLog.addToLog(SextanteLog.LOG_ERROR,"Could not load R script:" + descriptionFile)
92+
SextanteLog.addToLog(SextanteLog.LOG_ERROR,"Could not load R script:" + descriptionFile + "\n" + str(e))
9393

9494

9595

0 commit comments

Comments
 (0)