def find_dates(stream): records = [ ] bolus = bytearray(stream.read(4)) extra = bytearray( ) opcode = '' for B in iter(lambda: stream.read(1), ""): h, t = B[:1], B[1:] bolus.append(h) bolus.extend(t) if len(bolus) < 6: bolus.extend(stream.read(opcode_min_read(bolus[0]))) try: date = parse_date(bolus[-5:]) opcode = bolus[0] if len(bolus) <= 5: raise NotADate('too short of a record') #sniff_invalid_opcode( bolus[0] ) extra_len = opcode_read_ahead(bolus[0]) if extra_len > 0: extra.extend( bytearray(stream.read(extra_len)) ) records.append( (date, bolus[:-5], bolus[-5:], extra) ) bolus = bytearray(stream.read(4)) extra = bytearray( ) opcode = '' except NotADate, e: opcode = bolus[0] pass
def parse_date(date): try: return history.parse_date(date) except NotADate, e: pass
def find_dates(stream): records = [] errors = [] bolus = bytearray() extra = bytearray() opcode = '' for B in iter(lambda: bytearray(stream.read(2)), bytearray("")): opcode = B[0] #bolus.append(B) bolus.extend(B) head_length = Record.lookup_head(opcode) body_length = Record.lookup_body(opcode) date_length = Record.lookup_date(opcode) total = len(bolus) if total < head_length: bolus.extend(bytearray(stream.read(head_length - total))) variable_read = Record.variable_read(opcode, bolus) if variable_read > 2: #print "super special" bolus.extend(bytearray(stream.read(variable_read))) #print lib.hexdump( bolus ) opcode = bolus[variable_read] #print "NEW OPCODE %#04x" % opcode head_length = Record.lookup_head(opcode) body_length = Record.lookup_body(opcode) total = len(bolus) - variable_read if total < head_length: bolus.extend(stream.read(head_length - total)) total = len(bolus) #body_length = Record.lookup_body(opcode) #date_length = Record.lookup_date(opcode) #total = len(bolus) total = len(bolus) head_length = total head = bolus[:max(total, 1)] bolus.extend(bytearray(stream.read(date_length))) date = bolus[head_length:head_length + date_length] total = len(bolus) # print repr(bolus), date_length, repr(date) if len(date) < 5: print "DATE LESS THAN 5!", stream.tell() print lib.hexdump(bolus) break records[-1].body.extend(bolus) datetime = parse_date(date) if bytearray([0x00] * max(total - 2, 5)) in bolus: nulls = bytearray(eat_nulls(stream)) pos = stream.tell() if pos > 1021: bolus = bolus + nulls + bytearray(stream.read(-1)) crc = bolus[-2:] nulls = bolus[:-2] print "EOF {} nulls, CRC:".format(len(nulls)) print lib.hexdump(crc) else: print total, ' ', max(total - 2, 5) print lib.hexdump([0x00] * min(total, 5)) print print "TOO MANY NULLS, BAILING ON STREAM at %s " % stream.tell( ) print "bolus" print lib.hexdump(bolus) print "nulls" print lib.hexdump(nulls) print "MISSING: ARE THERE 32 more bytes?" print lib.hexdump(bytearray(stream.read(32))) # records[-1].body.extend(nulls) break if not Record.is_midnight(head): if datetime is None: print("#### MISSING DATETIME @ %s," % stream.tell()), print "reading more to debug %#04x" % opcode print lib.hexdump(bolus, indent=4) print int_dump(bolus, indent=11) extra = bytearray(stream.read(32)) print "##### DEBUG HEX" print lib.hexdump(extra, indent=4) print "##### DEBUG DECIMAL" print int_dump(extra, indent=11) if history.parse_date(bolus): print "XXX:???:XXX", history.parse_date(bolus).isoformat() break if datetime is not None or Record.is_midnight(head): body = bytearray(stream.read(body_length)) bolus.extend(body) if False or Record.seeks_null(opcode, body): print "should eat up to null, second %s" % repr(body[1:]) if body[1:]: if body[-1] != 0x00: extra = seek_null(stream) print "found %s extra" % len(extra) body.extend(extra) bolus.extend(extra) epi = bytearray(stream.read(date_length)) finished = parse_date(epi) record = Record(head, date, body) prefix = "#### RECORD %s %s" % (len(records), str(record)) print record.pformat(prefix) print "" records.append(record) bolus = bytearray() opcode = '' return records
def parse_date(date): try: return history.parse_date(date) except NotADate, e: pass return None
def find_dates(stream): records = [ ] errors = [ ] bolus = bytearray( ) extra = bytearray( ) opcode = '' for B in iter(lambda: bytearray(stream.read(2)), bytearray("")): opcode = B[0] #bolus.append(B) bolus.extend(B) head_length = Record.lookup_head(opcode) body_length = Record.lookup_body(opcode) date_length = Record.lookup_date(opcode) total = len(bolus) if total < head_length: bolus.extend(bytearray(stream.read(head_length-total))) variable_read = Record.variable_read(opcode, bolus) if variable_read > 2: #print "super special" bolus.extend(bytearray(stream.read(variable_read))) #print lib.hexdump( bolus ) opcode = bolus[variable_read] #print "NEW OPCODE %#04x" % opcode head_length = Record.lookup_head(opcode) body_length = Record.lookup_body(opcode) total = len(bolus) - variable_read if total < head_length: bolus.extend( stream.read(head_length-total) ) total = len(bolus) #body_length = Record.lookup_body(opcode) #date_length = Record.lookup_date(opcode) #total = len(bolus) total = len(bolus) head_length = total head = bolus[:max(total, 1)] bolus.extend(bytearray(stream.read(date_length))) date = bolus[head_length:head_length+date_length] total = len(bolus) # print repr(bolus), date_length, repr(date) if len(date) < 5: print "DATE LESS THAN 5!", stream.tell( ) print lib.hexdump(bolus) break records[-1].body.extend(bolus) datetime = parse_date(date) if bytearray( [0x00] * max(total-2, 5) ) in bolus: nulls = bytearray(eat_nulls(stream)) pos = stream.tell( ) if pos > 1021: bolus = bolus + nulls + bytearray(stream.read(-1)) crc = bolus[-2:] nulls = bolus[:-2] print "EOF {} nulls, CRC:".format(len(nulls)) print lib.hexdump(crc) else: print total, ' ', max(total-2, 5) print lib.hexdump( [0x00] * min(total, 5) ) print print "TOO MANY NULLS, BAILING ON STREAM at %s " % stream.tell( ) print "bolus" print lib.hexdump(bolus) print "nulls" print lib.hexdump(nulls) print "MISSING: ARE THERE 32 more bytes?" print lib.hexdump(bytearray(stream.read(32))) # records[-1].body.extend(nulls) break if not Record.is_midnight(head): if datetime is None: print ("#### MISSING DATETIME @ %s," % stream.tell( )), print "reading more to debug %#04x" % opcode print lib.hexdump(bolus, indent=4) print int_dump(bolus, indent=11) extra = bytearray(stream.read(32)) print "##### DEBUG HEX" print lib.hexdump(extra, indent=4) print "##### DEBUG DECIMAL" print int_dump(extra, indent=11) if history.parse_date(bolus): print "XXX:???:XXX", history.parse_date(bolus).isoformat( ) break if datetime is not None or Record.is_midnight(head): body = bytearray(stream.read(body_length)) bolus.extend(body) if False or Record.seeks_null(opcode, body): print "should eat up to null, second %s" % repr(body[1:]) if body[1:]: if body[-1] != 0x00: extra = seek_null(stream) print "found %s extra" % len(extra) body.extend(extra) bolus.extend(extra) epi = bytearray(stream.read(date_length)) finished = parse_date(epi) record = Record(head, date, body) prefix = "#### RECORD %s %s" % (len(records), str(record) ) print record.pformat(prefix) print "" records.append(record) bolus = bytearray( ) opcode = '' return records