qgis_deployment_toolbelt.utils.check_path module

Helpers to check file: readable, exists, etc..

Author: Julien Moura (https://github.com/guts)

qgis_deployment_toolbelt.utils.check_path.check_folder_is_empty(input_var: Path, raise_error: bool = True) bool

Check if a variable is valid path, exists, is a folder and is empty.

Parameters:
  • input_var (Path) – path to check

  • raise_error (bool, optional) – if True, it raises an exception. Defaults to True.

Returns:

True if the path is pointing to an empty folder

Return type:

bool

qgis_deployment_toolbelt.utils.check_path.check_path(input_path: str | Path, must_exists: bool = True, must_be_readable: bool = True, must_be_writable: bool = False, must_be_a_folder: bool = False, must_be_a_file: bool = False, raise_error: bool = True) bool

Meta function of the module. Check if a given path complies with some constraints.

Parameters:
  • input_path (Union[str, Path]) – path to check

  • must_exists (bool, optional) – path must exist. Defaults to True.

  • must_be_readable (bool, optional) – path must be readable. Defaults to True.

  • must_be_writable (bool, optional) – path must be writable. Defaults to False.

  • must_be_a_folder (bool, optional) – path must be a folder. Mutually exclusive with must_be_a_file. Defaults to False.

  • must_be_a_file (bool, optional) – path must be a file. Mutually exclusive with must_be_a_folder. Defaults to False.

  • raise_error (bool, optional) – if True, it raises an exception. Defaults to True.

Raises:
  • ValueError – if must_be_a_file and must_be_a_folder are both set to True

  • FileNotFoundError – if the path is not a file and must_be_a_file is set to True

  • NotADirectoryError – if the path is not a folder and must_be_a_folder is set to True

Returns:

True if the path complies with constraints.

Return type:

bool

qgis_deployment_toolbelt.utils.check_path.check_path_exists(input_path: str | Path, attempt: int = 1, raise_error: bool = True) bool

Check if the input path (file or folder) exists.

Parameters:
  • input_path (Union[str, Path]) – path to check

  • attempt (int) – attempt count. If attempts == 2, it tries expanding user and variables.

  • raise_error (bool, optional) – if True, it raises an exception. Defaults to True.

Raises:

FileExistsError – if the path doesn’t exist and raise_error is False

Returns:

True if the path exists.

Return type:

bool

qgis_deployment_toolbelt.utils.check_path.check_path_is_readable(input_path: Path, raise_error: bool = True) bool

Check if the input path (file or folder) is readable.

Parameters:
  • input_path (Path) – path to check

  • raise_error (bool, optional) – if True, it raises an exception. Defaults to True.

Raises:

FileExistsError – if the path is not readable and raise_error is False

Returns:

True if the path is readable.

Return type:

bool

qgis_deployment_toolbelt.utils.check_path.check_path_is_writable(input_path: Path, raise_error: bool = True) bool

Check if the input path (file or folder) is writable.

Parameters:
  • input_path (Path) – path to check

  • raise_error (bool, optional) – if True, it raises an exception. Defaults to True.

Raises:

FileExistsError – if the path is not writable and raise_error is False

Returns:

True if the path is writable.

Return type:

bool

qgis_deployment_toolbelt.utils.check_path.check_var_can_be_path(input_var: str, attempt: int = 1, raise_error: bool = True) bool

Check is the path can be converted as pathlib.Path.

Parameters:
  • input_var (str) – var to check

  • attempt (int) – attempt count. If attempts == 2, it tries expanding user and variables.

  • raise_error (bool, optional) – if True, it raises an exception. Defaults to True.

Raises:
  • TypeError – if input path can’t be converted and raise_error is False

  • ValueError – if input path is an empty string

Returns:

True if the input can be converted to pathlib.Path

Return type:

bool