Class: QgsPathResolver¶
Resolves relative paths into absolute paths and vice versa.
Used for writing and reading paths for projects.
Methods
Turn filename read from the project file to an absolute path |
|
Prepare a filename to save it to the project file. |
Static Methods
Removes the custom pre-processor function with matching id. |
|
Removes the custom writer function with matching id. |
|
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. |
|
Sets a path writer function, which allows for manipulation of paths and data sources prior to writing them to the project file. |
- class qgis.core.QgsPathResolver[source]¶
Bases:
object
- __init__(baseFileName: str | None = '', attachmentDir: str | None = '')
Initialize path resolver with a base filename. Null filename means no conversion between relative/absolute path
- Parameters:
baseFileName (Optional[str] = '')
attachmentDir (Optional[str] = '')
- __init__(a0: QgsPathResolver)
- Parameters:
a0 (QgsPathResolver)
- readPath(self, filename: str | None) str [source]¶
Turn filename read from the project file to an absolute path
- Parameters:
filename (Optional[str])
- Return type:
str
- static removePathPreprocessor(id: str | None)[source]¶
Removes the custom pre-processor function with matching
id
.The
id
must correspond to a pre-processor previously added via a call tosetPathPreprocessor()
.- Raises:
KeyError – if no processor with the specified
id
exists.
See also
Added in version 3.10.
- Parameters:
id (Optional[str])
- static removePathWriter(id: str | None)[source]¶
Removes the custom writer function with matching
id
.The
id
must correspond to a writer previously added via a call tosetPathWriter()
. An KeyError will be raised if no processor with the specifiedid
exists.See also
Added in version 3.22.
- Parameters:
id (Optional[str])
- static setPathPreprocessor(a0: Callable[..., None] | None) str [source]¶
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.
If multiple preprocessors are set, they will be called in sequence based on the order in which they were originally set.
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)
Added in version 3.10.
- Parameters:
a0 (Optional[Callable[..., None]])
- Return type:
str
- static setPathWriter(a0: Callable[..., None] | None) str [source]¶
Sets a path writer function, which allows for manipulation of paths and data sources prior to writing them to the project file.
The
writer
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 writer function is called before any bad layer handler.
If multiple writers are set, they will be called in sequence based on the order in which they were originally set.
Example - replace path with a variable:
def my_processor(path): return path.replace('c:/Users/ClintBarton/Documents/Projects', '$projectdir$') QgsPathResolver.setPathWriter(my_processor)
Added in version 3.22.
- Parameters:
a0 (Optional[Callable[..., None]])
- Return type:
str