2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
ScriptAlgorithmProvider.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'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2015-05-18 19:51:26 +03:00
|
|
|
import os
|
|
|
|
|
2019-02-13 18:52:18 +01:00
|
|
|
from qgis.core import (Qgis,
|
|
|
|
QgsMessageLog,
|
|
|
|
QgsApplication,
|
2017-04-04 12:58:25 +10:00
|
|
|
QgsProcessingProvider)
|
2017-01-09 12:34:46 +10:00
|
|
|
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.ProcessingConfig import ProcessingConfig, Setting
|
2018-01-29 16:46:48 +02:00
|
|
|
|
2017-04-04 12:52:21 +10:00
|
|
|
from processing.gui.ProviderActions import (ProviderActions,
|
|
|
|
ProviderContextMenuActions)
|
2018-01-29 15:50:16 +02:00
|
|
|
|
|
|
|
from processing.script.AddScriptFromFileAction import AddScriptFromFileAction
|
|
|
|
from processing.script.CreateNewScriptAction import CreateNewScriptAction
|
2018-03-29 08:37:20 +02:00
|
|
|
from processing.script.AddScriptFromTemplateAction import AddScriptFromTemplateAction
|
2018-01-29 15:50:16 +02:00
|
|
|
from processing.script.DeleteScriptAction import DeleteScriptAction
|
|
|
|
from processing.script.EditScriptAction import EditScriptAction
|
2018-05-20 17:20:56 +07:00
|
|
|
from processing.script.OpenScriptFromFileAction import OpenScriptFromFileAction
|
2018-01-29 16:46:48 +02:00
|
|
|
from processing.script import ScriptUtils
|
2019-02-13 18:52:18 +01:00
|
|
|
from processing.tools.system import userFolder
|
2015-05-18 19:51:26 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2017-04-04 12:58:25 +10:00
|
|
|
class ScriptAlgorithmProvider(QgsProcessingProvider):
|
2014-07-13 17:16:24 +02:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
def __init__(self):
|
2017-01-09 09:18:25 +10:00
|
|
|
super().__init__()
|
2017-04-04 11:04:36 +10:00
|
|
|
self.algs = []
|
2017-04-04 09:46:46 +10:00
|
|
|
self.folder_algorithms = []
|
2018-01-29 16:03:01 +02:00
|
|
|
self.actions = [CreateNewScriptAction(),
|
2018-03-29 08:37:20 +02:00
|
|
|
AddScriptFromTemplateAction(),
|
2018-05-20 17:20:56 +07:00
|
|
|
OpenScriptFromFileAction(),
|
2018-04-02 11:38:09 +10:00
|
|
|
AddScriptFromFileAction()
|
2018-01-29 19:18:54 +02:00
|
|
|
]
|
2018-01-29 15:50:16 +02:00
|
|
|
self.contextMenuActions = [EditScriptAction(),
|
|
|
|
DeleteScriptAction()]
|
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(),
|
2018-01-29 16:46:48 +02:00
|
|
|
ScriptUtils.SCRIPTS_FOLDERS,
|
|
|
|
self.tr("Scripts folder(s)"),
|
|
|
|
ScriptUtils.defaultScriptsFolder(),
|
|
|
|
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)
|
2018-01-29 16:46:48 +02:00
|
|
|
|
2017-04-04 13:28:44 +10:00
|
|
|
ProcessingConfig.readSettings()
|
|
|
|
self.refreshAlgorithms()
|
2018-01-29 16:46:48 +02:00
|
|
|
|
2017-04-04 09:46:46 +10:00
|
|
|
return True
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def unload(self):
|
2018-01-29 16:46:48 +02:00
|
|
|
ProcessingConfig.removeSetting(ScriptUtils.SCRIPTS_FOLDERS)
|
|
|
|
|
2017-04-04 12:42:00 +10:00
|
|
|
ProviderActions.deregisterProviderActions(self)
|
2017-04-04 12:52:21 +10:00
|
|
|
ProviderContextMenuActions.deregisterProviderContextMenuActions(self.contextMenuActions)
|
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("/processingScript.svg")
|
|
|
|
|
|
|
|
def svgIconPath(self):
|
|
|
|
return QgsApplication.iconPath("processingScript.svg")
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-01-09 09:18:25 +10:00
|
|
|
def id(self):
|
2018-01-29 16:46:48 +02:00
|
|
|
return "script"
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-01-09 09:18:25 +10:00
|
|
|
def name(self):
|
2018-01-29 16:46:48 +02:00
|
|
|
return self.tr("Scripts")
|
2018-01-25 12:21:32 +10:00
|
|
|
|
|
|
|
def supportsNonFileBasedOutput(self):
|
|
|
|
# TODO - this may not be strictly true. We probably need a way for scripts
|
|
|
|
# to indicate whether individual outputs support non-file based outputs,
|
|
|
|
# but for now allow it. At best we expose nice features to users, at worst
|
|
|
|
# they'll get an error if they use them with incompatible outputs...
|
|
|
|
return True
|
2018-01-29 16:46:48 +02:00
|
|
|
|
|
|
|
def loadAlgorithms(self):
|
|
|
|
self.algs = []
|
|
|
|
folders = ScriptUtils.scriptsFolders()
|
2019-02-13 18:52:18 +01:00
|
|
|
# always add default script folder to the list
|
|
|
|
defaultScriptFolder = ScriptUtils.defaultScriptsFolder()
|
|
|
|
if defaultScriptFolder not in folders:
|
|
|
|
folders.append(defaultScriptFolder)
|
|
|
|
# load all scripts
|
2018-01-29 16:46:48 +02:00
|
|
|
for folder in folders:
|
2019-02-13 18:52:18 +01:00
|
|
|
folder = ScriptUtils.resetScriptFolder(folder)
|
|
|
|
if not folder:
|
|
|
|
continue
|
|
|
|
|
2018-02-23 10:28:53 +02:00
|
|
|
items = [f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))]
|
2018-01-29 16:46:48 +02:00
|
|
|
for entry in items:
|
2018-02-23 10:28:53 +02:00
|
|
|
if entry.lower().endswith(".py"):
|
|
|
|
moduleName = os.path.splitext(os.path.basename(entry))[0]
|
|
|
|
filePath = os.path.abspath(os.path.join(folder, entry))
|
2018-02-01 12:50:04 +02:00
|
|
|
alg = ScriptUtils.loadAlgorithm(moduleName, filePath)
|
2018-01-29 16:46:48 +02:00
|
|
|
if alg is not None:
|
|
|
|
self.algs.append(alg)
|
|
|
|
|
|
|
|
for a in self.algs:
|
|
|
|
self.addAlgorithm(a)
|