From 60e329fdb4ee1debc49ca2eae5e351b6b2004d51 Mon Sep 17 00:00:00 2001 From: Jamie Hardt Date: Thu, 2 Jan 2020 10:50:36 -0800 Subject: [PATCH] Improved exceptions in certain EOF cases Pursuant to #4 --- wavinfo/__init__.py | 3 ++- wavinfo/riff_parser.py | 46 +++++++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/wavinfo/__init__.py b/wavinfo/__init__.py index 61726f2..51fd41f 100644 --- a/wavinfo/__init__.py +++ b/wavinfo/__init__.py @@ -5,7 +5,8 @@ Go to the documentation for wavinfo.WavInfoReader for more information. """ from .wave_reader import WavInfoReader +from .riff_parser import WavInfoEOFError -__version__ = '1.3' +__version__ = '1.3.1' __author__ = 'Jamie Hardt ' __license__ = "MIT" \ No newline at end of file diff --git a/wavinfo/riff_parser.py b/wavinfo/riff_parser.py index 290fa76..21833a7 100644 --- a/wavinfo/riff_parser.py +++ b/wavinfo/riff_parser.py @@ -1,52 +1,60 @@ import struct -import pdb from collections import namedtuple 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: for chunk in self.children: if type(chunk) is ListChunkDescriptor and \ chunk.signature is chunk_path[0]: - return chunk.find(chunk_path[1:]) + return chunk.find(chunk_path[1:]) else: for chunk in self.children: if type(chunk) is ChunkDescriptor and \ chunk.ident is chunk_path[0]: - 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): from_stream.seek(self.start) 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() signature = stream.read(4) - #print("Parsing list chunk with siganture: ", signature) children = [] 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: - children.append(child_chunk) + children.append(child_chunk) else: - break + break return ListChunkDescriptor(signature=signature, children=children) + def parse_chunk(stream, rf64_context=None): ident = stream.read(4) - if len(ident) != 4: - return + size_bytes = stream.read(4) - sizeb = stream.read(4) - size = struct.unpack('