monkey_wrench.input_output._types module

monkey_wrench.input_output._types.ensure_path_does_not_end_with_slash(path: Path) Path[source]

Check that the path does not end with a slash, and therefore, it does not point to a directory.

monkey_wrench.input_output._types.AbsolutePath

Type annotation and Pydantic validator to represent (convert to) an absolute and normalized path.

alias of Annotated[PathType, AfterValidator(func=~monkey_wrench.input_output._types.)]

monkey_wrench.input_output._types.ExistingFilePath

Type annotation and Pydantic validator for an existing file path.

Note

After the validation, the path will be normalized and made into an absolute path.

Warning

The path must not end with / or \.

alias of Annotated[Path, PathType(path_type=file), AfterValidator(func=~monkey_wrench.input_output._types.), AfterValidator(func=ensure_path_does_not_end_with_slash)]

monkey_wrench.input_output._types.NewFilePath

Type annotation and Pydantic validator for a non-existing file path.

Note

After the validation, the path will be normalized and made into an absolute path.

Warning

The path must not end with / or \.

alias of Annotated[Path, PathType(path_type=new), AfterValidator(func=~monkey_wrench.input_output._types.), AfterValidator(func=ensure_path_does_not_end_with_slash)]

monkey_wrench.input_output._types.ExistingDirectoryPath

Type annotation and Pydantic validator for an existing directory path.

Note

After the validation, the path will be normalized and made into an absolute path.

Note

The path can optionally end with / or \.

alias of Annotated[Path, PathType(path_type=dir), AfterValidator(func=~monkey_wrench.input_output._types.)]

monkey_wrench.input_output._types.NewDirectoryPath

Type annotation and Pydantic validator for a non-existing directory path.

Note

After the validation, the path will be normalized and made into an absolute path.

Note

The path can optionally end with / or \.

alias of Annotated[Path, PathType(path_type=new), AfterValidator(func=~monkey_wrench.input_output._types.)]

monkey_wrench.input_output._types.OpenMode

Type alias for the union of a literal "a" (for appending to), or "w" (for overwriting an existing file).

This only concerns ASCII (text) files.

alias of Literal[‘w’, ‘a’]

class monkey_wrench.input_output._types.TempDirectory(*, temp_directory_path: ~typing.Annotated[~pathlib.Path, ~pydantic.types.PathType(path_type=dir), ~pydantic.functional_validators.AfterValidator(func=~monkey_wrench.input_output._types.<lambda>)])[source]

Bases: Model

Pydantic model for a temporary directory, including a context manager.

temp_directory_path: <lambda>)]

The path to an existing directory, which will be used as the top-level temporary directory.

Note

This directory will be used as a parent directory for subsequent (child) temporary directories. As a result, it will not be removed or cleaned up. However, the child temporary directories will always be removed and cleaned up.

Note

If it is not set (i.e. it is None), it takes on a value according to the following order of priority:

1- The value of the TMPDIR environment variable.

2- /tmp/.

classmethod validate_temporary_directory(data: Any) Any[source]

Return the path to the top-level temporary directory according to the priority rules.

temp_dir_context_manager() Generator[Path, None, None][source]

Context manager to create a temporary directory and also set the global temporary directory to the same path.

Note

The temporary directory created by this context manager will reside inside TempDirectory.temp_directory_path.

Note

The reason to set the global temporary directory is to ensure that any other inner functions or context managers that might invoke tempfile.TemporaryDirectory() also use the given global temporary directory.

Yields:

The full path of the (created) temporary directory.