Skip to content

Module isort.io

Defines any IO utilities used by isort

View Source
"""Defines any IO utilities used by isort"""

import re

import tokenize

from contextlib import contextmanager

from io import BytesIO, StringIO, TextIOWrapper

from pathlib import Path

from typing import Iterator, NamedTuple, TextIO, Union

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

class File(NamedTuple):

    stream: TextIO

    path: Path

    encoding: str

    @staticmethod

    def from_contents(contents: str, filename: str) -> "File":

        encoding, _ = tokenize.detect_encoding(BytesIO(contents.encode("utf-8")).readline)

        return File(StringIO(contents), path=Path(filename).resolve(), encoding=encoding)

    @property

    def extension(self):

        return self.path.suffix.lstrip(".")

    @staticmethod

    def _open(filename):

        """Open a file in read only mode using the encoding detected by

        detect_encoding().

        """

        buffer = open(filename, "rb")

        try:

            encoding, _ = tokenize.detect_encoding(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, **kwargs):

        pass

Empty = _EmptyIO()

Variables

Empty

Classes

File

class File(
    /,
    *args,
    **kwargs
)

File(stream, path, encoding)

View Source
class File(NamedTuple):

    stream: TextIO

    path: Path

    encoding: str

    @staticmethod

    def from_contents(contents: str, filename: str) -> "File":

        encoding, _ = tokenize.detect_encoding(BytesIO(contents.encode("utf-8")).readline)

        return File(StringIO(contents), path=Path(filename).resolve(), encoding=encoding)

    @property

    def extension(self):

        return self.path.suffix.lstrip(".")

    @staticmethod

    def _open(filename):

        """Open a file in read only mode using the encoding detected by

        detect_encoding().

        """

        buffer = open(filename, "rb")

        try:

            encoding, _ = tokenize.detect_encoding(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()

Ancestors (in MRO)

  • builtins.tuple

Class variables

encoding
path
stream

Static methods

from_contents
def from_contents(
    contents: str,
    filename: str
) -> 'File'
View Source
    @staticmethod

    def from_contents(contents: str, filename: str) -> "File":

        encoding, _ = tokenize.detect_encoding(BytesIO(contents.encode("utf-8")).readline)

        return File(StringIO(contents), path=Path(filename).resolve(), encoding=encoding)
read
def read(
    filename: Union[str, pathlib.Path]
) -> Iterator[ForwardRef('File')]
View Source
    @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()

Instance variables

extension

Methods

count
def count(
    self,
    value,
    /
)

Return number of occurrences of value.

index
def index(
    self,
    value,
    start=0,
    stop=9223372036854775807,
    /
)

Return first index of value.

Raises ValueError if the value is not present.