Read a candump log file and return a packet list

count: read only <count> packets
is_not_log_file_format: read input with candumps stdout format
interfaces: return only packets from a specified interface

def rdcandump(filename, count=None,
              is_not_log_file_format=False,
              interface=None):
    """Read a candump log file and return a packet list

count: read only <count> packets
is_not_log_file_format: read input with candumps stdout format
interfaces: return only packets from a specified interface

    """
    try:
        if isinstance(filename, six.string_types):
            file = open(filename, "rb")
        else:
            file = filename

        pkts = list()
        ifilter = None
        if interface is not None:
            if isinstance(interface, six.string_types):
                ifilter = [interface]
            else:
                ifilter = interface

        for l in file.readlines():
            if is_not_log_file_format:
                h, data = l.split(b']')
                intf, idn, le = h.split()
                t = None
            else:
                t, intf, f = l.split()
                idn, data = f.split(b'#')
                le = None
                t = float(t[1:-1])

            if ifilter is not None and intf.decode('ASCII') not in ifilter:
                continue

            data = data.replace(b' ', b'')
            data = data.strip()

            pkt = CAN(identifier=int(idn, 16), data=binascii.unhexlify(data))
            if le is not None:
                pkt.length = int(le[1:])
            else:
                pkt.length = len(pkt.data)

            if len(idn) > 3:
                pkt.flags = 0b100

            if t is not None:
                pkt.time = t

            pkts.append(pkt)
            if count is not None and len(pkts) >= count:
                break

    finally:
        file.close()

    return pkts