embutils.utils.path module

Path checking utilities.

date

2021

author

Christian Wiche

contact

cwichel@gmail.com

license

The MIT License (MIT)

exception embutils.utils.path.FileSuffixError[source]

Bases: OSError

File suffix is not the expected.

exception embutils.utils.path.FileTypeError[source]

Bases: OSError

File type is not the expected.

class embutils.utils.path.Path(*args, **kwargs)[source]

Bases: pathlib.Path

Path class extensions.

Extends the Path initialization to new supported types.

Returns

Path object.

Return type

pl.Path

Raises

TypeError – Input type cant be converted to a path.

_accessor
_closed
reachable() bool[source]

Checks if the path is reachable.

Returns

True if reachable, false otherwise.

Return type

bool

static validate(path: Optional[Any] = None, none_ok: bool = False, reachable: bool = False, must_exist: bool = False) Optional[embutils.utils.path.Path][source]

Validates the provided input.

Parameters
  • path (TPAny) – Path to be converted / validated.

  • none_ok (bool) – Allows None input.

  • reachable (bool) – Path must be reachable.

  • must_exist (bool) – Path must exist.

Returns

Verified path.

Return type

Path

Raises
  • TypeError – Input type cant be converted to a path.

  • ValueError – Provided path is not supported.

  • FileNotFoundError – Path cant be reached or doesnt exist.

static validate_dir(path: Optional[Any] = None, none_ok: bool = False, must_exist: bool = False, create: bool = False) Optional[embutils.utils.path.Path][source]

Validate the directory path.

Parameters
  • path (TPAny) – Path to be converted / validated.

  • none_ok (bool) – Allows None input.

  • must_exist (bool) – Path must exist.

  • create (bool) – Create directory if it doesn’t exist.

Returns

Verified path.

Return type

Path

Raises
  • FileTypeError – Path is not a directory.

  • FileNotFoundError – Path cant be reached or doesnt exist.

static validate_file(path: Optional[Any] = None, none_ok: bool = False, must_exist: bool = False, default: Optional[str] = None, suffixes: Optional[List[str]] = None) Optional[embutils.utils.path.Path][source]

Validate the file path.

Parameters
  • path (TPAny) – Path to be converted / validated.

  • none_ok (bool) – Allows None input.

  • must_exist (bool) – Path must exist.

  • default (str) – Filename to be used when the input is a directory.

  • suffixes (tp.List[str]) – List of supported suffixes.

Returns

Verified path.

Return type

Path

Raises
  • FileTypeError – Path is not a file.

  • FileSuffixError – Path doesnt have the expected suffix.

  • FileNotFoundError – Path cant be reached or doesnt exist.

static which(name: str) embutils.utils.path.Path[source]

Get executable path.

Parameters

name (str) – Executable name.

Returns

Executable path.

Return type

Path

Raises

FileNotFoundError – Executable not found in PATH.