chunk.py 5.4 KB

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