PHP 8.2.30
Preview: io.py Size: 2.16 KB
/opt/cloudlinux/venv/lib/python3.11/site-packages/isort/io.py

"""Defines any IO utilities used by isort"""
import dataclasses
import re
import tokenize
from contextlib import contextmanager
from io import BytesIO, StringIO, TextIOWrapper
from pathlib import Path
from typing import Any, Callable, Iterator, TextIO, Union

from isort.exceptions import UnsupportedEncoding

_ENCODING_PATTERN = re.compile(rb"^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)")


@dataclasses.dataclass(frozen=True)
class File:
    stream: TextIO
    path: Path
    encoding: str

    @staticmethod
    def detect_encoding(filename: Union[str, Path], readline: Callable[[], bytes]) -> str:
        try:
            return tokenize.detect_encoding(readline)[0]
        except Exception:
            raise UnsupportedEncoding(filename)

    @staticmethod
    def from_contents(contents: str, filename: str) -> "File":
        encoding = File.detect_encoding(filename, BytesIO(contents.encode("utf-8")).readline)
        return File(stream=StringIO(contents), path=Path(filename).resolve(), encoding=encoding)

    @property
    def extension(self) -> str:
        return self.path.suffix.lstrip(".")

    @staticmethod
    def _open(filename: Union[str, Path]) -> TextIOWrapper:
        """Open a file in read only mode using the encoding detected by
        detect_encoding().
        """
        buffer = open(filename, "rb")
        try:
            encoding = File.detect_encoding(filename, buffer.readline)
            buffer.seek(0)
            text = TextIOWrapper(buffer, encoding, line_buffering=True, newline="")
            text.mode = "r"  # type: ignore
            return text
        except Exception:
            buffer.close()
            raise

    @staticmethod
    @contextmanager
    def read(filename: Union[str, Path]) -> Iterator["File"]:
        file_path = Path(filename).resolve()
        stream = None
        try:
            stream = File._open(file_path)
            yield File(stream=stream, path=file_path, encoding=stream.encoding)
        finally:
            if stream is not None:
                stream.close()


class _EmptyIO(StringIO):
    def write(self, *args: Any, **kwargs: Any) -> None:  # type: ignore # skipcq: PTC-W0049
        pass


Empty = _EmptyIO()

Directory Contents

Dirs: 4 × Files: 28

Name Size Perms Modified Actions
- drwxr-xr-x 2026-02-06 08:01:08
Edit Download
stdlibs DIR
- drwxr-xr-x 2026-02-06 08:01:08
Edit Download
_vendored DIR
- drwxr-xr-x 2026-01-20 13:01:48
Edit Download
- drwxr-xr-x 2026-02-06 08:01:08
Edit Download
25.51 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
933 B lrw-r--r-- 2026-01-20 13:01:48
Edit Download
22.00 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
6.89 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
1.55 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
5.35 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
3.26 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
8.18 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
2.16 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
3.63 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
388 B lrw-r--r-- 2026-01-20 13:01:48
Edit Download
45.73 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
27.15 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
24.74 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
5.05 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
2.09 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
0 B lrw-r--r-- 2026-01-20 13:01:48
Edit Download
1.28 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
297 B lrw-r--r-- 2026-01-20 13:01:48
Edit Download
34.75 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
2.24 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
4.41 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
2.36 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
6.17 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
13.25 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
72 B lrw-r--r-- 2026-01-20 13:01:48
Edit Download
871 B lrw-r--r-- 2026-01-20 13:01:48
Edit Download
36 B lrw-r--r-- 2026-01-20 13:01:48
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).