Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2....
File: chunk.py
"""Simple class to read IFF chunks.
[0] Fix | Delete
[1] Fix | Delete
An IFF chunk (used in formats such as AIFF, TIFF, RMFF (RealMedia File
[2] Fix | Delete
Format)) has the following structure:
[3] Fix | Delete
[4] Fix | Delete
+----------------+
[5] Fix | Delete
| ID (4 bytes) |
[6] Fix | Delete
+----------------+
[7] Fix | Delete
| size (4 bytes) |
[8] Fix | Delete
+----------------+
[9] Fix | Delete
| data |
[10] Fix | Delete
| ... |
[11] Fix | Delete
+----------------+
[12] Fix | Delete
[13] Fix | Delete
The ID is a 4-byte string which identifies the type of chunk.
[14] Fix | Delete
[15] Fix | Delete
The size field (a 32-bit value, encoded using big-endian byte order)
[16] Fix | Delete
gives the size of the whole chunk, including the 8-byte header.
[17] Fix | Delete
[18] Fix | Delete
Usually an IFF-type file consists of one or more chunks. The proposed
[19] Fix | Delete
usage of the Chunk class defined here is to instantiate an instance at
[20] Fix | Delete
the start of each chunk and read from the instance until it reaches
[21] Fix | Delete
the end, after which a new instance can be instantiated. At the end
[22] Fix | Delete
of the file, creating a new instance will fail with an EOFError
[23] Fix | Delete
exception.
[24] Fix | Delete
[25] Fix | Delete
Usage:
[26] Fix | Delete
while True:
[27] Fix | Delete
try:
[28] Fix | Delete
chunk = Chunk(file)
[29] Fix | Delete
except EOFError:
[30] Fix | Delete
break
[31] Fix | Delete
chunktype = chunk.getname()
[32] Fix | Delete
while True:
[33] Fix | Delete
data = chunk.read(nbytes)
[34] Fix | Delete
if not data:
[35] Fix | Delete
pass
[36] Fix | Delete
# do something with data
[37] Fix | Delete
[38] Fix | Delete
The interface is file-like. The implemented methods are:
[39] Fix | Delete
read, close, seek, tell, isatty.
[40] Fix | Delete
Extra methods are: skip() (called by close, skips to the end of the chunk),
[41] Fix | Delete
getname() (returns the name (ID) of the chunk)
[42] Fix | Delete
[43] Fix | Delete
The __init__ method has one required argument, a file-like object
[44] Fix | Delete
(including a chunk instance), and one optional argument, a flag which
[45] Fix | Delete
specifies whether or not chunks are aligned on 2-byte boundaries. The
[46] Fix | Delete
default is 1, i.e. aligned.
[47] Fix | Delete
"""
[48] Fix | Delete
[49] Fix | Delete
class Chunk:
[50] Fix | Delete
def __init__(self, file, align=True, bigendian=True, inclheader=False):
[51] Fix | Delete
import struct
[52] Fix | Delete
self.closed = False
[53] Fix | Delete
self.align = align # whether to align to word (2-byte) boundaries
[54] Fix | Delete
if bigendian:
[55] Fix | Delete
strflag = '>'
[56] Fix | Delete
else:
[57] Fix | Delete
strflag = '<'
[58] Fix | Delete
self.file = file
[59] Fix | Delete
self.chunkname = file.read(4)
[60] Fix | Delete
if len(self.chunkname) < 4:
[61] Fix | Delete
raise EOFError
[62] Fix | Delete
try:
[63] Fix | Delete
self.chunksize = struct.unpack(strflag+'L', file.read(4))[0]
[64] Fix | Delete
except struct.error:
[65] Fix | Delete
raise EOFError
[66] Fix | Delete
if inclheader:
[67] Fix | Delete
self.chunksize = self.chunksize - 8 # subtract header
[68] Fix | Delete
self.size_read = 0
[69] Fix | Delete
try:
[70] Fix | Delete
self.offset = self.file.tell()
[71] Fix | Delete
except (AttributeError, IOError):
[72] Fix | Delete
self.seekable = False
[73] Fix | Delete
else:
[74] Fix | Delete
self.seekable = True
[75] Fix | Delete
[76] Fix | Delete
def getname(self):
[77] Fix | Delete
"""Return the name (ID) of the current chunk."""
[78] Fix | Delete
return self.chunkname
[79] Fix | Delete
[80] Fix | Delete
def getsize(self):
[81] Fix | Delete
"""Return the size of the current chunk."""
[82] Fix | Delete
return self.chunksize
[83] Fix | Delete
[84] Fix | Delete
def close(self):
[85] Fix | Delete
if not self.closed:
[86] Fix | Delete
try:
[87] Fix | Delete
self.skip()
[88] Fix | Delete
finally:
[89] Fix | Delete
self.closed = True
[90] Fix | Delete
[91] Fix | Delete
def isatty(self):
[92] Fix | Delete
if self.closed:
[93] Fix | Delete
raise ValueError, "I/O operation on closed file"
[94] Fix | Delete
return False
[95] Fix | Delete
[96] Fix | Delete
def seek(self, pos, whence=0):
[97] Fix | Delete
"""Seek to specified position into the chunk.
[98] Fix | Delete
Default position is 0 (start of chunk).
[99] Fix | Delete
If the file is not seekable, this will result in an error.
[100] Fix | Delete
"""
[101] Fix | Delete
[102] Fix | Delete
if self.closed:
[103] Fix | Delete
raise ValueError, "I/O operation on closed file"
[104] Fix | Delete
if not self.seekable:
[105] Fix | Delete
raise IOError, "cannot seek"
[106] Fix | Delete
if whence == 1:
[107] Fix | Delete
pos = pos + self.size_read
[108] Fix | Delete
elif whence == 2:
[109] Fix | Delete
pos = pos + self.chunksize
[110] Fix | Delete
if pos < 0 or pos > self.chunksize:
[111] Fix | Delete
raise RuntimeError
[112] Fix | Delete
self.file.seek(self.offset + pos, 0)
[113] Fix | Delete
self.size_read = pos
[114] Fix | Delete
[115] Fix | Delete
def tell(self):
[116] Fix | Delete
if self.closed:
[117] Fix | Delete
raise ValueError, "I/O operation on closed file"
[118] Fix | Delete
return self.size_read
[119] Fix | Delete
[120] Fix | Delete
def read(self, size=-1):
[121] Fix | Delete
"""Read at most size bytes from the chunk.
[122] Fix | Delete
If size is omitted or negative, read until the end
[123] Fix | Delete
of the chunk.
[124] Fix | Delete
"""
[125] Fix | Delete
[126] Fix | Delete
if self.closed:
[127] Fix | Delete
raise ValueError, "I/O operation on closed file"
[128] Fix | Delete
if self.size_read >= self.chunksize:
[129] Fix | Delete
return ''
[130] Fix | Delete
if size < 0:
[131] Fix | Delete
size = self.chunksize - self.size_read
[132] Fix | Delete
if size > self.chunksize - self.size_read:
[133] Fix | Delete
size = self.chunksize - self.size_read
[134] Fix | Delete
data = self.file.read(size)
[135] Fix | Delete
self.size_read = self.size_read + len(data)
[136] Fix | Delete
if self.size_read == self.chunksize and \
[137] Fix | Delete
self.align and \
[138] Fix | Delete
(self.chunksize & 1):
[139] Fix | Delete
dummy = self.file.read(1)
[140] Fix | Delete
self.size_read = self.size_read + len(dummy)
[141] Fix | Delete
return data
[142] Fix | Delete
[143] Fix | Delete
def skip(self):
[144] Fix | Delete
"""Skip the rest of the chunk.
[145] Fix | Delete
If you are not interested in the contents of the chunk,
[146] Fix | Delete
this method should be called so that the file points to
[147] Fix | Delete
the start of the next chunk.
[148] Fix | Delete
"""
[149] Fix | Delete
[150] Fix | Delete
if self.closed:
[151] Fix | Delete
raise ValueError, "I/O operation on closed file"
[152] Fix | Delete
if self.seekable:
[153] Fix | Delete
try:
[154] Fix | Delete
n = self.chunksize - self.size_read
[155] Fix | Delete
# maybe fix alignment
[156] Fix | Delete
if self.align and (self.chunksize & 1):
[157] Fix | Delete
n = n + 1
[158] Fix | Delete
self.file.seek(n, 1)
[159] Fix | Delete
self.size_read = self.size_read + n
[160] Fix | Delete
return
[161] Fix | Delete
except IOError:
[162] Fix | Delete
pass
[163] Fix | Delete
while self.size_read < self.chunksize:
[164] Fix | Delete
n = min(8192, self.chunksize - self.size_read)
[165] Fix | Delete
dummy = self.read(n)
[166] Fix | Delete
if not dummy:
[167] Fix | Delete
raise EOFError
[168] Fix | Delete
[169] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function