Skip to content

Commit

Permalink
try to reset script path pointing to the current setting path
Browse files Browse the repository at this point in the history
  • Loading branch information
luipir committed Feb 13, 2019
1 parent 2e20f13 commit a2bf5cc
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 2 deletions.
14 changes: 13 additions & 1 deletion python/plugins/processing/script/ScriptAlgorithmProvider.py
Expand Up @@ -27,7 +27,9 @@

import os

from qgis.core import (QgsApplication,
from qgis.core import (Qgis,
QgsMessageLog,
QgsApplication,
QgsProcessingProvider)

from processing.core.ProcessingConfig import ProcessingConfig, Setting
Expand All @@ -42,6 +44,7 @@
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):
Expand Down Expand Up @@ -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:
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"):
Expand Down
46 changes: 45 additions & 1 deletion python/plugins/processing/script/ScriptUtils.py
Expand Up @@ -33,6 +33,7 @@
from qgis.PyQt.QtCore import QCoreApplication

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


def loadAlgorithm(moduleName, filePath):
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions python/plugins/processing/tests/CMakeLists.txt
Expand Up @@ -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)
83 changes: 83 additions & 0 deletions python/plugins/processing/tests/ScriptUtilsTest.py
@@ -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()

0 comments on commit a2bf5cc

Please sign in to comment.