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