2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
ModelerAlgorithmProvider.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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2013-04-12 12:28:59 +02:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
import os
|
|
|
|
|
2017-04-04 12:58:25 +10:00
|
|
|
from qgis.core import (QgsApplication,
|
2017-04-24 14:35:50 +10:00
|
|
|
QgsProcessingProvider,
|
|
|
|
QgsMessageLog,
|
|
|
|
QgsProcessingUtils)
|
2017-01-09 12:34:46 +10:00
|
|
|
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.ProcessingConfig import ProcessingConfig, Setting
|
|
|
|
from processing.modeler.ModelerUtils import ModelerUtils
|
|
|
|
from processing.modeler.ModelerAlgorithm import ModelerAlgorithm
|
|
|
|
from processing.modeler.WrongModelException import WrongModelException
|
|
|
|
from processing.modeler.EditModelAction import EditModelAction
|
|
|
|
from processing.modeler.CreateNewModelAction import CreateNewModelAction
|
|
|
|
from processing.modeler.DeleteModelAction import DeleteModelAction
|
2014-04-20 16:07:04 +02:00
|
|
|
from processing.modeler.AddModelFromFileAction import AddModelFromFileAction
|
2014-06-02 22:43:47 +02:00
|
|
|
from processing.gui.GetScriptsAndModels import GetModelsAction
|
2017-04-04 12:52:21 +10:00
|
|
|
from processing.gui.ProviderActions import (ProviderActions,
|
|
|
|
ProviderContextMenuActions)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2015-05-18 19:51:26 +03:00
|
|
|
pluginPath = os.path.split(os.path.dirname(__file__))[0]
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2017-04-04 12:58:25 +10:00
|
|
|
class ModelerAlgorithmProvider(QgsProcessingProvider):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def __init__(self):
|
2017-01-09 09:18:25 +10:00
|
|
|
super().__init__()
|
2014-06-02 22:43:47 +02:00
|
|
|
self.actions = [CreateNewModelAction(), AddModelFromFileAction(), GetModelsAction()]
|
2014-06-08 00:21:12 +02:00
|
|
|
self.contextMenuActions = [EditModelAction(), DeleteModelAction()]
|
2017-04-04 11:04:36 +10:00
|
|
|
self.algs = []
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-04-04 09:46:46 +10:00
|
|
|
def load(self):
|
2017-04-04 12:22:29 +10:00
|
|
|
ProcessingConfig.settingIcons[self.name()] = self.icon()
|
2017-01-09 09:18:25 +10:00
|
|
|
ProcessingConfig.addSetting(Setting(self.name(),
|
2015-08-22 14:29:41 +02:00
|
|
|
ModelerUtils.MODELS_FOLDER, self.tr('Models folder', 'ModelerAlgorithmProvider'),
|
2016-05-24 21:56:17 +03:00
|
|
|
ModelerUtils.defaultModelsFolder(), valuetype=Setting.MULTIPLE_FOLDERS))
|
2017-04-04 12:42:00 +10:00
|
|
|
ProviderActions.registerProviderActions(self, self.actions)
|
2017-04-04 12:52:21 +10:00
|
|
|
ProviderContextMenuActions.registerProviderContextMenuActions(self.contextMenuActions)
|
2017-04-04 13:28:44 +10:00
|
|
|
ProcessingConfig.readSettings()
|
|
|
|
self.refreshAlgorithms()
|
2017-04-04 09:46:46 +10:00
|
|
|
return True
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-04-04 12:42:00 +10:00
|
|
|
def unload(self):
|
|
|
|
ProviderActions.deregisterProviderActions(self)
|
2017-04-04 12:52:21 +10:00
|
|
|
ProviderContextMenuActions.deregisterProviderContextMenuActions(self.contextMenuActions)
|
2017-04-04 12:42:00 +10:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
def modelsFolder(self):
|
2016-07-25 13:21:27 +02:00
|
|
|
return ModelerUtils.modelsFolders()[0]
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-01-09 09:18:25 +10:00
|
|
|
def name(self):
|
2014-10-03 21:56:24 +03:00
|
|
|
return self.tr('Models', 'ModelerAlgorithmProvider')
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-01-09 09:18:25 +10:00
|
|
|
def id(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
return 'model'
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-01-09 09:18:25 +10:00
|
|
|
def icon(self):
|
2017-01-09 12:34:46 +10:00
|
|
|
return QgsApplication.getThemeIcon("/processingModel.svg")
|
|
|
|
|
|
|
|
def svgIconPath(self):
|
|
|
|
return QgsApplication.iconPath("processingModel.svg")
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-04-04 09:46:46 +10:00
|
|
|
def loadAlgorithms(self):
|
2017-04-04 11:04:36 +10:00
|
|
|
self.algs = []
|
2016-05-24 21:56:17 +03:00
|
|
|
folders = ModelerUtils.modelsFolders()
|
|
|
|
for f in folders:
|
|
|
|
self.loadFromFolder(f)
|
2017-04-04 11:04:36 +10:00
|
|
|
for a in self.algs:
|
|
|
|
self.addAlgorithm(a)
|
2012-12-10 00:12:07 +01:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
def loadFromFolder(self, folder):
|
2012-11-10 00:31:01 +01:00
|
|
|
if not os.path.exists(folder):
|
|
|
|
return
|
2014-05-26 17:39:35 +02:00
|
|
|
for path, subdirs, files in os.walk(folder):
|
|
|
|
for descriptionFile in files:
|
|
|
|
if descriptionFile.endswith('model'):
|
2014-07-02 07:46:03 +02:00
|
|
|
try:
|
2014-05-26 17:39:35 +02:00
|
|
|
fullpath = os.path.join(path, descriptionFile)
|
2014-08-22 11:27:57 +02:00
|
|
|
alg = ModelerAlgorithm.fromFile(fullpath)
|
2017-03-29 12:51:59 +10:00
|
|
|
if alg.name():
|
2014-11-23 14:03:32 +01:00
|
|
|
alg.descriptionFile = fullpath
|
2017-04-04 11:04:36 +10:00
|
|
|
self.algs.append(alg)
|
2014-10-18 16:32:50 +02:00
|
|
|
else:
|
2017-04-26 12:52:23 +10:00
|
|
|
QgsMessageLog.logMessage(self.tr('Could not load model {0}', 'ModelerAlgorithmProvider').format(descriptionFile),
|
|
|
|
self.tr('Processing'), QgsMessageLog.CRITICAL)
|
2015-08-22 14:29:41 +02:00
|
|
|
except WrongModelException as e:
|
2017-04-26 12:52:23 +10:00
|
|
|
QgsMessageLog.logMessage(self.tr('Could not load model {0}\n{1}', 'ModelerAlgorithmProvider').format(descriptionFile, e.msg),
|
|
|
|
self.tr('Processing'), QgsMessageLog.CRITICAL)
|