def rebuild_history(rooms, doors, input_filenames, output_filename): # Some older files don't have door ids, so we need to infer the door # ids from the room ids by_room_and_exit = {} for input in input_filenames: with open(input) as infile: reader = csv.DictReader(infile) for row in reader: transition = Transition.from_csv_row(rooms, doors, row) if transition.id.entry_room is NullRoom: # print("ignoring %s" % transition.id) continue key = (transition.id.room, transition.id.exit_room) r = by_room_and_exit.get(key) if r is None: by_room_and_exit[key] = {} by_room_and_exit[key][transition.id] = True with open(output_filename, 'w') as outfile: print(','.join(Transition.csv_headers()), file=outfile) writer = csv.writer(outfile) for input in input_filenames: with open(input) as infile: reader = csv.DictReader(infile) n = 1 # we already read the header for row in reader: n += 1 transition = Transition.from_csv_row(rooms, doors, row) if transition.id.entry_door.entry_room is NullRoom: key = (transition.id.room, transition.id.exit_room) l = by_room_and_exit.get(key) if l: if len(l) == 1: print("Fixing entry door on line %d" % n) transition.id.entry_door = list( l.keys())[0].entry_door else: print( "More than one entry door found for %s to $s (line %d): %s" % (transition.id.room, transition.id.exit_room, n, repr(l))) else: # print("No entry door found for %s to %s (line %d)" % # (transition.id.room, transition.id.exit_room, n)) pass writer.writerow(transition.as_csv_row())
def read_transition_log_csv_incrementally(csvfile, rooms, doors): history = History() reader = csv.DictReader(csvfile) n = 1 # start at 1 for the header for row in reader: n += 1 try: action = 'reading history file' transition = Transition.from_csv_row(rooms, doors, row) action = 'recording transition' history.record(transition, from_file=True) yield history, transition except Exception as e: raise RuntimeError("Error %s, line %d\nrow: %s" % (action, n, row)) from e return history