gpf_entrepot_toolbelt.utils.check_path module

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

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

gpf_entrepot_toolbelt.utils.check_path.check_file_extension_in_list(filepath: Path, extensions_list: Iterable) bool

Vérifie que le fichier possède une extension autorisée dans la liste en parametre

Paramètres:
  • filepath (Path) – chemin du fichier à vérifier

  • extensions_list (Iterable) – List of file extensions.

Renvoie:

True si le fichier doit être traité, False sinon

Type renvoyé:

bool

gpf_entrepot_toolbelt.utils.check_path.check_file_has_another_extension(filepath: Path, other_extension: str = '.shp') bool

Vérifie si le fichier en entrée existe également avec une autre extension.

Usage typique : déterminer si un fichier .dbf est associé à un .shp.

Paramètres:
  • filepath (pathlib.Path) – chemin vers le fichier

  • other_extension (str, optional) – extension (with its leading point). Defaults to « .shp ».

Renvoie:

True si le fichier avec l’autre extension existe, False s’il n’existe pas.

Type renvoyé:

bool

Exemple

>>> print(
check_file_has_another_extension(
    Path("/workdir/upload/livraison/BDTOPO/CANTON.dbf")
)
True
>>> print(
check_file_has_another_extension(
    filepath=Path("/workdir/upload/livraison/BDTOPO/CANTON.dbf"),
    other_extension=".shp"
)
True
>>> print(
check_file_has_another_extension(
    filepath=Path("/workdir/upload/livraison/BDTOPO/CANTON.shp"),
    other_extension=".dbf"
)
True
>>> print(
check_file_has_another_extension(
    filepath=Path("/workdir/upload/livraison/BDTOPO/CANTON.shp"),
    other_extension=".pdf"
)
False
gpf_entrepot_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, skip_log: bool = False, raise_error: bool = True) bool

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

Paramètres:
  • 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.

  • skip_log (bool, optional) – if True, does not log on error. Defaults to False.

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

Lève:
  • 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

Renvoie:

True if the path complies with constraints.

Type renvoyé:

bool

gpf_entrepot_toolbelt.utils.check_path.check_path_exists(input_path: str | Path, raise_error: bool = True, skip_log: bool = False) bool

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

Paramètres:
  • input_path (Union[str, Path]) – path to check

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

Lève:

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

Renvoie:

True if the path exists.

Type renvoyé:

bool

gpf_entrepot_toolbelt.utils.check_path.check_path_is_readable(input_path: Path, raise_error: bool = True, skip_log: bool = False) bool

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

Paramètres:
  • input_path (Path) – path to check

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

Lève:

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

Renvoie:

True if the path is readable.

Type renvoyé:

bool

gpf_entrepot_toolbelt.utils.check_path.check_path_is_writable(input_path: Path, raise_error: bool = True, skip_log: bool = False) bool

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

Paramètres:
  • input_path (Path) – path to check

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

Lève:

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

Renvoie:

True if the path is writable.

Type renvoyé:

bool

gpf_entrepot_toolbelt.utils.check_path.check_var_can_be_path(input_var: str, raise_error: bool = True, skip_log: bool = False) bool

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

Paramètres:
  • input_var (str) – var to check

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

Lève:

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

Renvoie:

True if the input can be converted to pathlib.Path

Type renvoyé:

bool