From 8529f3e5cf0d4d5b38fe578e423142f2a87da71c Mon Sep 17 00:00:00 2001 From: flofriday Date: Thu, 11 Apr 2024 00:18:04 +0200 Subject: [PATCH] Improve floor parsing fallback The fallback now respects the language and is better formatted than it was before. --- app/format.py | 56 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/app/format.py b/app/format.py index 929af30..53057ed 100644 --- a/app/format.py +++ b/app/format.py @@ -3,12 +3,21 @@ import re from dataclasses import dataclass from functools import cache +from typing import Optional from icalendar import Calendar summary_regex = re.compile("([0-9A-Z]{3}\\.[0-9A-Z]{3}) ([A-Z]{2}) (.*)") +class MultiLangString: + "A string to hold multiple languages" + + def __init__(self, de: str, en: str = None) -> None: + self.de = de + self.en = en if en is not None else de + + @dataclass(kw_only=True, slots=True) class Event: name: str = "" @@ -20,7 +29,7 @@ class Event: address: str = "" room: str = "" room_code: str = "" - floor: str = "" + floor: Optional[MultiLangString] = None tiss_url: str = "" room_url: str = "" map_url: str = "" @@ -32,8 +41,8 @@ def plain_description_en(self) -> str: text += f"{self.name}\n" text += f"Room: {self.room}\n" - if self.floor != "": - text += f"Floor: {self.floor}\n" + if self.floor is not None: + text += f"Floor: {self.floor.en}\n" text += f"\n{self.description}" return text @@ -54,8 +63,8 @@ def html_description_en(self) -> str: else: text += f"Room: {self.room}
" - if self.floor != "": - text += f"Floor: {self.floor}
" + if self.floor is not None: + text += f"Floor: {self.floor.en}
" text += f"
{html.escape(self.description)}" @@ -67,8 +76,8 @@ def plain_description_de(self) -> str: if self.shorthand != "": text += f"{self.name}" text += f"\nRaum: {self.room}\n" - if self.floor != "": - text += f"Stock: {self.floor}\n" + if self.floor is not None: + text += f"Stock: {self.floor.de}\n" text += f"\n{self.description}" return text @@ -89,8 +98,8 @@ def html_description_de(self) -> str: else: text += f"Raum: {self.room}
" - if self.floor != "": - text += f"Stock: {self.floor}
" + if self.floor is not None: + text += f"Stock: {self.floor.en}
" text += f"
{html.escape(self.description)}" @@ -235,7 +244,7 @@ def add_location(event: Event) -> Event: return event -def create_floor_fallback(room_code: str) -> str: +def create_floor_fallback(room_code: str) -> MultiLangString: """The floor information is encoded in the room code. Format: TTFFRR[R] @@ -244,11 +253,22 @@ def create_floor_fallback(room_code: str) -> str: """ if len(room_code) < 6 or len(room_code) > 7: - return "" + return None + + floor_code = room_code[2:4] - # FIXME: Yes I also really hate that there is text in here and in a language - # that might not be the users prevered one. - return room_code[2:4] + " (evtl. ungenau)" + if floor_code.isnumeric(): + floor = int(floor_code) + return MultiLangString(f"{floor}. Stock", f"{floor}. Floor") + elif floor_code == "EG": + return MultiLangString("Erdgeschoß", "Ground Floor") + elif floor_code == "DG": + return MultiLangString("Dachgeschoß", "Roof Floor") + elif floor_code[0] == "U" and floor_code[1].isnumeric(): + floor = int(floor_code[1]) + return MultiLangString(f"{floor}. Untergeschoß", f"{floor}. Underground Floor") + else: + return MultiLangString(floor_code) @cache @@ -272,7 +292,7 @@ def read_shorthands() -> dict[str, str]: @cache -def read_rooms() -> dict[str, tuple[str, str, str, str]]: +def read_rooms() -> dict[str, tuple[str, MultiLangString, str, str]]: with open("app/resources/rooms.csv") as f: reader = csv.reader(f) @@ -288,7 +308,11 @@ def read_rooms() -> dict[str, tuple[str, str, str, str]]: floor_fields = [ field for field in floor_fields if any([kw in field for kw in keywords]) ] - floor = floor_fields[0] if floor_fields != [] else "" + floor = None + if floor_fields != []: + raw_floor = floor_fields[0] + # FIXME: Some parsing for localization + floor = MultiLangString(raw_floor) if floor_fields != [] else None code = fields[7].strip() url = fields[8].strip()