Skip to content

Commit

Permalink
[processing] allow adding core processing algs using scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
volaya committed Jul 11, 2014
1 parent 11f197e commit 784b46b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 31 deletions.
13 changes: 12 additions & 1 deletion python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -16,6 +16,8 @@
* *
***************************************************************************
"""
from processing.script.ScriptUtils import ScriptUtils
import os


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

class QGISAlgorithmProvider(AlgorithmProvider):

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

def __init__(self):
AlgorithmProvider.__init__(self)
self.alglist = [SumLines(), PointsInPolygon(),
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/gui/DeleteScriptAction.py
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/gui/EditScriptAction.py
Expand Up @@ -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)

Expand Down
5 changes: 4 additions & 1 deletion python/plugins/processing/script/ScriptAlgorithm.py
Expand Up @@ -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.
Expand All @@ -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()
Expand All @@ -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 = ''
Expand Down
28 changes: 3 additions & 25 deletions python/plugins/processing/script/ScriptAlgorithmProvider.py
Expand Up @@ -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',
Expand Down Expand Up @@ -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 changes: 25 additions & 2 deletions python/plugins/processing/script/ScriptUtils.py
Expand Up @@ -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:

Expand All @@ -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.