orchestrata_ultimate.core.isolation

Reference › orchestrata_ultimate › core › isolation

Isolation is a small context manager that gives a caller a private temporary directory. On construction it generates a UUID test_id and creates a directory with the prefix iso_; create_file and create_directory build paths beneath it, and cleanup (called on exit from the with block) removes the whole tree, ignoring errors. It belongs to the test-support side of core and has no imports from the rest of the project.

At a glance

Source src/orchestrata_ultimate/core/isolation.py
Lines of code 40
Classes 1
Functions 0
Imports (internal) none
Imported by none
Tests that import it tests/core/test_isolation.py, tests/generated_core/test_isolation_generated.py

Class diagram

Class diagram orchestrata_ultimate.core.isolation

Dependency diagram

No internal imports and no importers.

API

Classes

Isolation

Line 10

Utility for creating isolated temporary directories and files.

Method Signature Summary
__init__ __init__() -> None
__enter__ __enter__() -> Self
__exit__ __exit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None) -> None
create_file create_file(path: Path, content: str = '') -> Path
create_directory create_directory(path: Path) -> Path
cleanup cleanup() -> None

Source

Permalink: https://gitlab.com/MSamy/orchestrata-ultimate-architecture/-/blob/a4cbc2bdf/src/orchestrata_ultimate/core/isolation.py

# mypy: ignore-errors
import shutil
import tempfile
import uuid
from pathlib import Path
from types import TracebackType
from typing import Self


class Isolation:
    """Utility for creating isolated temporary directories and files."""

    def __init__(self) -> None:
        self.test_id = str(uuid.uuid4())
        self.temp_dir = Path(tempfile.mkdtemp(prefix="iso_"))

    def __enter__(self) -> Self:
        return self

    def __exit__(
        self,
        exc_type: type[BaseException] | None,
        exc_val: BaseException | None,
        exc_tb: TracebackType | None,
    ) -> None:
        self.cleanup()

    def create_file(self, path: Path, content: str = "") -> Path:
        full_path = self.temp_dir / path
        full_path.parent.mkdir(parents=True, exist_ok=True)
        full_path.write_text(content)
        return full_path

    def create_directory(self, path: Path) -> Path:
        full_path = self.temp_dir / path
        full_path.mkdir(parents=True, exist_ok=True)
        return full_path

    def cleanup(self) -> None:
        shutil.rmtree(self.temp_dir, ignore_errors=True)