team-10/env/Lib/site-packages/streamlit/temporary_directory.py

66 lines
1.8 KiB
Python
Raw Normal View History

2025-08-02 07:34:44 +02:00
# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2025)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import shutil
import tempfile
from typing import TYPE_CHECKING, Any, cast
from streamlit import util
if TYPE_CHECKING:
from types import TracebackType
# We provide our own context manager for temporary directory that wraps
# tempfile.mkdtemp
class TemporaryDirectory:
"""Temporary directory context manager.
Creates a temporary directory that exists within the context manager scope.
It returns the path to the created directory.
Wrapper on top of tempfile.mkdtemp.
Parameters
----------
suffix : str or None
Suffix to the filename.
prefix : str or None
Prefix to the filename.
dir : str or None
Enclosing directory.
"""
def __init__(self, *args: Any, **kwargs: Any) -> None:
self._args = args
self._kwargs = kwargs
def __repr__(self) -> str:
return util.repr_(self)
def __enter__(self) -> str:
self._path = cast("str", tempfile.mkdtemp(*self._args, **self._kwargs))
return self._path
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
exc_traceback: TracebackType | None,
) -> None:
shutil.rmtree(self._path)