scenario.path module

Path management.

AnyPathType

Type for path-like data: either a simple string or a os.PathLike instance.

class Path

Bases: object

Helper class for path management.

This class really looks like pathlib.Path, but differs from it in that:

  1. it ensures persistent paths, even though initialized from a relative path and the current directory changes afterwards,

  2. it provides a prettypath display from a main directory set for the current project (see setmainpath()),

  3. it does not describe the current working implicitely when initialized from nothing, but a void path.

The Path class supports the os.PathLike interface.

_main_path

Main path, used to compute the relative prettypath. Unset by default.

static setmainpath(path, log_level=20)

Sets the main path, used to compute the relative prettypath.

Parameters:
  • path – New main path.

  • log_level – Log level (as defined by the standard logging package) to use for the related log line.

static getmainpath()
Returns:

Main path, i.e. base path for prettypath computations.

static cwd()

Computes a Path instance representing the current working directory.

Returns:

Current working directory.

static home()

Computes a Path instance representing the current user’s home directory.

Returns:

Current user’s home directory.

static tmp()

Computes a Path instance representing the temporary directory.

Returns:

Temporary directory.

__init__(path=None, relative_to=None)

Ensures the management of an absolute path.

Parameters:
  • path

    File or directory path as a path-like.

    Makes the Path instance void when not set.

  • relative_to

    Base directory or file to consider as the root, when the path given is a relative path.

    Giving a file path as relative_to is equivalent to giving its owner directory.

If the path given is relative, it is transformed in its absolute form from the current working directory.

_abspath

pathlib.Path instance used to store the absolute path described by this Path instance.

as_posix

Shortcut to pathlib.PurePath.as_posix().

as_uri

Shortcut to pathlib.PurePath.as_uri().

is_absolute

Shortcut to pathlib.PurePath.is_absolute().

is_reserved

Shortcut to pathlib.PurePath.is_reserved().

match

Shortcut to pathlib.PurePath.match().

stat

Shortcut to pathlib.Path.stat().

chmod

Shortcut to pathlib.Path.chmod().

exists

Shortcut to pathlib.Path.exists().

expanduser

Shortcut to pathlib.Path.expanduser().

group

Shortcut to pathlib.Path.group().

is_dir

Shortcut to pathlib.Path.is_dir().

is_file

Shortcut to pathlib.Path.is_file().

is_mount

Shortcut to pathlib.Path.is_mount().

Shortcut to pathlib.Path.is_symlink().

is_socket

Shortcut to pathlib.Path.is_socket().

is_fifo

Shortcut to pathlib.Path.is_fifo().

is_block_device

Shortcut to pathlib.Path.is_block_device().

is_char_device

Shortcut to pathlib.Path.is_char_device().

lchmod

Shortcut to pathlib.Path.lchmod().

lstat

Shortcut to pathlib.Path.lstat().

mkdir

Shortcut for pathlib.Path.mkdir().

open

Shortcut to pathlib.Path.open().

owner

Shortcut to pathlib.Path.owner().

read_bytes

Shortcut to pathlib.Path.read_bytes().

read_text

Shortcut to pathlib.Path.read_text().

Shortcut to pathlib.Path.readlink().

rmdir

Shortcut to pathlib.Path.rmdir().

Shortcut to pathlib.Path.symlink_to().

Shortcut to pathlib.Path.hardlink_to().

Shortcut to pathlib.Path.link_to().

touch

Shortcut to pathlib.Path.touch().

Shortcut to pathlib.Path.unlink().

write_bytes

Shortcut to pathlib.Path.write_bytes().

write_text

Shortcut to pathlib.Path.write_text().

__fspath__()

os.PathLike interface implementation.

__repr__()

Canonical string representation.

__str__()

Human readable string representation (same as prettypath).

__hash__()

Hash computation.

Makes it possible to use Path objects as dictionary keys.

property parts

See pathlib.PurePath.parts.

property drive

See pathlib.PurePath.drive.

property root

See pathlib.PurePath.root.

property anchor

See pathlib.PurePath.anchor.

property parents

Gives the list of parent directories as Path objects.

See pathlib.PurePath.parents.

property parent

Gives the parent directory as a Path object.

See pathlib.PurePath.parent.

property name

Base name of the path.

See pathlib.PurePath.name.

property suffix

Gives the extension of the file (or directory name), with its leading dot, if any, or an empty string if no extension.

See pathlib.PurePath.suffix.

property suffixes

Gives the list of consecutive extensions, with their leading dot character.

See pathlib.PurePath.suffixes.

property stem

Gives the basename of the path, without the final extension if any.

See pathlib.PurePath.stem.

property abspath

Absolute form of the path in the POSIX style.

property prettypath

Gives the pretty path.

The pretty path is actually a relative path from the main path if set (see setmainpath()), or the current working directory otherwise, and presented in the POSIX style.

resolve()

Retrieves another Path instance similar to this one.

Returns:

New Path instance.

__eq__(other)

Checks whether other equals to this path.

Parameters:

other – Path to checks against.

Returns:

True when the paths equal, False otherwise.

samefile(other)

Returns True when other describes the same path as this one.

Parameters:

other – Other path (or anything that is not a path at all).

Returns:

True when other is the same path.

__truediv__(other)

Joins this directory path with a sub-path.

Parameters:

other – Sub-path to apply from this directory path.

Returns:

New Path instance.

joinpath(*other)

Joins this directory path with a list of sub-paths.

Parameters:

other – Sub-paths to apply from this directory path.

Returns:

New Path instance.

with_name(name)

See pathlib.PurePath.with_name().

with_stem(stem)

See pathlib.PurePath.with_stem().

with_suffix(suffix)

See pathlib.PurePath.with_suffix().

is_void()

Tells whether this path is void.

Returns:

True when the path is void, False otherwise.

is_relative_to(other)

Tells whether this path is a sub-path of the candidate parent directory.

Parameters:

other – Candidate parent directory.

Returns:

True when this path is a sub-path of other.

See pathlib.PurePath.is_relative_to().

relative_to(other)

Computes a relative path.

Parameters:

other – Reference path to compute the relative path from.

Returns:

Relative path from other in the POSIX style.

Note

The behaviour of this method differs from the one of pathlib.PurePath.relative_to().

pathlib.PurePath.relative_to() raises a ValueError as soon as this path is not a sub-path of other. In order te be able compute relative paths beginning with “../”, we use os.path.relpath() instead.

See pathlib.PurePath.relative_to().

iterdir()

Lists this directory path.

Returns:

Paths iterator.

See pathlib.Path.iterdir().

glob(pattern)

Returns the list of files that match the given pattern.

Parameters:

pattern – Path pattern (see glob.glob()). May be either a relative or an absolute path specification.

Returns:

List of paths that match the pattern.

See pathlib.Path.glob().

rglob(pattern)

See pathlib.Path.rglob().

rename(target)

Moves this file or directory as target.

Parameters:

target – Target path.

Returns:

New target Path instance.

See pathlib.Path.rename().

replace(target)

See pathlib.Path.replace().