Pillow/src/PIL/ContainerIO.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

122 lines
3.1 KiB
Python
Raw Normal View History

2010-07-31 06:52:47 +04:00
#
# The Python Imaging Library.
# $Id$
#
# a class to read from a container file
#
# History:
# 1995-06-18 fl Created
# 1995-09-07 fl Added readline(), readlines()
#
# Copyright (c) 1997-2001 by Secret Labs AB
# Copyright (c) 1995 by Fredrik Lundh
#
# See the README file for information on usage and redistribution.
#
from __future__ import annotations
2010-07-31 06:52:47 +04:00
import io
2023-12-30 01:12:30 +03:00
from typing import IO, AnyStr, Generic, Literal
2014-08-26 17:47:10 +04:00
2023-12-30 01:12:30 +03:00
class ContainerIO(Generic[AnyStr]):
"""
A file object that provides read access to a part of an existing
file (for example a TAR file).
"""
2023-12-30 01:12:30 +03:00
def __init__(self, file: IO[AnyStr], offset: int, length: int) -> None:
2016-09-24 12:10:46 +03:00
"""
Create file object.
:param file: Existing file.
:param offset: Start of region, in bytes.
:param length: Size of region, in bytes.
"""
2023-12-30 01:12:30 +03:00
self.fh: IO[AnyStr] = file
2010-07-31 06:52:47 +04:00
self.pos = 0
self.offset = offset
self.length = length
self.fh.seek(offset)
##
# Always false.
2023-12-30 01:12:30 +03:00
def isatty(self) -> bool:
return False
2010-07-31 06:52:47 +04:00
2023-12-30 01:12:30 +03:00
def seek(self, offset: int, mode: Literal[0, 1, 2] = io.SEEK_SET) -> None:
2016-09-24 12:10:46 +03:00
"""
Move file pointer.
:param offset: Offset in bytes.
:param mode: Starting position. Use 0 for beginning of region, 1
for current offset, and 2 for end of region. You cannot move
the pointer outside the defined region.
"""
2010-07-31 06:52:47 +04:00
if mode == 1:
self.pos = self.pos + offset
elif mode == 2:
self.pos = self.length + offset
else:
self.pos = offset
# clamp
self.pos = max(0, min(self.pos, self.length))
self.fh.seek(self.offset + self.pos)
2023-12-30 01:12:30 +03:00
def tell(self) -> int:
2016-09-24 12:10:46 +03:00
"""
Get current file pointer.
2010-07-31 06:52:47 +04:00
2016-09-24 12:10:46 +03:00
:returns: Offset from start of region, in bytes.
"""
return self.pos
2010-07-31 06:52:47 +04:00
2023-12-30 01:12:30 +03:00
def read(self, n: int = 0) -> AnyStr:
2016-09-24 12:10:46 +03:00
"""
Read data.
2017-09-20 13:49:14 +03:00
:param n: Number of bytes to read. If omitted or zero,
2016-09-24 12:10:46 +03:00
read until end of region.
:returns: An 8-bit string.
"""
2010-07-31 06:52:47 +04:00
if n:
n = min(n, self.length - self.pos)
else:
n = self.length - self.pos
if not n: # EOF
2023-12-30 01:12:30 +03:00
return b"" if "b" in self.fh.mode else "" # type: ignore[return-value]
2010-07-31 06:52:47 +04:00
self.pos = self.pos + n
return self.fh.read(n)
2023-12-30 01:12:30 +03:00
def readline(self) -> AnyStr:
2016-09-24 12:10:46 +03:00
"""
Read a line of text.
:returns: An 8-bit string.
"""
2023-12-30 01:12:30 +03:00
s: AnyStr = b"" if "b" in self.fh.mode else "" # type: ignore[assignment]
newline_character = b"\n" if "b" in self.fh.mode else "\n"
while True:
2010-07-31 06:52:47 +04:00
c = self.read(1)
if not c:
break
s = s + c
if c == newline_character:
2010-07-31 06:52:47 +04:00
break
return s
2023-12-30 01:12:30 +03:00
def readlines(self) -> list[AnyStr]:
2016-09-24 12:10:46 +03:00
"""
Read multiple lines of text.
:returns: A list of 8-bit strings.
"""
2018-06-15 16:44:22 +03:00
lines = []
while True:
2010-07-31 06:52:47 +04:00
s = self.readline()
if not s:
break
2018-06-15 16:44:22 +03:00
lines.append(s)
return lines