Pull null-buffer check out into its own function: binarywave.assert_null.
authorW. Trevor King <wking@tremily.us>
Mon, 16 Jul 2012 19:51:14 +0000 (15:51 -0400)
committerW. Trevor King <wking@tremily.us>
Mon, 16 Jul 2012 19:51:14 +0000 (15:51 -0400)
igor/binarywave.py

index 09d06dc2a0ce251f7e0def544a94e498b9581615..3429ad18a9bd655c11c62535c8e7fc6b5d3b4144 100644 (file)
@@ -423,6 +423,52 @@ def checksum(buffer, byte_order, oldcksum, numbytes):
             oldcksum -= 2**31
     return oldcksum & 0xffff
 
+def hex_bytes(buffer, spaces=None):
+    r"""Pretty-printing for binary buffers.
+
+    >>> hex_bytes(buffer('\x00\x01\x02\x03\x04'))
+    '0001020304'
+    >>> hex_bytes(buffer('\x00\x01\x02\x03\x04'), spaces=1)
+    '00 01 02 03 04'
+    >>> hex_bytes(buffer('\x00\x01\x02\x03\x04'), spaces=2)
+    '0001 0203 04'
+    >>> hex_bytes(buffer('\x00\x01\x02\x03\x04\x05\x06'), spaces=2)
+    '0001 0203 0405 06'
+    >>> hex_bytes(buffer('\x00\x01\x02\x03\x04\x05\x06'), spaces=3)
+    '000102 030405 06'
+    """
+    hex_bytes = ['{:02x}'.format(ord(x)) for x in buffer]
+    if spaces is None:
+        return ''.join(hex_bytes)
+    elif spaces is 1:
+        return ' '.join(hex_bytes)
+    for i in range(len(hex_bytes)//spaces):
+        hex_bytes.insert((spaces+1)*(i+1)-1, ' ')
+    return ''.join(hex_bytes)
+
+def assert_null(buffer, strict=True):
+    r"""Ensure an input buffer is entirely zero.
+
+    >>> assert_null(buffer(''))
+    >>> assert_null(buffer('\x00\x00'))
+    >>> assert_null(buffer('\x00\x01\x02\x03'))
+    Traceback (most recent call last):
+      ...
+    ValueError: 00 01 02 03
+    >>> stderr = sys.stderr
+    >>> sys.stderr = sys.stdout
+    >>> assert_null(buffer('\x00\x01\x02\x03'), strict=False)
+    warning: post-data padding not zero: 00 01 02 03
+    >>> sys.stderr = stderr
+    """
+    if buffer and ord(max(buffer)) != 0:
+        hex_string = hex_bytes(buffer, spaces=1)
+        if strict:
+            raise ValueError(hex_string)
+        else:
+            sys.stderr.write(
+                'warning: post-data padding not zero: {}\n'.format(hex_string))
+
 # Translated from ReadWave()
 def loadibw(filename, strict=True):
     if hasattr(filename, 'read'):
@@ -486,13 +532,7 @@ def loadibw(filename, strict=True):
             #   * 16 bytes of padding
             #   * Optional wave note data
             pad_b = buffer(f.read(16))  # skip the padding
-            if max(pad_b) != 0:
-                if strict:
-                    assert max(pad_b) == 0, pad_b
-                else:
-                    sys.stderr.write(
-                        'warning: post-data padding not zero: {}\n'.format(
-                            pad_b))
+            assert_null(pad_b, strict=strict)
             bin_info['note'] = str(f.read(bin_info['noteSize'])).strip()
         elif version == 3:
             # Post-data info:
@@ -507,13 +547,7 @@ def loadibw(filename, strict=True):
             no trailing null byte.
             """
             pad_b = buffer(f.read(16))  # skip the padding
-            if max(pad_b) != 0:
-                if strict:
-                    assert max(pad_b) == 0, pad_b
-                else:
-                    sys.stderr.write(
-                        'warning: post-data padding not zero: {}\n'.format(
-                            pad_b))
+            assert_null(pad_b, strict=strict)
             bin_info['note'] = str(f.read(bin_info['noteSize'])).strip()
             bin_info['formula'] = str(f.read(bin_info['formulaSize'])).strip()
         elif version == 5: