| 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.""" |