2018-02-05 08:50:37 +02:00

81 lines
2.6 KiB
Python

# -*- coding: utf-8 -*-
"""
***************************************************************************
ScriptUtils.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$'
import os
import inspect
import importlib
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import QgsProcessingAlgorithm, QgsMessageLog
from processing.core.ProcessingConfig import ProcessingConfig
from processing.tools.system import mkdir, userFolder
SCRIPTS_FOLDERS = "SCRIPTS_FOLDERS"
def defaultScriptsFolder():
folder = str(os.path.join(userFolder(), "scripts"))
mkdir(folder)
return os.path.abspath(folder)
def scriptsFolders():
folder = ProcessingConfig.getSetting(SCRIPTS_FOLDERS)
if folder is not None:
return folder.split(";")
else:
return [ScriptUtils.defaultScriptsFolder()]
def loadAlgorithm(moduleName, filePath):
try:
spec = importlib.util.spec_from_file_location(moduleName, filePath)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
className = module.__all__[0]
obj = getattr(module, className)
return obj()
except ImportError as e:
QgsMessageLog.logMessage("Could not import script algorithm '{}' from '{}'\n{}".format(moduleName, filePath, str(e)),
"Processing",
QgsMessageLog.CRITICAL)
def findAlgorithmSource(className):
fileName = "{}.py".format(className)
folders = scriptsFolders()
for folder in folders:
items = os.scandir(folder)
for entry in items:
if entry.is_file() and entry.name == fileName:
return os.path.abspath(os.path.join(folder, fileName))
return None