Skip to content
Permalink
Browse files

[processing] allow adding core processing algs using scripts

  • Loading branch information
volaya committed Jul 11, 2014
1 parent 11f197e commit 784b46bbcdba0748a6a6804d27f8632ba5b1e49b
@@ -16,6 +16,8 @@
* *
***************************************************************************
"""
from processing.script.ScriptUtils import ScriptUtils
import os


__author__ = 'Victor Olaya'
@@ -114,6 +116,8 @@

class QGISAlgorithmProvider(AlgorithmProvider):

_icon = QIcon(':/processing/images/qgis.png')

def __init__(self):
AlgorithmProvider.__init__(self)
self.alglist = [SumLines(), PointsInPolygon(),
@@ -166,6 +170,13 @@ def __init__(self):
# RasterLayerHistogram(), MeanAndStdDevPlot(),
# BarPlot(), PolarPlot()
]

folder = os.path.join(os.path.dirname(__file__), 'scripts')
scripts = ScriptUtils.loadFromFolder(folder)
for script in scripts:
script._icon = self._icon
script.allowEdit = False
self.alglist.extend(scripts)

def initializeSettings(self):
AlgorithmProvider.initializeSettings(self)
@@ -180,7 +191,7 @@ def getDescription(self):
return 'QGIS geoalgorithms'

def getIcon(self):
return QIcon(':/processing/images/qgis.png')
return self._icon

def _loadAlgorithms(self):
self.algs = self.alglist
@@ -46,7 +46,7 @@ def __init__(self, scriptType):

def isEnabled(self):
if self.scriptType == self.SCRIPT_PYTHON:
return isinstance(self.alg, ScriptAlgorithm)
return isinstance(self.alg, ScriptAlgorithm) and self.alg.allowEdit
elif self.scriptType == self.SCRIPT_R:
return isinstance(self.alg, RAlgorithm)

@@ -42,7 +42,7 @@ def __init__(self, scriptType):

def isEnabled(self):
if self.scriptType == ScriptEditorDialog.SCRIPT_PYTHON:
return isinstance(self.alg, ScriptAlgorithm)
return isinstance(self.alg, ScriptAlgorithm) and self.alg.allowEdit
elif self.scriptType == ScriptEditorDialog.SCRIPT_R:
return isinstance(self.alg, RAlgorithm)

@@ -55,6 +55,8 @@

class ScriptAlgorithm(GeoAlgorithm):

_icon = QtGui.QIcon(os.path.dirname(__file__) + '/../images/script.png')

def __init__(self, descriptionFile, script=None):
"""The script parameter can be used to directly pass the code
of the script without a file.
@@ -65,6 +67,7 @@ def __init__(self, descriptionFile, script=None):

GeoAlgorithm.__init__(self)
self.script = script
self.allowEdit = True
self.descriptionFile = descriptionFile
if script is not None:
self.defineCharacteristicsFromScript()
@@ -77,7 +80,7 @@ def getCopy(self):
return newone

def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + '/../images/script.png')
return self._icon

def defineCharacteristicsFromFile(self):
self.script = ''
@@ -25,25 +25,21 @@

__revision__ = '$Format:%H$'

import os.path
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from processing.core.ProcessingConfig import ProcessingConfig, Setting
from processing.core.ProcessingLog import ProcessingLog
from processing.core.AlgorithmProvider import AlgorithmProvider
from processing.gui.EditScriptAction import EditScriptAction
from processing.gui.DeleteScriptAction import DeleteScriptAction
from processing.gui.CreateNewScriptAction import CreateNewScriptAction
from processing.script.ScriptAlgorithm import ScriptAlgorithm
from processing.script.ScriptUtils import ScriptUtils
from processing.script.WrongScriptException import WrongScriptException
from processing.script.AddScriptFromFileAction import AddScriptFromFileAction
from processing.gui.GetScriptsAndModels import GetScriptsAction
import processing.resources_rc


class ScriptAlgorithmProvider(AlgorithmProvider):

def __init__(self):
AlgorithmProvider.__init__(self)
self.actions.extend([CreateNewScriptAction('Create new script',
@@ -76,24 +72,6 @@ def getDescription(self):

def _loadAlgorithms(self):
folder = ScriptUtils.scriptsFolder()
self.loadFromFolder(folder)
folder = os.path.join(os.path.dirname(__file__), 'scripts')
self.loadFromFolder(folder)
self.algs = ScriptUtils.loadFromFolder(folder)


def loadFromFolder(self, folder):
if not os.path.exists(folder):
return
for path, subdirs, files in os.walk(folder):
for descriptionFile in files:
if descriptionFile.endswith('py'):
try:
fullpath = os.path.join(path, descriptionFile)
alg = ScriptAlgorithm(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 script:' + descriptionFile + '\n'
+ unicode(e))
@@ -27,8 +27,10 @@

import os
from processing.core.ProcessingConfig import ProcessingConfig
from processing.tools.system import *

from processing.script.ScriptAlgorithm import ScriptAlgorithm
from processing.script.WrongScriptException import WrongScriptException
from processing.core.ProcessingLog import ProcessingLog
from processing.tools.system import mkdir, userFolder

class ScriptUtils:

@@ -43,3 +45,24 @@ def scriptsFolder():
mkdir(folder)

return os.path.abspath(folder)

@staticmethod
def loadFromFolder(folder):
if not os.path.exists(folder):
return []
algs = []
for path, subdirs, files in os.walk(folder):
for descriptionFile in files:
if descriptionFile.endswith('py'):
try:
fullpath = os.path.join(path, descriptionFile)
alg = ScriptAlgorithm(fullpath)
if alg.name.strip() != '':
algs.append(alg)
except WrongScriptException, e:
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, e.msg)
except Exception, e:
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
'Could not load script:' + descriptionFile + '\n'
+ unicode(e))
return algs

0 comments on commit 784b46b

Please sign in to comment.
You can’t perform that action at this time.