From 2eebe0d3149a1c092e31208279574d0fa63e09a8 Mon Sep 17 00:00:00 2001 From: volaya Date: Mon, 23 May 2016 16:57:59 +0200 Subject: [PATCH] [processing] additional mechanism to add scripts from 3rd party plugin --- python/plugins/processing/ProcessingPlugin.py | 4 +- .../ProcessingExampleScriptsPlugin.py | 38 +++++++++++++++++++ .../algs/examplescripts/__init__.py | 31 +++++++++++++++ .../algs/examplescripts/examplescript.py | 3 ++ .../algs/examplescripts/metadata.txt | 18 +++++++++ .../algs/qgis/QGISAlgorithmProvider.py | 4 +- python/plugins/processing/core/Processing.py | 26 ++++++++++++- 7 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 python/plugins/processing/algs/examplescripts/ProcessingExampleScriptsPlugin.py create mode 100644 python/plugins/processing/algs/examplescripts/__init__.py create mode 100644 python/plugins/processing/algs/examplescripts/examplescript.py create mode 100644 python/plugins/processing/algs/examplescripts/metadata.txt diff --git a/python/plugins/processing/ProcessingPlugin.py b/python/plugins/processing/ProcessingPlugin.py index c4856af219d6..61a583d6c28f 100644 --- a/python/plugins/processing/ProcessingPlugin.py +++ b/python/plugins/processing/ProcessingPlugin.py @@ -55,11 +55,9 @@ class ProcessingPlugin: def __init__(self, iface): self.iface = iface - - def initGui(self): - Processing.initialize() + def initGui(self): self.commander = None self.toolbox = ProcessingToolbox() self.iface.addDockWidget(Qt.RightDockWidgetArea, self.toolbox) diff --git a/python/plugins/processing/algs/examplescripts/ProcessingExampleScriptsPlugin.py b/python/plugins/processing/algs/examplescripts/ProcessingExampleScriptsPlugin.py new file mode 100644 index 000000000000..20d0ecb2a916 --- /dev/null +++ b/python/plugins/processing/algs/examplescripts/ProcessingExampleScriptsPlugin.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- + +""" +*************************************************************************** + __init__.py + --------------------- + Date : May 2016 + Copyright : (C) 2016 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__ = 'May 2016' +__copyright__ = '(C) 2016, Victor Olaya' + +# This will get replaced with a git SHA1 when you do a git archive + +__revision__ = '$Format:%H$' + +import os + +from processing.core.Processing import Processing + +class ProcessingExampleScriptsPlugin: + + def initGui(self): + Processing.addScripts(self, os.path.dirname(__file__)) + + def unload(self): + pass diff --git a/python/plugins/processing/algs/examplescripts/__init__.py b/python/plugins/processing/algs/examplescripts/__init__.py new file mode 100644 index 000000000000..35952f5af29d --- /dev/null +++ b/python/plugins/processing/algs/examplescripts/__init__.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- + +""" +*************************************************************************** + __init__.py + --------------------- + Date : July 2013 + Copyright : (C) 2013 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. * +* * +*************************************************************************** +""" +from .ProcessingExampleScriptsPlugin import ProcessingExampleScriptsPlugin + +__author__ = 'Victor Olaya' +__date__ = 'July 2013' +__copyright__ = '(C) 2013, Victor Olaya' + +# This will get replaced with a git SHA1 when you do a git archive + +__revision__ = '$Format:%H$' + + +def classFactory(iface): + return ProcessingExampleScriptsPlugin() diff --git a/python/plugins/processing/algs/examplescripts/examplescript.py b/python/plugins/processing/algs/examplescripts/examplescript.py new file mode 100644 index 000000000000..2832f78697f8 --- /dev/null +++ b/python/plugins/processing/algs/examplescripts/examplescript.py @@ -0,0 +1,3 @@ +##text=string + +print text \ No newline at end of file diff --git a/python/plugins/processing/algs/examplescripts/metadata.txt b/python/plugins/processing/algs/examplescripts/metadata.txt new file mode 100644 index 000000000000..c40da321ac58 --- /dev/null +++ b/python/plugins/processing/algs/examplescripts/metadata.txt @@ -0,0 +1,18 @@ +[general] +name=Processing Example Scripts +description=An example plugin that adds algorithms to Processing as plugins +category=Analysis +version=1.0 +qgisMinimumVersion=2.0 + +author=Victor Olaya +email=volayaf@gmail.com + +tags=analysis,processing + +homepage= +tracker= +repository= + +experimental=False +deprecated=False diff --git a/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py b/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py index fc3a03cdcfaa..8fcad91d5d69 100644 --- a/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py +++ b/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py @@ -222,6 +222,8 @@ def __init__(self): from .ExecuteSQL import ExecuteSQL self.alglist.extend([ExecuteSQL()]) + self.externalAlgs = [] #to store algs added by 3rd party plugins as scripts + folder = os.path.join(os.path.dirname(__file__), 'scripts') scripts = ScriptUtils.loadFromFolder(folder) for script in scripts: @@ -246,7 +248,7 @@ def getIcon(self): return self._icon def _loadAlgorithms(self): - self.algs = self.alglist + self.algs = list(self.alglist) + self.externalAlgs def supportsNonFileBasedOutput(self): return True diff --git a/python/plugins/processing/core/Processing.py b/python/plugins/processing/core/Processing.py index 4031ffb1e766..a3a0359e641f 100644 --- a/python/plugins/processing/core/Processing.py +++ b/python/plugins/processing/core/Processing.py @@ -17,6 +17,7 @@ *************************************************************************** """ + __author__ = 'Victor Olaya' __date__ = 'August 2012' __copyright__ = '(C) 2012, Victor Olaya' @@ -26,6 +27,7 @@ __revision__ = '$Format:%H$' import sys +import os import traceback from qgis.PyQt.QtCore import Qt, QCoreApplication, QObject, pyqtSignal @@ -37,6 +39,9 @@ import processing from processing.core.AlgorithmProvider import AlgorithmProvider +from processing.script.ScriptUtils import ScriptUtils +from processing.gui import AlgorithmClassification +from processing.modeler.ModelerUtils import ModelerUtils from processing.core.ProcessingConfig import ProcessingConfig from processing.core.GeoAlgorithm import GeoAlgorithm from processing.core.ProcessingLog import ProcessingLog @@ -73,7 +78,7 @@ class Processing: contextMenuActions = [] @staticmethod - def addProvider(provider, update=True): + def addProvider(provider, updateList=True): """Use this method to add algorithms from external providers. """ @@ -122,6 +127,8 @@ def getProviderFromName(name): @staticmethod def initialize(): + if Processing.providers: + return # Add the basic providers for c in AlgorithmProvider.__subclasses__(): Processing.addProvider(c()) @@ -129,6 +136,23 @@ def initialize(): ProcessingConfig.initialize() ProcessingConfig.readSettings() RenderingStyles.loadStyles() + + @staticmethod + def addScripts(folder): + Processing.initialize() + provider = Processing.getProviderFromName("qgis") + scripts = ScriptUtils.loadFromFolder(folder) + print scripts + for script in scripts: + script.allowEdit = False + script._icon = provider._icon + script.provider = provider + provider.externalAlgs.extend(scripts) + Processing.reloadProvider("qgis") + + @staticmethod + def removeScripts(folder): + pass @staticmethod def updateAlgsList():