import os
import numpy as np
from phobos import cobs
from phobos import pose

def pose_logfile(filename, dtype=None):
    if dtype is None:
        _, dtype, _ = pose.parse_format(pose.pose_def_file)

    bytedata = None
    gitsha1 = None
    num_errors = 0
    data = bytearray()

    with open(filename, 'rb') as f:
        bytedata = f.read()

    packet_start = 0;
    packet_end = 0;
    mv = memoryview(bytedata)
    for i, byte in enumerate(bytedata):
        #if byte == b'\x00': # COBS packet delimiter
        if byte == 0: # b'\x00': # COBS packet delimiter
                      # Python 3.3+ returns an int when iterating over a
                      # 'bytes' object??
            packet_end = i
            try:
                p = cobs.decode(mv[packet_start:packet_end], True)
            except cobs.DecodeError as e:
                # further testing necessary to determine cause of:
                # 'not enough input bytes for length code'
                # print(e)
                num_errors += 1
                # add a packet with all floats as nan
                data.extend(bytearray([0xff] * dtype.itemsize))
            else:
                if len(p) != dtype.itemsize:
                    if len(p) == 7: # this is sent as the first packet
                        gitsha1 = p.decode('ascii')
                    else:
                        print('invalid packet size: {0} not {1}'.format(
                            len(p), dtype.itemsize))
                else:
                    data.extend(p)
            finally:
                packet_start = i + 1
                packet_end = i + 1

    return gitsha1, np.frombuffer(data, dtype), num_errors

@@ -1681,8 +1681,912 @@
 _errors%0A
+%0A%0Adef cobs_framed_log(filename, datum_datatype=None):%0A    bytedata = None%0A    packets = %5B%5D%0A    datums = %5B%5D%0A%0A    with open(filename, 'rb') as f:%0A        bytedata = f.read()%0A    mv = memoryview(bytedata)%0A%0A    packet_start = 0;%0A    packet_end = 0;%0A    num_errors = 0;%0A    for i, byte in enumerate(bytedata):%0A        if byte == 0:%0A            packet_end = i%0A            try:%0A                unstuffed_packet = cobs.decode(mv%5Bpacket_start:packet_end%5D)%0A            except cobs.DecodeError as e:%0A                print(e)%0A                num_errors += 1%0A                # TODO handle unstuff errors%0A            else:%0A                packets.append(unstuffed_packet)%0A                # TODO pop packets and decode to datums%0A            finally:%0A                packet_start = i + 1%0A                packet_end = i + 1%0A    print(%22%7B%7D error(s) when unstuffing file %7B%7D%22.format(num_errors, filename))%0A    return packets%0A

Add Python functions to unstuff COBS packets