2012-10-05 23:28:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
RAlgorithmProvider.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2012-12-08 19:42:43 +02:00
|
|
|
import os.path
|
2012-09-15 18:25:25 +03:00
|
|
|
from PyQt4.QtCore import *
|
|
|
|
from PyQt4.QtGui import *
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.ProcessingConfig import ProcessingConfig, Setting
|
|
|
|
from processing.core.ProcessingLog import ProcessingLog
|
|
|
|
from processing.core.AlgorithmProvider import AlgorithmProvider
|
2012-12-08 19:42:43 +02:00
|
|
|
from processing.gui.EditScriptAction import EditScriptAction
|
|
|
|
from processing.gui.DeleteScriptAction import DeleteScriptAction
|
|
|
|
from processing.gui.CreateNewScriptAction import CreateNewScriptAction
|
2014-04-17 01:41:25 +02:00
|
|
|
from RUtils import RUtils
|
|
|
|
from RAlgorithm import RAlgorithm
|
2012-12-08 19:42:43 +02:00
|
|
|
from processing.script.WrongScriptException import WrongScriptException
|
2013-09-12 13:19:00 +02:00
|
|
|
from processing.tools.system import *
|
2012-12-08 19:42:43 +02:00
|
|
|
import processing.resources_rc
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
class RAlgorithmProvider(AlgorithmProvider):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
AlgorithmProvider.__init__(self)
|
|
|
|
self.activate = False
|
2013-10-01 20:52:22 +03:00
|
|
|
self.actions.append(CreateNewScriptAction('Create new R script',
|
|
|
|
CreateNewScriptAction.SCRIPT_R))
|
|
|
|
self.contextMenuActions = \
|
|
|
|
[EditScriptAction(EditScriptAction.SCRIPT_R),
|
|
|
|
DeleteScriptAction(DeleteScriptAction.SCRIPT_R)]
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def initializeSettings(self):
|
|
|
|
AlgorithmProvider.initializeSettings(self)
|
2013-10-01 20:52:22 +03:00
|
|
|
ProcessingConfig.addSetting(Setting(self.getDescription(),
|
|
|
|
RUtils.RSCRIPTS_FOLDER, 'R Scripts folder'
|
|
|
|
, RUtils.RScriptsFolder()))
|
2013-09-12 13:19:00 +02:00
|
|
|
if isWindows():
|
2013-10-01 20:52:22 +03:00
|
|
|
ProcessingConfig.addSetting(Setting(self.getDescription(),
|
|
|
|
RUtils.R_FOLDER, 'R folder', RUtils.RFolder()))
|
|
|
|
ProcessingConfig.addSetting(Setting(self.getDescription(),
|
|
|
|
RUtils.R_USE64, 'Use 64 bit version', False))
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def unload(self):
|
|
|
|
AlgorithmProvider.unload(self)
|
2013-08-12 20:44:27 +02:00
|
|
|
ProcessingConfig.removeSetting(RUtils.RSCRIPTS_FOLDER)
|
2013-09-12 13:19:00 +02:00
|
|
|
if isWindows():
|
2013-08-12 20:44:27 +02:00
|
|
|
ProcessingConfig.removeSetting(RUtils.R_FOLDER)
|
|
|
|
ProcessingConfig.removeSetting(RUtils.R_USE64)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def getIcon(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
return QIcon(':/processing/images/r.png')
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def getDescription(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
return 'R scripts'
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def getName(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
return 'r'
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def _loadAlgorithms(self):
|
|
|
|
folder = RUtils.RScriptsFolder()
|
2012-11-08 00:40:27 +01:00
|
|
|
self.loadFromFolder(folder)
|
2013-10-01 20:52:22 +03:00
|
|
|
folder = os.path.join(os.path.dirname(__file__), 'scripts')
|
2012-11-08 00:40:27 +01:00
|
|
|
self.loadFromFolder(folder)
|
2012-12-10 00:12:07 +01:00
|
|
|
|
2012-11-08 00:40:27 +01:00
|
|
|
def loadFromFolder(self, folder):
|
2012-11-10 00:31:01 +01:00
|
|
|
if not os.path.exists(folder):
|
|
|
|
return
|
2014-05-26 17:39:35 +02:00
|
|
|
for path, subdirs, files in os.walk(folder):
|
|
|
|
for descriptionFile in files:
|
|
|
|
if descriptionFile.endswith('rsx'):
|
|
|
|
try:
|
|
|
|
fullpath = os.path.join(path, descriptionFile)
|
|
|
|
alg = RAlgorithm(fullpath)
|
|
|
|
if alg.name.strip() != '':
|
|
|
|
self.algs.append(alg)
|
|
|
|
except WrongScriptException, e:
|
|
|
|
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, e.msg)
|
|
|
|
except Exception, e:
|
|
|
|
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
|
|
|
|
'Could not load R script:' + descriptionFile + '\n'
|
|
|
|
+ unicode(e))
|