Exemplo n.º 1
0
def need_rebuild(filename):
    if not os.path.exists(filename):
        return False

    with open(filename) as infile:
        reader = csv.DictReader(infile)
        return reader.fieldnames != Transition.csv_headers()
Exemplo n.º 2
0
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())
Exemplo n.º 3
0
 def _write_header(self):
     print(','.join(Transition.csv_headers()), file=self.file)