Skip to content

Commit a2bf5cc

Browse files
committedFeb 13, 2019
try to reset script path pointing to the current setting path
1 parent 2e20f13 commit a2bf5cc

File tree

4 files changed

+142
-2
lines changed

4 files changed

+142
-2
lines changed
 

‎python/plugins/processing/script/ScriptAlgorithmProvider.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727

2828
import os
2929

30-
from qgis.core import (QgsApplication,
30+
from qgis.core import (Qgis,
31+
QgsMessageLog,
32+
QgsApplication,
3133
QgsProcessingProvider)
3234

3335
from processing.core.ProcessingConfig import ProcessingConfig, Setting
@@ -42,6 +44,7 @@
4244
from processing.script.EditScriptAction import EditScriptAction
4345
from processing.script.OpenScriptFromFileAction import OpenScriptFromFileAction
4446
from processing.script import ScriptUtils
47+
from processing.tools.system import userFolder
4548

4649

4750
class ScriptAlgorithmProvider(QgsProcessingProvider):
@@ -102,7 +105,16 @@ def supportsNonFileBasedOutput(self):
102105
def loadAlgorithms(self):
103106
self.algs = []
104107
folders = ScriptUtils.scriptsFolders()
108+
# always add default script folder to the list
109+
defaultScriptFolder = ScriptUtils.defaultScriptsFolder()
110+
if defaultScriptFolder not in folders:
111+
folders.append(defaultScriptFolder)
112+
# load all scripts
105113
for folder in folders:
114+
folder = ScriptUtils.resetScriptFolder(folder)
115+
if not folder:
116+
continue
117+
106118
items = [f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))]
107119
for entry in items:
108120
if entry.lower().endswith(".py"):

‎python/plugins/processing/script/ScriptUtils.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from qgis.PyQt.QtCore import QCoreApplication
3434

3535
from qgis.core import (Qgis,
36+
QgsApplication,
3637
QgsProcessingAlgorithm,
3738
QgsProcessingFeatureBasedAlgorithm,
3839
QgsMessageLog
@@ -57,7 +58,7 @@ def scriptsFolders():
5758
if folder is not None:
5859
return folder.split(";")
5960
else:
60-
return [ScriptUtils.defaultScriptsFolder()]
61+
return [defaultScriptsFolder()]
6162

6263

6364
def loadAlgorithm(moduleName, filePath):
@@ -90,3 +91,46 @@ def findAlgorithmSource(name):
9091
return scriptsRegistry[name]
9192
except:
9293
return None
94+
95+
96+
def resetScriptFolder(folder):
97+
"""Check if script folder exist. If not, notify and try to check if it is absolute to another user setting.
98+
If so, modify folder to change user setting to the current user setting."""
99+
100+
newFolder = folder
101+
if os.path.exists(newFolder):
102+
return newFolder
103+
104+
QgsMessageLog.logMessage(QgsApplication .translate("loadAlgorithms", "Script folder {} does not exist").format(newFolder),
105+
QgsApplication.translate("loadAlgorithms", "Processing"),
106+
Qgis.Warning)
107+
108+
if not os.path.isabs(newFolder):
109+
return None
110+
111+
# try to check if folder is absolute to other QgsApplication.qgisSettingsDirPath()
112+
113+
# isolate "QGIS3/profiles/"
114+
appIndex = -4
115+
profileIndex = -3
116+
currentSettingPath = QgsApplication.qgisSettingsDirPath()
117+
paths = currentSettingPath.split(os.sep)
118+
commonSettingPath = os.path.join(paths[appIndex], paths[profileIndex])
119+
120+
if commonSettingPath in newFolder:
121+
# strip not common folder part. e.g. preserve the profile path
122+
# stripping the heading part that come from another location
123+
tail = newFolder[newFolder.find(commonSettingPath):]
124+
# tail folder with the actual userSetting path
125+
header = os.path.join(os.sep, os.path.join(*paths[:appIndex]))
126+
newFolder = os.path.join(header, tail)
127+
128+
# skip if it does not exist
129+
if not os.path.exists(newFolder):
130+
return None
131+
132+
QgsMessageLog.logMessage(QgsApplication .translate("loadAlgorithms", "Script folder changed into {}").format(newFolder),
133+
QgsApplication.translate("loadAlgorithms", "Processing"),
134+
Qgis.Warning)
135+
136+
return newFolder

‎python/plugins/processing/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ IF(ENABLE_TESTS)
1919
ADD_PYTHON_TEST(ProcessingGrass7AlgorithmsVectorTest Grass7AlgorithmsVectorTest.py)
2020
ADD_PYTHON_TEST(ProcessingSagaAlgorithmsTest SagaAlgorithmsTest.py)
2121
ADD_PYTHON_TEST(ProcessingCheckValidityAlgorithmTest CheckValidityAlgorithm.py)
22+
ADD_PYTHON_TEST(ProcessingScriptUtilsTest ScriptUtilsTest.py)
2223
ENDIF(ENABLE_TESTS)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
ScriptUtilsTest
6+
---------------------
7+
Date : February 2019
8+
Copyright : (C) 2019 by Luigi Pirelli
9+
Email : luipir at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Luigi Pirelli'
21+
__date__ = 'February 2019'
22+
__copyright__ = '(C) 2019, Luigi Pirelli'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
import os
29+
import shutil
30+
import tempfile
31+
32+
from qgis.core import NULL, QgsApplication
33+
from qgis.testing import start_app, unittest
34+
35+
from processing.script import ScriptUtils
36+
37+
testDataPath = os.path.join(os.path.dirname(__file__), 'testdata')
38+
39+
start_app()
40+
41+
42+
class ScriptUtilsTest(unittest.TestCase):
43+
44+
@classmethod
45+
def setUpClass(cls):
46+
cls.cleanup_paths = []
47+
48+
@classmethod
49+
def tearDownClass(cls):
50+
for path in cls.cleanup_paths:
51+
shutil.rmtree(path)
52+
53+
def testResetScriptFolder(self):
54+
# if folder exist
55+
defaultScriptFolder = ScriptUtils.defaultScriptsFolder()
56+
folder = ScriptUtils.resetScriptFolder(defaultScriptFolder)
57+
self.assertEqual(folder, defaultScriptFolder)
58+
folder = ScriptUtils.resetScriptFolder('.')
59+
self.assertEqual(folder, '.')
60+
# if folder does not exist and not absolute
61+
folder = ScriptUtils.resetScriptFolder('fake')
62+
self.assertEqual(folder, None)
63+
# if absolute but not relative to QgsApplication.qgisSettingsDirPath()
64+
folder = os.path.join(tempfile.gettempdir(), 'fakePath')
65+
newFolder = ScriptUtils.resetScriptFolder(folder)
66+
self.assertEqual(newFolder, folder)
67+
68+
# if absolute profile but poiting somewhere
69+
# reset the path as pointing to profile into the current settings
70+
folder = QgsApplication.qgisSettingsDirPath()
71+
72+
# modify default profile changing absolute path pointing somewhere
73+
paths = folder.split(os.sep)
74+
paths[0] = '/'
75+
paths[1] = 'fakelocation'
76+
folder = os.path.join(*paths)
77+
78+
folder = ScriptUtils.resetScriptFolder(folder)
79+
self.assertEqual(folder, QgsApplication.qgisSettingsDirPath())
80+
81+
82+
if __name__ == '__main__':
83+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.