- ..
- __init__.py
- __main__.py
- _binary.py
- _deprecate.py
- _imaging.cpython-310-x86_64-linux-gnu.so
- _imagingcms.cpython-310-x86_64-linux-gnu.so
- _imagingft.cpython-310-x86_64-linux-gnu.so
- _imagingmath.cpython-310-x86_64-linux-gnu.so
- _imagingmorph.cpython-310-x86_64-linux-gnu.so
- _imagingtk.cpython-310-x86_64-linux-gnu.so
- _tkinter_finder.py
- _util.py
- _version.py
- _webp.cpython-310-x86_64-linux-gnu.so
- BdfFontFile.py
- BlpImagePlugin.py
- BmpImagePlugin.py
- BufrStubImagePlugin.py
- ContainerIO.py
- CurImagePlugin.py
- DcxImagePlugin.py
- DdsImagePlugin.py
- EpsImagePlugin.py
- ExifTags.py
- features.py
- FitsImagePlugin.py
- FliImagePlugin.py
- FontFile.py
- FpxImagePlugin.py
- FtexImagePlugin.py
- GbrImagePlugin.py
- GdImageFile.py
- GifImagePlugin.py
- GimpGradientFile.py
- GimpPaletteFile.py
- GribStubImagePlugin.py
- Hdf5StubImagePlugin.py
- IcnsImagePlugin.py
- IcoImagePlugin.py
- Image.py
- ImageChops.py
- ImageCms.py
- ImageColor.py
- ImageDraw.py
- ImageDraw2.py
- ImageEnhance.py
- ImageFile.py
- ImageFilter.py
- ImageFont.py
- ImageGrab.py
- ImageMath.py
- ImageMode.py
- ImageMorph.py
- ImageOps.py
- ImagePalette.py
- ImagePath.py
- ImageQt.py
- ImageSequence.py
- ImageShow.py
- ImageStat.py
- ImageTk.py
- ImageTransform.py
- ImageWin.py
- ImImagePlugin.py
- ImtImagePlugin.py
- IptcImagePlugin.py
- Jpeg2KImagePlugin.py
- JpegImagePlugin.py
- JpegPresets.py
- McIdasImagePlugin.py
- MicImagePlugin.py
- MpegImagePlugin.py
- MpoImagePlugin.py
- MspImagePlugin.py
- PaletteFile.py
- PalmImagePlugin.py
- PcdImagePlugin.py
- PcfFontFile.py
- PcxImagePlugin.py
- PdfImagePlugin.py
- PdfParser.py
- PixarImagePlugin.py
- PngImagePlugin.py
- PpmImagePlugin.py
- PsdImagePlugin.py
- PSDraw.py
- PyAccess.py
- QoiImagePlugin.py
- SgiImagePlugin.py
- SpiderImagePlugin.py
- SunImagePlugin.py
- TarIO.py
- TgaImagePlugin.py
- TiffImagePlugin.py
- TiffTags.py
- WalImageFile.py
- WebPImagePlugin.py
- WmfImagePlugin.py
- XbmImagePlugin.py
- XpmImagePlugin.py
- XVThumbImagePlugin.py
ContainerIO.py @a8e0244 — raw · history · blame
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #
# 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.
#
import io
class ContainerIO:
"""
A file object that provides read access to a part of an existing
file (for example a TAR file).
"""
def __init__(self, file, offset, length):
"""
Create file object.
:param file: Existing file.
:param offset: Start of region, in bytes.
:param length: Size of region, in bytes.
"""
self.fh = file
self.pos = 0
self.offset = offset
self.length = length
self.fh.seek(offset)
##
# Always false.
def isatty(self):
return False
def seek(self, offset, mode=io.SEEK_SET):
"""
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.
"""
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)
def tell(self):
"""
Get current file pointer.
:returns: Offset from start of region, in bytes.
"""
return self.pos
def read(self, n=0):
"""
Read data.
:param n: Number of bytes to read. If omitted or zero,
read until end of region.
:returns: An 8-bit string.
"""
if n:
n = min(n, self.length - self.pos)
else:
n = self.length - self.pos
if not n: # EOF
return b"" if "b" in self.fh.mode else ""
self.pos = self.pos + n
return self.fh.read(n)
def readline(self):
"""
Read a line of text.
:returns: An 8-bit string.
"""
s = b"" if "b" in self.fh.mode else ""
newline_character = b"\n" if "b" in self.fh.mode else "\n"
while True:
c = self.read(1)
if not c:
break
s = s + c
if c == newline_character:
break
return s
def readlines(self):
"""
Read multiple lines of text.
:returns: A list of 8-bit strings.
"""
lines = []
while True:
s = self.readline()
if not s:
break
lines.append(s)
return lines
|