CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
ContainerIO.py174 linesDownload Raw Back to PIL
1#
2# The Python Imaging Library.
3# $Id$
4#
5# a class to read from a container file
6#
7# History:
8# 1995-06-18 fl     Created
9# 1995-09-07 fl     Added readline(), readlines()
10#
11# Copyright (c) 1997-2001 by Secret Labs AB
12# Copyright (c) 1995 by Fredrik Lundh
13#
14# See the README file for information on usage and redistribution.
15#
16from __future__ import annotations
17
18import io
19from collections.abc import Iterable
20from typing import IO, AnyStr, NoReturn
21
22
23class ContainerIO(IO[AnyStr]):
24    """
25    A file object that provides read access to a part of an existing
26    file (for example a TAR file).
27    """
28
29    def __init__(self, file: IO[AnyStr], offset: int, length: int) -> None:
30        """
31        Create file object.
32
33        :param file: Existing file.
34        :param offset: Start of region, in bytes.
35        :param length: Size of region, in bytes.
36        """
37        self.fh: IO[AnyStr] = file
38        self.pos = 0
39        self.offset = offset
40        self.length = length
41        self.fh.seek(offset)
42
43    ##
44    # Always false.
45
46    def isatty(self) -> bool:
47        return False
48
49    def seekable(self) -> bool:
50        return True
51
52    def seek(self, offset: int, mode: int = io.SEEK_SET) -> int:
53        """
54        Move file pointer.
55
56        :param offset: Offset in bytes.
57        :param mode: Starting position. Use 0 for beginning of region, 1
58           for current offset, and 2 for end of region.  You cannot move
59           the pointer outside the defined region.
60        :returns: Offset from start of region, in bytes.
61        """
62        if mode == 1:
63            self.pos = self.pos + offset
64        elif mode == 2:
65            self.pos = self.length + offset
66        else:
67            self.pos = offset
68        # clamp
69        self.pos = max(0, min(self.pos, self.length))
70        self.fh.seek(self.offset + self.pos)
71        return self.pos
72
73    def tell(self) -> int:
74        """
75        Get current file pointer.
76
77        :returns: Offset from start of region, in bytes.
78        """
79        return self.pos
80
81    def readable(self) -> bool:
82        return True
83
84    def read(self, n: int = -1) -> AnyStr:
85        """
86        Read data.
87
88        :param n: Number of bytes to read. If omitted, zero or negative,
89            read until end of region.
90        :returns: An 8-bit string.
91        """
92        if n > 0:
93            n = min(n, self.length - self.pos)
94        else:
95            n = self.length - self.pos
96        if n <= 0:  # EOF
97            return b"" if "b" in self.fh.mode else ""  # type: ignore[return-value]
98        self.pos = self.pos + n
99        return self.fh.read(n)
100
101    def readline(self, n: int = -1) -> AnyStr:
102        """
103        Read a line of text.
104
105        :param n: Number of bytes to read. If omitted, zero or negative,
106            read until end of line.
107        :returns: An 8-bit string.
108        """
109        s: AnyStr = b"" if "b" in self.fh.mode else ""  # type: ignore[assignment]
110        newline_character = b"\n" if "b" in self.fh.mode else "\n"
111        while True:
112            c = self.read(1)
113            if not c:
114                break
115            s = s + c
116            if c == newline_character or len(s) == n:
117                break
118        return s
119
120    def readlines(self, n: int | None = -1) -> list[AnyStr]:
121        """
122        Read multiple lines of text.
123
124        :param n: Number of lines to read. If omitted, zero, negative or None,
125            read until end of region.
126        :returns: A list of 8-bit strings.
127        """
128        lines = []
129        while True:
130            s = self.readline()
131            if not s:
132                break
133            lines.append(s)
134            if len(lines) == n:
135                break
136        return lines
137
138    def writable(self) -> bool:
139        return False
140
141    def write(self, b: AnyStr) -> NoReturn:
142        raise NotImplementedError()
143
144    def writelines(self, lines: Iterable[AnyStr]) -> NoReturn:
145        raise NotImplementedError()
146
147    def truncate(self, size: int | None = None) -> int:
148        raise NotImplementedError()
149
150    def __enter__(self) -> ContainerIO[AnyStr]:
151        return self
152
153    def __exit__(self, *args: object) -> None:
154        self.close()
155
156    def __iter__(self) -> ContainerIO[AnyStr]:
157        return self
158
159    def __next__(self) -> AnyStr:
160        line = self.readline()
161        if not line:
162            msg = "end of region"
163            raise StopIteration(msg)
164        return line
165
166    def fileno(self) -> int:
167        return self.fh.fileno()
168
169    def flush(self) -> None:
170        self.fh.flush()
171
172    def close(self) -> None:
173        self.fh.close()
174 
Aluode/PerceptionLabPortable · CoolFace