2016-01-31 14:44:46 +00: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.
|
|
|
|
#
|
|
|
|
|
|
|
|
##
|
|
|
|
# A file object that provides read access to a part of an existing
|
|
|
|
# file (for example a TAR file).
|
|
|
|
|
|
|
|
|
|
|
|
class ContainerIO(object):
|
|
|
|
|
|
|
|
def __init__(self, file, offset, length):
|
2019-01-20 10:35:31 +00:00
|
|
|
"""
|
|
|
|
Create file object.
|
|
|
|
|
|
|
|
:param file: Existing file.
|
|
|
|
:param offset: Start of region, in bytes.
|
|
|
|
:param length: Size of region, in bytes.
|
|
|
|
"""
|
2016-01-31 14:44:46 +00:00
|
|
|
self.fh = file
|
|
|
|
self.pos = 0
|
|
|
|
self.offset = offset
|
|
|
|
self.length = length
|
|
|
|
self.fh.seek(offset)
|
|
|
|
|
|
|
|
##
|
|
|
|
# Always false.
|
|
|
|
|
|
|
|
def isatty(self):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def seek(self, offset, mode=0):
|
2019-01-20 10:35:31 +00: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.
|
|
|
|
"""
|
2016-01-31 14:44:46 +00: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)
|
|
|
|
|
|
|
|
def tell(self):
|
2019-01-20 10:35:31 +00:00
|
|
|
"""
|
|
|
|
Get current file pointer.
|
2016-01-31 14:44:46 +00:00
|
|
|
|
2019-01-20 10:35:31 +00:00
|
|
|
:returns: Offset from start of region, in bytes.
|
|
|
|
"""
|
|
|
|
return self.pos
|
2016-01-31 14:44:46 +00:00
|
|
|
|
|
|
|
def read(self, n=0):
|
2019-01-20 10:35:31 +00:00
|
|
|
"""
|
|
|
|
Read data.
|
|
|
|
|
|
|
|
:param n: Number of bytes to read. If omitted or zero,
|
|
|
|
read until end of region.
|
|
|
|
:returns: An 8-bit string.
|
|
|
|
"""
|
2016-01-31 14:44:46 +00:00
|
|
|
if n:
|
|
|
|
n = min(n, self.length - self.pos)
|
|
|
|
else:
|
|
|
|
n = self.length - self.pos
|
|
|
|
if not n: # EOF
|
|
|
|
return ""
|
|
|
|
self.pos = self.pos + n
|
|
|
|
return self.fh.read(n)
|
|
|
|
|
|
|
|
def readline(self):
|
2019-01-20 10:35:31 +00:00
|
|
|
"""
|
|
|
|
Read a line of text.
|
|
|
|
|
|
|
|
:returns: An 8-bit string.
|
|
|
|
"""
|
2016-01-31 14:44:46 +00:00
|
|
|
s = ""
|
|
|
|
while True:
|
|
|
|
c = self.read(1)
|
|
|
|
if not c:
|
|
|
|
break
|
|
|
|
s = s + c
|
|
|
|
if c == "\n":
|
|
|
|
break
|
|
|
|
return s
|
|
|
|
|
|
|
|
def readlines(self):
|
2019-01-20 10:35:31 +00:00
|
|
|
"""
|
|
|
|
Read multiple lines of text.
|
|
|
|
|
|
|
|
:returns: A list of 8-bit strings.
|
|
|
|
"""
|
|
|
|
lines = []
|
2016-01-31 14:44:46 +00:00
|
|
|
while True:
|
|
|
|
s = self.readline()
|
|
|
|
if not s:
|
|
|
|
break
|
2019-01-20 10:35:31 +00:00
|
|
|
lines.append(s)
|
|
|
|
return lines
|