Class: QgsPathResolver

class qgis.core.QgsPathResolver(baseFileName: str = '')

Bases: sip.wrapper

Initialize path resolver with a base filename. Null filename means no conversion between relative/absolute path

QgsPathResolver(QgsPathResolver)

Resolves relative paths into absolute paths and vice versa. Used for writing

New in version 3.0: Enums

Methods

readPath

Turn filename read from the project file to an absolute path

setPathPreprocessor

Sets a path pre-processor function, which allows for manipulation of paths and data sources prior to resolving them to file references or layer sources.

writePath

Prepare a filename to save it to the project file.

Signals

Attributes

readPath(self, filename: str) → str

Turn filename read from the project file to an absolute path

setPathPreprocessor(Callable[..., None])

Sets a 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.

Note

Setting a new processor replaces any existing processor.

Example - replace an outdated folder path with a new one: .. code-block:: python

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: .. code-block:: python

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: .. code-block:: python

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)

New in version 3.10.

writePath(self, filename: str) → str

Prepare a filename to save it to the project file. Creates an absolute or relative path according to the project settings. Paths written to the project file should be prepared with this method.