diff --git a/lflex_celcat_survival/events.py b/lflex_celcat_survival/events.py index f28b4971c6261b8e73b1e1a9cf1ccda545bd8989..7dd1a72effe1fe4871aafe16a96cf69fbbc5e36b 100644 --- a/lflex_celcat_survival/events.py +++ b/lflex_celcat_survival/events.py @@ -11,7 +11,7 @@ import re from . import fetch ROOM_RE = re.compile(r'^(?:FSI|F2SMH) / (.*)$') -COURSE_TYPE_RE = re.compile(r'COURS|COURS/TD|TD|TP|CONTROLE CONTINU|CONTROLE PARTIEL') +COURSE_TYPE_RE = re.compile(r'COURS|COURS/TD|TD|TP|CONTROLE CONTINU|CONTROLE PARTIEL|EXAMEN|Controle de Substitution|CONSULTATION DE COPIES') STUDENT_GROUP_RE = re.compile(r'K?IN[A-Z0-9]+') class CelcatEvents: @@ -98,47 +98,52 @@ def parse_description(row): groups = [] course_type = 'unset' - if len(fields) == 0: - raise ValueError(f'There should be at least 1 field, but fields are {fields}') - elif len(fields) == 1: - # probably not a course. examples: "CONGES\r\n" or "FERIE\r\n" - course_type = fields[0] - else: - # first fields should be the room, but this is not always set - while (m := ROOM_RE.match(fields[0])) is not None: - rooms.append(m[1]) + try: + if len(fields) == 0: + raise ValueError(f'There should be at least 1 field, but fields are {fields}') + elif len(fields) == 1: + # probably not a course. examples: "CONGES\r\n" or "FERIE\r\n" + course_type = fields[0] + else: + # first fields should be the room, but this is not always set + while (m := ROOM_RE.match(fields[0])) is not None: + rooms.append(m[1]) + fields = fields[1:] + + # assume that the next field is the course name, and skip it fields = fields[1:] - # assume that the next field is the course name, and skip it - fields = fields[1:] + # skip notes at the end of the fields until they look like a course type + while COURSE_TYPE_RE.match(fields[-1]) is None: + fields = fields[:-1] + if len(fields) <= 0: + break - # skip notes at the end of the fields until they look like a course type - while COURSE_TYPE_RE.match(fields[-1]) is None: + # last field is a course type + course_type = fields[-1] fields = fields[:-1] - if len(fields) <= 0: - break - # last field is a course type - course_type = fields[-1] - fields = fields[:-1] + # the last field may be a teacher, but this is optional + if STUDENT_GROUP_RE.match(fields[-1]) is None: + teacher = fields[-1] + fields = fields[:-1] - # the last field may be a teacher, but this is optional - if STUDENT_GROUP_RE.match(fields[-1]) is None: - teacher = fields[-1] - fields = fields[:-1] + # all remaining fields should be student groups + groups = [] + while len(fields) > 0 and (m := STUDENT_GROUP_RE.match(fields[0])) is not None: + groups.append(m[0]) + fields = fields[1:] - # all remaining fields should be student groups - groups = [] - while len(fields) > 0 and (m := STUDENT_GROUP_RE.match(fields[0])) is not None: - groups.append(m[0]) - fields = fields[1:] + if len(rooms) == 0: + rooms = ['unset'] + if len(groups) == 0: + groups = ['unset'] - if len(rooms) == 0: - rooms = ['unset'] - if len(groups) == 0: - groups = ['unset'] + return pd.Series([rooms, teacher, course_type, groups], index=['rooms_parsed', 'teacher_parsed', 'course_type_parsed', 'groups_parsed']) + except Exception as e: + print(f"Could not parse an event description. fields={preparse_fields}") + raise e - return pd.Series([rooms, teacher, course_type, groups], index=['rooms_parsed', 'teacher_parsed', 'course_type_parsed', 'groups_parsed']) def request_slots_by_mod_code(flat_slot_df, session): subject_codes = list(flat_slot_df['mod_code'].dropna().unique())