Skip to content
Browse files
Merge pull request #9172 from luipir/fix_19731
try to reset script path pointing to the current setting path
  • Loading branch information
luipir committed Apr 29, 2019
2 parents b48ae67 + a2bf5cc commit c0ec79ef052b8444770b2287f024ae5dd5912586
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 2 deletions.
@@ -27,7 +27,9 @@

import os

from qgis.core import (QgsApplication,
from qgis.core import (Qgis,

from processing.core.ProcessingConfig import ProcessingConfig, Setting
@@ -42,6 +44,7 @@
from processing.script.EditScriptAction import EditScriptAction
from processing.script.OpenScriptFromFileAction import OpenScriptFromFileAction
from processing.script import ScriptUtils
from import userFolder

class ScriptAlgorithmProvider(QgsProcessingProvider):
@@ -102,7 +105,16 @@ def supportsNonFileBasedOutput(self):
def loadAlgorithms(self):
self.algs = []
folders = ScriptUtils.scriptsFolders()
# always add default script folder to the list
defaultScriptFolder = ScriptUtils.defaultScriptsFolder()
if defaultScriptFolder not in folders:
# load all scripts
for folder in folders:
folder = ScriptUtils.resetScriptFolder(folder)
if not folder:

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"):
@@ -33,6 +33,7 @@
from qgis.PyQt.QtCore import QCoreApplication

from qgis.core import (Qgis,
@@ -57,7 +58,7 @@ def scriptsFolders():
if folder is not None:
return folder.split(";")
return [ScriptUtils.defaultScriptsFolder()]
return [defaultScriptsFolder()]

def loadAlgorithm(moduleName, filePath):
@@ -90,3 +91,46 @@ def findAlgorithmSource(name):
return scriptsRegistry[name]
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"),

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"),

return newFolder
@@ -20,4 +20,5 @@ IF(ENABLE_TESTS)
@@ -0,0 +1,83 @@
# -*- coding: utf-8 -*-

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')


class ScriptUtilsTest(unittest.TestCase):

def setUpClass(cls):
cls.cleanup_paths = []

def tearDownClass(cls):
for path in cls.cleanup_paths:

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__':

0 comments on commit c0ec79e

Please sign in to comment.