Skip to content

TempContainer

TempContainer

Bases: Container

A Container that uses a temporary directory as its root. The directory is automatically cleaned up when the context manager exits.

Source code in gdutils/datacontainer/temp.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class TempContainer(Container):
    """
    A Container that uses a temporary directory as its root.
    The directory is automatically cleaned up when the context manager exits.
    """

    def __init__(
        self,
        clean: bool = False,
        infos_name: str = "tree.json",
        auto_register: bool = True,
        **kwargs: Any,
    ) -> None:
        """
        Initialize a TempContainer.

        Parameters:
            clean: Passed to Container (default False).
            infos_name: Registry filename (default "tree.json").
            auto_register: Whether to auto-register files (default True).
            **kwargs: Additional arguments passed to TemporaryDirectory
                      (suffix, prefix, dir, ignore_cleanup_errors).
        """
        # Extract TemporaryDirectory args
        temp_kwargs = {
            k: v
            for k, v in kwargs.items()
            if k in ["suffix", "prefix", "dir", "ignore_cleanup_errors"]
        }
        self._temp_dir = TemporaryDirectory(**temp_kwargs)

        super().__init__(
            root=self._temp_dir.name,
            clean=clean,
            infos_name=infos_name,
            auto_register=auto_register,
        )

    def __exit__(self, exc_type: Any, exc: Any, tb: Any) -> None:
        """
        Exit the context manager.

        Saves the container registry (via super), then cleans up the temporary directory.
        """
        try:
            super().__exit__(exc_type, exc, tb)
        finally:
            self._temp_dir.cleanup()

__exit__(exc_type, exc, tb)

Exit the context manager.

Saves the container registry (via super), then cleans up the temporary directory.

Source code in gdutils/datacontainer/temp.py
47
48
49
50
51
52
53
54
55
56
def __exit__(self, exc_type: Any, exc: Any, tb: Any) -> None:
    """
    Exit the context manager.

    Saves the container registry (via super), then cleans up the temporary directory.
    """
    try:
        super().__exit__(exc_type, exc, tb)
    finally:
        self._temp_dir.cleanup()

__init__(clean=False, infos_name='tree.json', auto_register=True, **kwargs)

Initialize a TempContainer.

Parameters:

Name Type Description Default
clean bool

Passed to Container (default False).

False
infos_name str

Registry filename (default "tree.json").

'tree.json'
auto_register bool

Whether to auto-register files (default True).

True
**kwargs Any

Additional arguments passed to TemporaryDirectory (suffix, prefix, dir, ignore_cleanup_errors).

{}
Source code in gdutils/datacontainer/temp.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def __init__(
    self,
    clean: bool = False,
    infos_name: str = "tree.json",
    auto_register: bool = True,
    **kwargs: Any,
) -> None:
    """
    Initialize a TempContainer.

    Parameters:
        clean: Passed to Container (default False).
        infos_name: Registry filename (default "tree.json").
        auto_register: Whether to auto-register files (default True).
        **kwargs: Additional arguments passed to TemporaryDirectory
                  (suffix, prefix, dir, ignore_cleanup_errors).
    """
    # Extract TemporaryDirectory args
    temp_kwargs = {
        k: v
        for k, v in kwargs.items()
        if k in ["suffix", "prefix", "dir", "ignore_cleanup_errors"]
    }
    self._temp_dir = TemporaryDirectory(**temp_kwargs)

    super().__init__(
        root=self._temp_dir.name,
        clean=clean,
        infos_name=infos_name,
        auto_register=auto_register,
    )