Skip to content
Permalink
Browse files
[sextante] R scripts can now be run from the script editor
  • Loading branch information
volaya committed Mar 20, 2013
1 parent ead1d9f commit 0105a6faf90ca54c4fce17bc6e7dc64b8d5cb7ee
@@ -37,6 +37,7 @@ def isEnabled(self):

def execute(self):
dlg = EditRScriptDialog(self.alg)
dlg.show()
dlg.exec_()
if dlg.update:
self.toolbox.updateTree()
@@ -16,6 +16,9 @@
* *
***************************************************************************
"""
from sextante.gui.ParametersDialog import ParametersDialog
from sextante.core.QGisLayers import QGisLayers
from sextante.modeler.Providers import Providers

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
@@ -39,11 +42,11 @@ def __init__(self, alg):
else:
self.filename = None
QtGui.QDialog.__init__(self)
self.setModal(True)
self.setModal(False)
self.setupUi()
self.update = False
self.help = None

def setupUi(self):
self.resize(600,400)
self.setWindowTitle("Edit script")
@@ -62,9 +65,13 @@ def setupUi(self):
self.saveButton = QtGui.QPushButton()
self.saveButton.setText("Save")
self.buttonBox.addButton(self.saveButton, QtGui.QDialogButtonBox.ActionRole)
self.runButton = QtGui.QPushButton()
self.runButton.setText("Run")
self.buttonBox.addButton(self.runButton, QtGui.QDialogButtonBox.ActionRole)
self.closeButton = QtGui.QPushButton()
self.closeButton.setText("Close")
self.buttonBox.addButton(self.closeButton, QtGui.QDialogButtonBox.ActionRole)
QObject.connect(self.runButton, QtCore.SIGNAL("clicked()"), self.runAlgorithm)
QObject.connect(self.saveButton, QtCore.SIGNAL("clicked()"), self.saveAlgorithm)
QObject.connect(self.closeButton, QtCore.SIGNAL("clicked()"), self.cancelPressed)
layout.addWidget(self.text)
@@ -75,7 +82,7 @@ def setupUi(self):

def editHelp(self):
if self.alg is None:
alg = RAlgorithm(None, unicode(self.text.toPlainText()))
alg = RAlgorithm(None, unicode(self.text.toPlainText()))
else:
alg = self.alg
dlg = HelpEditionDialog(alg)
@@ -86,6 +93,24 @@ def editHelp(self):
self.help = dlg.descriptions


def runAlgorithm(self):
alg = RAlgorithm(None, unicode(self.text.toPlainText()))
alg.provider = Providers.providers['r']
dlg = alg.getCustomParametersDialog()
if not dlg:
dlg = ParametersDialog(alg)
canvas = QGisLayers.iface.mapCanvas()
prevMapTool = canvas.mapTool()
dlg.show()
dlg.exec_()
if canvas.mapTool()!=prevMapTool:
try:
canvas.mapTool().reset()
except:
pass
canvas.setMapTool(prevMapTool)


def saveAlgorithm(self):
if self.filename is None:
self.filename = str(QtGui.QFileDialog.getSaveFileName(self, "Save Script", RUtils.RScriptsFolder(), "SEXTANTE R script (*.rsx)"))
@@ -75,31 +75,30 @@ def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/../images/r.png")

def defineCharacteristicsFromScript(self):
lines = self.script.split("\n")
self.silentOutputs = []
lines = self.script.split("\n")
self.name = "[Unnamed algorithm]"
self.group = "User R scripts"
for line in lines:
if line.startswith("##"):
try:
self.processParameterLine(line.strip("\n"))
except:
pass

def defineCharacteristicsFromFile(self):
self.parseDescription(iter(lines))


def defineCharacteristicsFromFile(self):
filename = os.path.basename(self.descriptionFile)
self.name = filename[:filename.rfind(".")].replace("_", " ")
self.group = "User R scripts"
with open(self.descriptionFile, 'r') as f:
lines = [line.strip() for line in f]
self.parseDescription(iter(lines))

def parseDescription(self, lines):
self.script = ""
self.commands=[]
self.showPlots = False
self.showConsoleOutput = False
self.useRasterPackage = True
self.passFileNames = False
self.verboseCommands = []
filename = os.path.basename(self.descriptionFile)
self.name = filename[:filename.rfind(".")].replace("_", " ")
self.group = "User R scripts"
ender = 0
lines = open(self.descriptionFile)
line = lines.readline().strip("\n").strip("\r")
ender = 0
line = lines.next().strip("\n").strip("\r")
while ender < 10:
if line.startswith("##"):
try:
@@ -119,8 +118,10 @@ def defineCharacteristicsFromFile(self):
ender=0
self.commands.append(line)
self.script += line + "\n"
line = lines.readline().strip("\n").strip("\r")
lines.close()
try:
line = lines.next().strip("\n").strip("\r")
except:
break

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

def helpFile(self):
helpfile = self.descriptionFile + ".help"
helpfile = unicode(self.descriptionFile) + ".help"
if os.path.exists(helpfile):
h2h = Help2Html()
return h2h.getHtmlFile(self, helpfile)
@@ -89,7 +89,7 @@ def loadFromFolder(self, folder):
except WrongScriptException,e:
SextanteLog.addToLog(SextanteLog.LOG_ERROR,e.msg)
except Exception, e:
SextanteLog.addToLog(SextanteLog.LOG_ERROR,"Could not load R script:" + descriptionFile)
SextanteLog.addToLog(SextanteLog.LOG_ERROR,"Could not load R script:" + descriptionFile + "\n" + str(e))



0 comments on commit 0105a6f

Please sign in to comment.