try to reset script path pointing to the current setting path

This commit is contained in:
Luigi Pirelli 2019-02-13 18:52:18 +01:00
parent 2e20f13279
commit a2bf5ccefd
4 changed files with 142 additions and 2 deletions

View File

@ -27,7 +27,9 @@ __revision__ = '$Format:%H$'
import os
from qgis.core import (QgsApplication,
from qgis.core import (Qgis,
QgsMessageLog,
QgsApplication,
QgsProcessingProvider)
from processing.core.ProcessingConfig import ProcessingConfig, Setting
@ -42,6 +44,7 @@ from processing.script.DeleteScriptAction import DeleteScriptAction
from processing.script.EditScriptAction import EditScriptAction
from processing.script.OpenScriptFromFileAction import OpenScriptFromFileAction
from processing.script import ScriptUtils
from processing.tools.system import userFolder
class ScriptAlgorithmProvider(QgsProcessingProvider):
@ -102,7 +105,16 @@ class ScriptAlgorithmProvider(QgsProcessingProvider):
def loadAlgorithms(self):
self.algs = []
folders = ScriptUtils.scriptsFolders()
# always add default script folder to the list
defaultScriptFolder = ScriptUtils.defaultScriptsFolder()
if defaultScriptFolder not in folders:
folders.append(defaultScriptFolder)
# load all scripts
for folder in folders:
folder = ScriptUtils.resetScriptFolder(folder)
if not folder:
continue
items = [f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))]
for entry in items:
if entry.lower().endswith(".py"):

View File

@ -33,6 +33,7 @@ import importlib
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (Qgis,
QgsApplication,
QgsProcessingAlgorithm,
QgsProcessingFeatureBasedAlgorithm,
QgsMessageLog
@ -57,7 +58,7 @@ def scriptsFolders():
if folder is not None:
return folder.split(";")
else:
return [ScriptUtils.defaultScriptsFolder()]
return [defaultScriptsFolder()]
def loadAlgorithm(moduleName, filePath):
@ -90,3 +91,46 @@ def findAlgorithmSource(name):
return scriptsRegistry[name]
except:
return None
def resetScriptFolder(folder):
"""Check if script folder exist. If not, notify and try to check if it is absolute to another user setting.
If so, modify folder to change user setting to the current user setting."""
newFolder = folder
if os.path.exists(newFolder):
return newFolder
QgsMessageLog.logMessage(QgsApplication .translate("loadAlgorithms", "Script folder {} does not exist").format(newFolder),
QgsApplication.translate("loadAlgorithms", "Processing"),
Qgis.Warning)
if not os.path.isabs(newFolder):
return None
# try to check if folder is absolute to other QgsApplication.qgisSettingsDirPath()
# isolate "QGIS3/profiles/"
appIndex = -4
profileIndex = -3
currentSettingPath = QgsApplication.qgisSettingsDirPath()
paths = currentSettingPath.split(os.sep)
commonSettingPath = os.path.join(paths[appIndex], paths[profileIndex])
if commonSettingPath in newFolder:
# strip not common folder part. e.g. preserve the profile path
# stripping the heading part that come from another location
tail = newFolder[newFolder.find(commonSettingPath):]
# tail folder with the actual userSetting path
header = os.path.join(os.sep, os.path.join(*paths[:appIndex]))
newFolder = os.path.join(header, tail)
# skip if it does not exist
if not os.path.exists(newFolder):
return None
QgsMessageLog.logMessage(QgsApplication .translate("loadAlgorithms", "Script folder changed into {}").format(newFolder),
QgsApplication.translate("loadAlgorithms", "Processing"),
Qgis.Warning)
return newFolder

View File

@ -19,4 +19,5 @@ IF(ENABLE_TESTS)
ADD_PYTHON_TEST(ProcessingGrass7AlgorithmsVectorTest Grass7AlgorithmsVectorTest.py)
ADD_PYTHON_TEST(ProcessingSagaAlgorithmsTest SagaAlgorithmsTest.py)
ADD_PYTHON_TEST(ProcessingCheckValidityAlgorithmTest CheckValidityAlgorithm.py)
ADD_PYTHON_TEST(ProcessingScriptUtilsTest ScriptUtilsTest.py)
ENDIF(ENABLE_TESTS)

View File

@ -0,0 +1,83 @@
# -*- coding: utf-8 -*-
"""
***************************************************************************
ScriptUtilsTest
---------------------
Date : February 2019
Copyright : (C) 2019 by Luigi Pirelli
Email : luipir 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__ = 'Luigi Pirelli'
__date__ = 'February 2019'
__copyright__ = '(C) 2019, Luigi Pirelli'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import os
import shutil
import tempfile
from qgis.core import NULL, QgsApplication
from qgis.testing import start_app, unittest
from processing.script import ScriptUtils
testDataPath = os.path.join(os.path.dirname(__file__), 'testdata')
start_app()
class ScriptUtilsTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.cleanup_paths = []
@classmethod
def tearDownClass(cls):
for path in cls.cleanup_paths:
shutil.rmtree(path)
def testResetScriptFolder(self):
# if folder exist
defaultScriptFolder = ScriptUtils.defaultScriptsFolder()
folder = ScriptUtils.resetScriptFolder(defaultScriptFolder)
self.assertEqual(folder, defaultScriptFolder)
folder = ScriptUtils.resetScriptFolder('.')
self.assertEqual(folder, '.')
# if folder does not exist and not absolute
folder = ScriptUtils.resetScriptFolder('fake')
self.assertEqual(folder, None)
# if absolute but not relative to QgsApplication.qgisSettingsDirPath()
folder = os.path.join(tempfile.gettempdir(), 'fakePath')
newFolder = ScriptUtils.resetScriptFolder(folder)
self.assertEqual(newFolder, folder)
# if absolute profile but poiting somewhere
# reset the path as pointing to profile into the current settings
folder = QgsApplication.qgisSettingsDirPath()
# modify default profile changing absolute path pointing somewhere
paths = folder.split(os.sep)
paths[0] = '/'
paths[1] = 'fakelocation'
folder = os.path.join(*paths)
folder = ScriptUtils.resetScriptFolder(folder)
self.assertEqual(folder, QgsApplication.qgisSettingsDirPath())
if __name__ == '__main__':
unittest.main()