-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[API][FEATURE] Allow setting a custom path pre-processor for QgsPathR…
…esolver QgsPathResolver::setPathPreprocessor allows setting a custom path pre-processor function, which allows for manipulation of paths and data sources prior to resolving them to file references or layer sources. The processor function must accept a single string argument (representing the original file path or data source), and return a processed version of this path. The path pre-processor function is called before any bad layer handler. Example - replace an outdated folder path with a new one: def my_processor(path): return path.replace('c:/Users/ClintBarton/Documents/Projects', 'x:/Projects/') QgsPathResolver.setPathPreprocessor(my_processor) Example - replace a stored database host with a new one: def my_processor(path): return path.replace('host=10.1.1.115', 'host=10.1.1.116') QgsPathResolver.setPathPreprocessor(my_processor) Example - replace stored database credentials with new ones: def my_processor(path): path= path.replace("user='gis_team'", "user='team_awesome'") path = path.replace("password='cats'", "password='g7as!m*'") return path QgsPathResolver.setPathPreprocessor(my_processor)
1 parent
d2f07e8
commit 5dc39fe
Showing
5 changed files
with
241 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# -*- coding: utf-8 -*- | ||
"""QGIS Unit tests for QgsPathResolver. | ||
.. note:: 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__ = 'Nyall Dawson' | ||
__date__ = '22/07/2019' | ||
__copyright__ = 'Copyright 2019, The QGIS Project' | ||
|
||
import qgis # NOQA | ||
|
||
import tempfile | ||
import os | ||
from qgis.core import QgsPathResolver, QgsVectorLayer, QgsProject | ||
from qgis.testing import start_app, unittest | ||
from utilities import unitTestDataPath | ||
|
||
start_app() | ||
|
||
TEST_DATA_DIR = unitTestDataPath() | ||
|
||
PROCESSOR = None | ||
|
||
|
||
class TestQgsPathResolver(unittest.TestCase): | ||
|
||
def testCustomPreprocessor(self): | ||
self.assertEqual(QgsPathResolver().readPath('aaaaa'), 'aaaaa') | ||
|
||
def my_processor(path): | ||
return path.upper() | ||
|
||
global PROCESSOR | ||
PROCESSOR = my_processor | ||
QgsPathResolver.setPathPreprocessor(PROCESSOR) | ||
self.assertEqual(QgsPathResolver().readPath('aaaaa'), 'AAAAA') | ||
|
||
def testLoadLayerWithPreprocessor(self): | ||
""" | ||
Test that custom path preprocessor is used when loading layers | ||
""" | ||
lines_shp_path = os.path.join(TEST_DATA_DIR, 'moooooo.shp') | ||
|
||
lines_layer = QgsVectorLayer(lines_shp_path, 'Lines', 'ogr') | ||
self.assertFalse(lines_layer.isValid()) | ||
p = QgsProject() | ||
p.addMapLayer(lines_layer) | ||
# save project to a temporary file | ||
temp_path = tempfile.mkdtemp() | ||
temp_project_path = os.path.join(temp_path, 'temp.qgs') | ||
self.assertTrue(p.write(temp_project_path)) | ||
|
||
p2 = QgsProject() | ||
self.assertTrue(p2.read(temp_project_path)) | ||
l = p2.mapLayersByName('Lines')[0] | ||
self.assertEqual(l.name(), 'Lines') | ||
self.assertFalse(l.isValid()) | ||
|
||
# custom processor to fix path | ||
def my_processor(path): | ||
return path.replace('moooooo', 'lines') | ||
|
||
global PROCESSOR | ||
PROCESSOR = my_processor | ||
QgsPathResolver.setPathPreprocessor(PROCESSOR) | ||
p3 = QgsProject() | ||
self.assertTrue(p3.read(temp_project_path)) | ||
l = p3.mapLayersByName('Lines')[0] | ||
self.assertEqual(l.name(), 'Lines') | ||
# layer should have correct path now | ||
self.assertTrue(l.isValid()) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |