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