| 53 | warnings._deprecated(__name__, remove=(3, 13)) |
| 54 | |
| 55 | class Chunk: |
| 56 | def __init__(self, file, align=True, bigendian=True, inclheader=False): |
| 57 | import struct |
| 58 | self.closed = False |
| 59 | self.align = align # whether to align to word (2-byte) boundaries |
| 60 | if bigendian: |
| 61 | strflag = '>' |
| 62 | else: |
| 63 | strflag = '<' |
| 64 | self.file = file |
| 65 | self.chunkname = file.read(4) |
| 66 | if len(self.chunkname) < 4: |
| 67 | raise EOFError |
| 68 | try: |
| 69 | self.chunksize = struct.unpack_from(strflag+'L', file.read(4))[0] |
| 70 | except struct.error: |
| 71 | raise EOFError from None |
| 72 | if inclheader: |
| 73 | self.chunksize = self.chunksize - 8 # subtract header |
| 74 | self.size_read = 0 |
| 75 | try: |
| 76 | self.offset = self.file.tell() |
| 77 | except (AttributeError, OSError): |
| 78 | self.seekable = False |
| 79 | else: |
| 80 | self.seekable = True |
| 81 | |
| 82 | def getname(self): |
| 83 | """Return the name (ID) of the current chunk.""" |
| 84 | return self.chunkname |
| 85 | |
| 86 | def getsize(self): |
| 87 | """Return the size of the current chunk.""" |
| 88 | return self.chunksize |
| 89 | |
| 90 | def close(self): |
| 91 | if not self.closed: |
| 92 | try: |
| 93 | self.skip() |
| 94 | finally: |
| 95 | self.closed = True |
| 96 | |
| 97 | def isatty(self): |
| 98 | if self.closed: |
| 99 | raise ValueError("I/O operation on closed file") |
| 100 | return False |
| 101 | |
| 102 | def seek(self, pos, whence=0): |
| 103 | """Seek to specified position into the chunk. |
| 104 | Default position is 0 (start of chunk). |
| 105 | If the file is not seekable, this will result in an error. |
| 106 | """ |
| 107 | |
| 108 | if self.closed: |
| 109 | raise ValueError("I/O operation on closed file") |
| 110 | if not self.seekable: |
| 111 | raise OSError("cannot seek") |
| 112 | if whence == 1: |