Skip to content

Commit

Permalink
[processing] separated alglist to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
volaya committed May 23, 2016
1 parent 9a5c583 commit 33fc3be
Show file tree
Hide file tree
Showing 24 changed files with 172 additions and 120 deletions.
7 changes: 3 additions & 4 deletions python/plugins/processing/ProcessingPlugin.py
Expand Up @@ -43,8 +43,7 @@
from processing.modeler.ModelerDialog import ModelerDialog
from processing.tools.system import tempFolder
from processing.gui.menus import removeMenus, initializeMenus, createMenus
from processing.core.defaultproviders import loadDefaultProviders

from processing.core.alglist import algList


cmd_folder = os.path.split(inspect.getfile(inspect.currentframe()))[0]
Expand All @@ -58,7 +57,7 @@ def __init__(self, iface):
self.iface = iface

def initGui(self):
loadDefaultProviders()

Processing.initialize()

self.commander = None
Expand Down Expand Up @@ -163,7 +162,7 @@ def openModeler(self):
dlg = ModelerDialog()
dlg.exec_()
if dlg.update:
self.toolbox.updateProvider('model')
algList.reloadProvider('model')

def openResults(self):
dlg = ResultsDialog()
Expand Down
64 changes: 26 additions & 38 deletions python/plugins/processing/core/Processing.py
Expand Up @@ -45,26 +45,28 @@
from processing.gui.Postprocessing import handleAlgorithmResults
from processing.gui.AlgorithmExecutor import runalg
from processing.tools import dataobjects


class AlgListWatcher(QObject):

providerAdded = pyqtSignal(str)
providerRemoved = pyqtSignal(str)

algListWatcher = AlgListWatcher()

from processing.core.alglist import algList

from processing.modeler.ModelerAlgorithmProvider import ModelerAlgorithmProvider
from processing.modeler.ModelerOnlyAlgorithmProvider import ModelerOnlyAlgorithmProvider
from processing.algs.qgis.QGISAlgorithmProvider import QGISAlgorithmProvider
from processing.algs.grass.GrassAlgorithmProvider import GrassAlgorithmProvider
from processing.algs.grass7.Grass7AlgorithmProvider import Grass7AlgorithmProvider
from processing.algs.lidar.LidarToolsAlgorithmProvider import LidarToolsAlgorithmProvider
from processing.algs.gdal.GdalOgrAlgorithmProvider import GdalOgrAlgorithmProvider
from processing.algs.otb.OTBAlgorithmProvider import OTBAlgorithmProvider
from processing.algs.r.RAlgorithmProvider import RAlgorithmProvider
from processing.algs.saga.SagaAlgorithmProvider import SagaAlgorithmProvider
from processing.script.ScriptAlgorithmProvider import ScriptAlgorithmProvider
from processing.algs.taudem.TauDEMAlgorithmProvider import TauDEMAlgorithmProvider
from processing.preconfigured.PreconfiguredAlgorithmProvider import PreconfiguredAlgorithmProvider

class Processing:

listeners = []
providers = []

# A dictionary of algorithms. Keys are names of providers
# and values are list with all algorithms from that provider
algs = {}

# Same structure as algs
# Same structure as algs in algList
actions = {}

# All the registered context menu actions for the toolbox
Expand All @@ -75,17 +77,16 @@ def addProvider(provider, update=True):
"""Use this method to add algorithms from external providers.
"""

if provider.getName() in [p.getName for p in Processing.providers]:
if provider.getName() in [p.getName for p in algList.providers]:
return
try:
provider.initializeSettings()
Processing.providers.append(provider)
ProcessingConfig.readSettings()
provider.loadAlgorithms()
Processing.algs[provider.getName()] = {a.commandLineName(): a for a in provider.algs}
Processing.actions[provider.getName()] = provider.actions
Processing.contextMenuActions.extend(provider.contextMenuActions)
algListWatcher.providerAdded.emit(provider.getName())
algList.addProvider(provider)
except:
ProcessingLog.addToLog(
ProcessingLog.LOG_ERROR,
Expand All @@ -103,8 +104,10 @@ def removeProvider(provider):
try:
provider.unload()
Processing.providers.remove(provider)
del Processing.algs[provider.getName()]
algListWatcher.providerRemoved.emit(provider.getName())
algList.remove(provider.getName())
del Processing.actions[provider.getName()]
for act in provider.contextMenuActions:
Processing.contextMenuActions.remove(act)
except:
# This try catch block is here to avoid problems if the
# plugin with a provider is unloaded after the Processing
Expand All @@ -115,10 +118,7 @@ def removeProvider(provider):
@staticmethod
def getProviderFromName(name):
"""Returns the provider with the given name."""
for provider in Processing.providers:
if provider.getName() == name:
return provider
return Processing.modeler
return algList.getProviderFromName(name)

@staticmethod
def initialize():
Expand All @@ -143,27 +143,15 @@ def updateAlgsList():

@staticmethod
def reloadProvider(providerName):
for p in Processing.providers:
if p.getName() == providerName:
p.loadAlgorithms()
Processing.algs[
p.getName()] = {a.commandLineName(): a for a in p.algs}

algList.reloadProvider(providerName)

@staticmethod
def getAlgorithm(name):
for provider in Processing.algs.values():
if name in provider:
return provider[name]
return None
return algList.getAlgorithm(name)

@staticmethod
def getAlgorithmFromFullName(name):
for provider in Processing.algs.values():
for alg in provider.values():
if alg.name == name:
return alg
return None
return algList.getAlgorithmFromFullName(name)

@staticmethod
def getObject(uri):
Expand Down
79 changes: 79 additions & 0 deletions python/plugins/processing/core/alglist.py
@@ -0,0 +1,79 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
Processing.py
---------------------
Date : August 2012
Copyright : (C) 2012 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__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

from qgis.PyQt.QtCore import QObject, pyqtSignal

class AlgorithmList(QObject):

providerAdded = pyqtSignal(str)
providerRemoved = pyqtSignal(str)
providerUpdated = pyqtSignal(str)

# A dictionary of algorithms. Keys are names of providers
# and values are list with all algorithms from that provider
algs = {}

providers = []

def removeProvider(self, providerName):
for p in self.providers:
if p.getName() == providerName:
self.providers.remove(p)
break
self.algs.remove(providerName)
self.providerRemoved.emit(providerName)

def reloadProvider(self, providerName):
for p in self.providers:
if p.getName() == providerName:
p.loadAlgorithms()
self.algs[p.getName()] = {a.commandLineName(): a for a in p.algs}
self.providerUpdated.emit(p.getName())
break

def addProvider(self, provider):
self.providers.append(provider)
self.algs[provider.getName()] = {a.commandLineName(): a for a in provider.algs}
self.providerAdded.emit(provider.getName())

def getProviderFromName(self, name):
for provider in self.providers:
if provider.getName() == name:
return provider

def getAlgorithm(self, name):
for provider in self.algs.values():
if name in provider:
return provider[name]

def getAlgorithmFromFullName(self, name):
for provider in self.algs.values():
for alg in provider.values():
if alg.name == name:
return alg

algList = AlgorithmList()
13 changes: 0 additions & 13 deletions python/plugins/processing/core/defaultproviders.py
Expand Up @@ -26,19 +26,6 @@
__revision__ = '$Format:%H$'


from processing.modeler.ModelerAlgorithmProvider import ModelerAlgorithmProvider
from processing.modeler.ModelerOnlyAlgorithmProvider import ModelerOnlyAlgorithmProvider
from processing.algs.qgis.QGISAlgorithmProvider import QGISAlgorithmProvider
from processing.algs.grass.GrassAlgorithmProvider import GrassAlgorithmProvider
from processing.algs.grass7.Grass7AlgorithmProvider import Grass7AlgorithmProvider
from processing.algs.lidar.LidarToolsAlgorithmProvider import LidarToolsAlgorithmProvider
from processing.algs.gdal.GdalOgrAlgorithmProvider import GdalOgrAlgorithmProvider
from processing.algs.otb.OTBAlgorithmProvider import OTBAlgorithmProvider
from processing.algs.r.RAlgorithmProvider import RAlgorithmProvider
from processing.algs.saga.SagaAlgorithmProvider import SagaAlgorithmProvider
from processing.script.ScriptAlgorithmProvider import ScriptAlgorithmProvider
from processing.algs.taudem.TauDEMAlgorithmProvider import TauDEMAlgorithmProvider
from processing.preconfigured.PreconfiguredAlgorithmProvider import PreconfiguredAlgorithmProvider

def loadDefaultProviders():
# this is here just to "trigger" the above imports so providers are loaded
Expand Down
7 changes: 3 additions & 4 deletions python/plugins/processing/gui/CommanderWindow.py
Expand Up @@ -32,6 +32,7 @@
from qgis.PyQt.QtCore import QSortFilterProxyModel
from qgis.utils import iface
from processing.core.Processing import Processing
from processing.core.alglist import algList
from processing.gui.MessageDialog import MessageDialog
from processing.gui.AlgorithmDialog import AlgorithmDialog
from processing.tools.system import userFolder, mkdir
Expand Down Expand Up @@ -99,9 +100,7 @@ def fillCombo(self):
self.combo.clear()

# Add algorithms
for providerName in Processing.algs.keys():
provider = Processing.algs[providerName]
algs = provider.values()
for algs in algList.algs.values():
for alg in algs:
self.combo.addItem('Processing algorithm: ' + alg.name)

Expand All @@ -111,7 +110,7 @@ def fillCombo(self):
types.FunctionType):
self.combo.addItem('Command: ' + command)

#Add menu entries
# Add menu entries
menuActions = []
actions = iface.mainWindow().menuBar().actions()
for action in actions:
Expand Down
8 changes: 0 additions & 8 deletions python/plugins/processing/gui/ContextAction.py
Expand Up @@ -35,14 +35,6 @@ def setData(self, itemData, toolbox):
self.itemData = itemData
self.toolbox = toolbox

def updateToolbox(self):
'''
Updates the list of algorithms and then the toolbox.
It only update the item corresponding to the provider of the algorithm.
To be called after the action is executed, if needed
'''
self.toolbox.updateProvider(self.alg.provider.getName())

def tr(self, string, context=''):
if context == '':
context = 'ContextAction'
Expand Down
5 changes: 3 additions & 2 deletions python/plugins/processing/gui/CreateNewScriptAction.py
Expand Up @@ -31,6 +31,7 @@

from processing.gui.ToolboxAction import ToolboxAction
from processing.gui.ScriptEditorDialog import ScriptEditorDialog
from processing.core.alglist import algList

pluginPath = os.path.split(os.path.dirname(__file__))[0]

Expand Down Expand Up @@ -62,6 +63,6 @@ def execute(self):
dlg.exec_()
if dlg.update:
if self.scriptType == self.SCRIPT_PYTHON:
self.toolbox.updateProvider('script')
algList.reloadProvider('script')
elif self.scriptType == self.SCRIPT_R:
self.toolbox.updateProvider('r')
algList.reloadProvider('r')
6 changes: 3 additions & 3 deletions python/plugins/processing/gui/DeleteScriptAction.py
Expand Up @@ -33,7 +33,7 @@

from processing.algs.r.RAlgorithm import RAlgorithm
from processing.script.ScriptAlgorithm import ScriptAlgorithm

from processing.core.alglist import algList

class DeleteScriptAction(ContextAction):

Expand All @@ -60,6 +60,6 @@ def execute(self):
if reply == QMessageBox.Yes:
os.remove(self.itemData.descriptionFile)
if self.scriptType == self.SCRIPT_PYTHON:
self.toolbox.updateProvider('script')
algList.reloadProvider('script')
elif self.scriptType == self.SCRIPT_R:
self.toolbox.updateProvider('r')
algList.reloadProvider('r')
6 changes: 3 additions & 3 deletions python/plugins/processing/gui/EditScriptAction.py
Expand Up @@ -29,7 +29,7 @@
from processing.gui.ScriptEditorDialog import ScriptEditorDialog
from processing.algs.r.RAlgorithm import RAlgorithm
from processing.script.ScriptAlgorithm import ScriptAlgorithm

from processing.core.alglist import algList

class EditScriptAction(ContextAction):

Expand All @@ -52,6 +52,6 @@ def execute(self):
dlg.exec_()
if dlg.update:
if self.scriptType == ScriptEditorDialog.SCRIPT_PYTHON:
self.toolbox.updateProvider('script')
algList.reloadProvider('script')
elif self.scriptType == ScriptEditorDialog.SCRIPT_R:
self.toolbox.updateProvider('r')
algList.reloadProvider('r')
14 changes: 7 additions & 7 deletions python/plugins/processing/gui/GetScriptsAndModels.py
Expand Up @@ -64,8 +64,8 @@ def getIcon(self):
def execute(self):
dlg = GetScriptsAndModelsDialog(GetScriptsAndModelsDialog.SCRIPTS)
dlg.exec_()
if dlg.updateToolbox:
self.toolbox.updateProvider('script')
if dlg.updateProvider:
algList.reloadProvider('script')


class GetRScriptsAction(ToolboxAction):
Expand All @@ -80,7 +80,7 @@ def getIcon(self):
def execute(self):
dlg = GetScriptsAndModelsDialog(GetScriptsAndModelsDialog.RSCRIPTS)
dlg.exec_()
if dlg.updateToolbox:
if dlg.updateProvider:
self.toolbox.updateProvider('r')


Expand All @@ -96,8 +96,8 @@ def getIcon(self):
def execute(self):
dlg = GetScriptsAndModelsDialog(GetScriptsAndModelsDialog.MODELS)
dlg.exec_()
if dlg.updateToolbox:
self.toolbox.updateProvider('model')
if dlg.updateProvider:
algList.reloadProvider('model')


class GetScriptsAndModelsDialog(BASE, WIDGET):
Expand Down Expand Up @@ -141,7 +141,7 @@ def __init__(self, resourceType):
self.icon = QIcon(os.path.join(pluginPath, 'images', 'r.svg'))

self.lastSelectedItem = None
self.updateToolbox = False
self.updateProvider = False
self.populateTree()
self.buttonBox.accepted.connect(self.okPressed)
self.buttonBox.rejected.connect(self.cancelPressed)
Expand Down Expand Up @@ -305,7 +305,7 @@ def okPressed(self):
if os.path.exists(path):
os.remove(path)

self.updateToolbox = len(toDownload) + len(toDelete) > 0
self.updateProvider = len(toDownload) + len(toDelete) > 0
super(GetScriptsAndModelsDialog, self).accept()


Expand Down

0 comments on commit 33fc3be

Please sign in to comment.