Improved exceptions in certain EOF cases

Pursuant to #4
This commit is contained in:
Jamie Hardt
2020-01-02 10:50:36 -08:00
parent 15db4c9ffa
commit 60e329fdb4
2 changed files with 27 additions and 22 deletions

View File

@@ -5,7 +5,8 @@ Go to the documentation for wavinfo.WavInfoReader for more information.
""" """
from .wave_reader import WavInfoReader from .wave_reader import WavInfoReader
from .riff_parser import WavInfoEOFError
__version__ = '1.3' __version__ = '1.3.1'
__author__ = 'Jamie Hardt <jamiehardt@gmail.com>' __author__ = 'Jamie Hardt <jamiehardt@gmail.com>'
__license__ = "MIT" __license__ = "MIT"

View File

@@ -1,12 +1,17 @@
import struct import struct
import pdb
from collections import namedtuple from collections import namedtuple
from .rf64_parser import parse_rf64 from .rf64_parser import parse_rf64
class ListChunkDescriptor(namedtuple('ListChunkDescriptor' , 'signature children')):
def find(chunk_path): class WavInfoEOFError(EOFError):
def __init__(self, identifier, chunk_start):
self.identifier = identifier
self.chunk_start = chunk_start
class ListChunkDescriptor(namedtuple('ListChunkDescriptor', 'signature children')):
def find(self, chunk_path):
if len(chunk_path) > 1: if len(chunk_path) > 1:
for chunk in self.children: for chunk in self.children:
if type(chunk) is ListChunkDescriptor and \ if type(chunk) is ListChunkDescriptor and \
@@ -19,20 +24,20 @@ class ListChunkDescriptor(namedtuple('ListChunkDescriptor' , 'signature children
return chunk return chunk
class ChunkDescriptor(namedtuple('ChunkDescriptor', 'ident start length rf64_context') ): class ChunkDescriptor(namedtuple('ChunkDescriptor', 'ident start length rf64_context')):
def read_data(self, from_stream): def read_data(self, from_stream):
from_stream.seek(self.start) from_stream.seek(self.start)
return from_stream.read(self.length) return from_stream.read(self.length)
def parse_list_chunk(stream, length, rf64_context =None):
def parse_list_chunk(stream, length, rf64_context=None):
start = stream.tell() start = stream.tell()
signature = stream.read(4) signature = stream.read(4)
#print("Parsing list chunk with siganture: ", signature)
children = [] children = []
while (stream.tell() - start) < length: while (stream.tell() - start) < length:
child_chunk = parse_chunk(stream, rf64_context= rf64_context) child_chunk = parse_chunk(stream, rf64_context=rf64_context)
if child_chunk: if child_chunk:
children.append(child_chunk) children.append(child_chunk)
else: else:
@@ -40,13 +45,16 @@ def parse_list_chunk(stream, length, rf64_context =None):
return ListChunkDescriptor(signature=signature, children=children) return ListChunkDescriptor(signature=signature, children=children)
def parse_chunk(stream, rf64_context=None): def parse_chunk(stream, rf64_context=None):
ident = stream.read(4) ident = stream.read(4)
if len(ident) != 4: size_bytes = stream.read(4)
return
sizeb = stream.read(4) if len(ident) != 4 or len(size_bytes) < 4:
size = struct.unpack('<I',sizeb)[0] raise WavInfoEOFError(identifier=ident, chunk_start=stream.tell() - 4)
size_bytes = stream.read(4)
size = struct.unpack('<I', size_bytes)[0]
if size == 0xFFFFFFFF: if size == 0xFFFFFFFF:
if rf64_context is None and ident == b'RF64': if rf64_context is None and ident == b'RF64':
@@ -58,13 +66,9 @@ def parse_chunk(stream, rf64_context=None):
if displacement % 2 is not 0: if displacement % 2 is not 0:
displacement = displacement + 1 displacement = displacement + 1
if ident in [b'RIFF',b'LIST', b'RF64']: if ident in [b'RIFF', b'LIST', b'RF64']:
#print("Parsing list chunk with ident: ", ident)
return parse_list_chunk(stream=stream, length=size, rf64_context=rf64_context) return parse_list_chunk(stream=stream, length=size, rf64_context=rf64_context)
else: else:
start = stream.tell() start = stream.tell()
stream.seek(displacement,1) stream.seek(displacement, 1)
#print("Parsing chunk with start=%i, ident=%s" % (start, ident))
return ChunkDescriptor(ident=ident, start=start, length=size, rf64_context=rf64_context) return ChunkDescriptor(ident=ident, start=start, length=size, rf64_context=rf64_context)