basic parsing of talks csv

This commit is contained in:
Clinton Roy 2019-10-13 10:29:59 +11:00 committed by Joel Addison
parent 2591a943fe
commit 619a311487

View file

@ -1,12 +1,15 @@
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from dateutil.parser import parse
from collections import Counter from collections import Counter
from pathlib import Path from pathlib import Path
import csv import csv
class Command(BaseCommand): class Command(BaseCommand):
known_headers = ["date", "start time", "end time", "kind", "rooms"] known_headers = ["date", "start time", "end time", "kind", "rooms"]
SLOTS = 'slots'
TALKS = 'talks'
help = "Updates the schedule based on two csv files, "\ help = "Updates the schedule based on two csv files, "\
"one that gives all the talk slots, the other the talks." "one that gives all the talk slots, the other the talks."
@ -16,14 +19,15 @@ class Command(BaseCommand):
return (date, start_time, end_time, room) return (date, start_time, end_time, room)
def add_arguments(self, parser): def add_arguments(self, parser):
parser.add_argument('timeslots', type=Path) parser.add_argument(self.SLOTS, type=Path)
parser.add_argument('talks', nargs="?", type=Path, default=None) parser.add_argument(self.TALKS, nargs="?", type=Path, default=None)
def handle(self, *args, **options): def handle(self, *args, **options):
all_kinds, all_rooms = set(), set()
slot = {} slot = {}
slot_type_count = Counter() slot_type_count = Counter()
with open(options['timeslots']) as csv_file: with open(options[self.SLOTS]) as csv_file:
csv_reader = csv.reader(csv_file) csv_reader = csv.reader(csv_file)
headers = next(csv_reader) headers = next(csv_reader)
@ -33,12 +37,60 @@ class Command(BaseCommand):
for row in csv_reader: for row in csv_reader:
assert len(row) == len(self.known_headers) assert len(row) == len(self.known_headers)
date, start_time, end_time, kind, rooms = row rowdate, start_time, end_time, kind, rooms = row
all_kinds.add(kind)
if rowdate:
date = rowdate
assert kind, "kind cannot be blank"
slot_type_count[(date, kind)] += 1 slot_type_count[(date, kind)] += 1
slot_name = f"{kind} {slot_type_count[(date, kind)]}"
for room in rooms.split(';'): for room in rooms.split(';'):
room = room.strip() room = room.strip()
slot[(date, kind, slot_type_count[(date, kind)])] = self.find_or_create_slot( all_rooms.add(room)
slot[(date, slot_name, room)] = self.find_or_create_slot(
date, start_time, end_time, room) date, start_time, end_time, room)
print(slot.keys())
if options[self.TALKS]:
with open(options[self.TALKS]) as csv_file:
csv_reader = csv.reader(csv_file)
used_rooms = next(csv_reader)
assert used_rooms[0] == '', "Cell (1, 1) must be empty"
for room in used_rooms[1:]:
assert room in all_rooms, f"Unknown room: {room}"
for row in csv_reader:
cell = row[0]
if cell.rsplit(' ', 1)[0] in all_kinds:
kind = cell
print('used_rooms', used_rooms)
for i, room in enumerate(used_rooms[1:], 1):
talk_id = row[i]
if not talk_id:
continue
assert (date, kind, room) in slot, f"Slot ({date}, '{kind}', '{room}') not found"
# TODO set the talk slot to the associated talk
# place_talk(slot[(date, slot_name)].talk = int(talk_id)
else:
assert parse(row[0]), "Not a date: {row[0]}"
date = row[0]
for col in row[1:]:
assert col == '', f"All other columns must be empty: {date}"