mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-19 00:04:52 -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'
|
__author__ = 'Victor Olaya'
|
||||||
@ -114,6 +116,8 @@ import processing.resources_rc
|
|||||||
|
|
||||||
class QGISAlgorithmProvider(AlgorithmProvider):
|
class QGISAlgorithmProvider(AlgorithmProvider):
|
||||||
|
|
||||||
|
_icon = QIcon(':/processing/images/qgis.png')
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
AlgorithmProvider.__init__(self)
|
AlgorithmProvider.__init__(self)
|
||||||
self.alglist = [SumLines(), PointsInPolygon(),
|
self.alglist = [SumLines(), PointsInPolygon(),
|
||||||
@ -167,6 +171,13 @@ class QGISAlgorithmProvider(AlgorithmProvider):
|
|||||||
# BarPlot(), PolarPlot()
|
# 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):
|
def initializeSettings(self):
|
||||||
AlgorithmProvider.initializeSettings(self)
|
AlgorithmProvider.initializeSettings(self)
|
||||||
|
|
||||||
@ -180,7 +191,7 @@ class QGISAlgorithmProvider(AlgorithmProvider):
|
|||||||
return 'QGIS geoalgorithms'
|
return 'QGIS geoalgorithms'
|
||||||
|
|
||||||
def getIcon(self):
|
def getIcon(self):
|
||||||
return QIcon(':/processing/images/qgis.png')
|
return self._icon
|
||||||
|
|
||||||
def _loadAlgorithms(self):
|
def _loadAlgorithms(self):
|
||||||
self.algs = self.alglist
|
self.algs = self.alglist
|
||||||
|
@ -46,7 +46,7 @@ class DeleteScriptAction(ContextAction):
|
|||||||
|
|
||||||
def isEnabled(self):
|
def isEnabled(self):
|
||||||
if self.scriptType == self.SCRIPT_PYTHON:
|
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:
|
elif self.scriptType == self.SCRIPT_R:
|
||||||
return isinstance(self.alg, RAlgorithm)
|
return isinstance(self.alg, RAlgorithm)
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ class EditScriptAction(ContextAction):
|
|||||||
|
|
||||||
def isEnabled(self):
|
def isEnabled(self):
|
||||||
if self.scriptType == ScriptEditorDialog.SCRIPT_PYTHON:
|
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:
|
elif self.scriptType == ScriptEditorDialog.SCRIPT_R:
|
||||||
return isinstance(self.alg, RAlgorithm)
|
return isinstance(self.alg, RAlgorithm)
|
||||||
|
|
||||||
|
@ -55,6 +55,8 @@ from processing.script.WrongScriptException import WrongScriptException
|
|||||||
|
|
||||||
class ScriptAlgorithm(GeoAlgorithm):
|
class ScriptAlgorithm(GeoAlgorithm):
|
||||||
|
|
||||||
|
_icon = QtGui.QIcon(os.path.dirname(__file__) + '/../images/script.png')
|
||||||
|
|
||||||
def __init__(self, descriptionFile, script=None):
|
def __init__(self, descriptionFile, script=None):
|
||||||
"""The script parameter can be used to directly pass the code
|
"""The script parameter can be used to directly pass the code
|
||||||
of the script without a file.
|
of the script without a file.
|
||||||
@ -65,6 +67,7 @@ class ScriptAlgorithm(GeoAlgorithm):
|
|||||||
|
|
||||||
GeoAlgorithm.__init__(self)
|
GeoAlgorithm.__init__(self)
|
||||||
self.script = script
|
self.script = script
|
||||||
|
self.allowEdit = True
|
||||||
self.descriptionFile = descriptionFile
|
self.descriptionFile = descriptionFile
|
||||||
if script is not None:
|
if script is not None:
|
||||||
self.defineCharacteristicsFromScript()
|
self.defineCharacteristicsFromScript()
|
||||||
@ -77,7 +80,7 @@ class ScriptAlgorithm(GeoAlgorithm):
|
|||||||
return newone
|
return newone
|
||||||
|
|
||||||
def getIcon(self):
|
def getIcon(self):
|
||||||
return QtGui.QIcon(os.path.dirname(__file__) + '/../images/script.png')
|
return self._icon
|
||||||
|
|
||||||
def defineCharacteristicsFromFile(self):
|
def defineCharacteristicsFromFile(self):
|
||||||
self.script = ''
|
self.script = ''
|
||||||
|
@ -25,18 +25,14 @@ __copyright__ = '(C) 2012, Victor Olaya'
|
|||||||
|
|
||||||
__revision__ = '$Format:%H$'
|
__revision__ = '$Format:%H$'
|
||||||
|
|
||||||
import os.path
|
|
||||||
from PyQt4.QtCore import *
|
from PyQt4.QtCore import *
|
||||||
from PyQt4.QtGui import *
|
from PyQt4.QtGui import *
|
||||||
from processing.core.ProcessingConfig import ProcessingConfig, Setting
|
from processing.core.ProcessingConfig import ProcessingConfig, Setting
|
||||||
from processing.core.ProcessingLog import ProcessingLog
|
|
||||||
from processing.core.AlgorithmProvider import AlgorithmProvider
|
from processing.core.AlgorithmProvider import AlgorithmProvider
|
||||||
from processing.gui.EditScriptAction import EditScriptAction
|
from processing.gui.EditScriptAction import EditScriptAction
|
||||||
from processing.gui.DeleteScriptAction import DeleteScriptAction
|
from processing.gui.DeleteScriptAction import DeleteScriptAction
|
||||||
from processing.gui.CreateNewScriptAction import CreateNewScriptAction
|
from processing.gui.CreateNewScriptAction import CreateNewScriptAction
|
||||||
from processing.script.ScriptAlgorithm import ScriptAlgorithm
|
|
||||||
from processing.script.ScriptUtils import ScriptUtils
|
from processing.script.ScriptUtils import ScriptUtils
|
||||||
from processing.script.WrongScriptException import WrongScriptException
|
|
||||||
from processing.script.AddScriptFromFileAction import AddScriptFromFileAction
|
from processing.script.AddScriptFromFileAction import AddScriptFromFileAction
|
||||||
from processing.gui.GetScriptsAndModels import GetScriptsAction
|
from processing.gui.GetScriptsAndModels import GetScriptsAction
|
||||||
import processing.resources_rc
|
import processing.resources_rc
|
||||||
@ -76,24 +72,6 @@ class ScriptAlgorithmProvider(AlgorithmProvider):
|
|||||||
|
|
||||||
def _loadAlgorithms(self):
|
def _loadAlgorithms(self):
|
||||||
folder = ScriptUtils.scriptsFolder()
|
folder = ScriptUtils.scriptsFolder()
|
||||||
self.loadFromFolder(folder)
|
self.algs = ScriptUtils.loadFromFolder(folder)
|
||||||
folder = os.path.join(os.path.dirname(__file__), 'scripts')
|
|
||||||
self.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
|
import os
|
||||||
from processing.core.ProcessingConfig import ProcessingConfig
|
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:
|
class ScriptUtils:
|
||||||
|
|
||||||
@ -43,3 +45,24 @@ class ScriptUtils:
|
|||||||
mkdir(folder)
|
mkdir(folder)
|
||||||
|
|
||||||
return os.path.abspath(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