mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
[processing] allow adding core processing algs using scripts
This commit is contained in:
parent
11f197ee5b
commit
784b46bbcd
@ -16,6 +16,8 @@
|
||||
* *
|
||||
***************************************************************************
|
||||
"""
|
||||
from processing.script.ScriptUtils import ScriptUtils
|
||||
import os
|
||||
|
||||
|
||||
__author__ = 'Victor Olaya'
|
||||
@ -114,6 +116,8 @@ import processing.resources_rc
|
||||
|
||||
class QGISAlgorithmProvider(AlgorithmProvider):
|
||||
|
||||
_icon = QIcon(':/processing/images/qgis.png')
|
||||
|
||||
def __init__(self):
|
||||
AlgorithmProvider.__init__(self)
|
||||
self.alglist = [SumLines(), PointsInPolygon(),
|
||||
@ -166,6 +170,13 @@ class QGISAlgorithmProvider(AlgorithmProvider):
|
||||
# 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 @@ class QGISAlgorithmProvider(AlgorithmProvider):
|
||||
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 @@ class DeleteScriptAction(ContextAction):
|
||||
|
||||
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 @@ class EditScriptAction(ContextAction):
|
||||
|
||||
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 @@ from processing.script.WrongScriptException import WrongScriptException
|
||||
|
||||
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 @@ class ScriptAlgorithm(GeoAlgorithm):
|
||||
|
||||
GeoAlgorithm.__init__(self)
|
||||
self.script = script
|
||||
self.allowEdit = True
|
||||
self.descriptionFile = descriptionFile
|
||||
if script is not None:
|
||||
self.defineCharacteristicsFromScript()
|
||||
@ -77,7 +80,7 @@ class ScriptAlgorithm(GeoAlgorithm):
|
||||
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 @@ __copyright__ = '(C) 2012, Victor Olaya'
|
||||
|
||||
__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 @@ class ScriptAlgorithmProvider(AlgorithmProvider):
|
||||
|
||||
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 @@ __revision__ = '$Format:%H$'
|
||||
|
||||
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 @@ class ScriptUtils:
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user