2012-10-04 19:33:47 +02:00
# -*- 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 '
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$ '
2012-09-15 18:25:25 +03:00
import os
2018-01-29 16:46:48 +02:00
import inspect
import importlib
from qgis . PyQt . QtCore import QCoreApplication
2018-02-05 22:11:34 -04:00
from qgis . core import ( Qgis ,
QgsProcessingAlgorithm ,
2018-02-02 08:34:47 +02:00
QgsProcessingFeatureBasedAlgorithm ,
QgsMessageLog
)
2018-01-29 16:46:48 +02:00
2013-08-12 20:44:27 +02:00
from processing . core . ProcessingConfig import ProcessingConfig
2014-07-10 17:24:07 +02:00
from processing . tools . system import mkdir , userFolder
2012-09-15 18:25:25 +03:00
2018-02-01 12:50:04 +02:00
scriptsRegistry = dict ( )
2018-01-29 16:46:48 +02:00
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 ( ) ]
2017-03-04 19:41:23 +01:00
2015-08-22 14:29:41 +02:00
2018-01-29 16:46:48 +02:00
def loadAlgorithm ( moduleName , filePath ) :
2018-02-01 12:50:04 +02:00
global scriptsRegistry
2018-01-29 16:46:48 +02:00
try :
spec = importlib . util . spec_from_file_location ( moduleName , filePath )
module = importlib . util . module_from_spec ( spec )
spec . loader . exec_module ( module )
2018-02-01 12:50:04 +02:00
for x in dir ( module ) :
obj = getattr ( module , x )
2018-02-02 08:34:47 +02:00
if inspect . isclass ( obj ) and issubclass ( obj , ( QgsProcessingAlgorithm , QgsProcessingFeatureBasedAlgorithm ) ) and obj . __name__ not in ( " QgsProcessingAlgorithm " , " QgsProcessingFeatureBasedAlgorithm " ) :
2018-02-01 12:50:04 +02:00
scriptsRegistry [ x ] = filePath
return obj ( )
2018-01-29 16:46:48 +02:00
except ImportError as e :
2018-02-18 15:49:19 +01:00
QgsMessageLog . logMessage ( QCoreApplication . translate ( " ScriptUtils " , " Could not import script algorithm ' {} ' from ' {} ' \n {} " ) . format ( moduleName , filePath , str ( e ) ) ,
QCoreApplication . translate ( " ScriptUtils " , " Processing " ) ,
2018-02-05 22:11:34 -04:00
Qgis . Critical )
2018-01-29 18:54:26 +02:00
def findAlgorithmSource ( className ) :
2018-02-01 12:50:04 +02:00
global scriptsRegistry
try :
return scriptsRegistry [ className ]
except :
return None