import re
import xml.etree.ElementTree
import collections
-from typing import Tuple, Optional, List
+from typing import Tuple, Optional, List, OrderedDict, Union
from mwparserfromhell.nodes import Template
opt_phone_comment_enum_to_str, lift_german_from_str, GASTHAUSBOX_DICT
-def sledrun_from_rodelbahnbox(value, sledrun):
+def split_lon_lat(value: Optional[LonLat]) -> Union[LonLat, Tuple[None, None]]:
+ if value is None:
+ return None, None
+ return value
+
+
+def join_lon_lat(lon: Optional[float], lat: Optional[float]) -> Optional[LonLat]:
+ if lon is None or lat is None:
+ return None
+ return LonLat(lon, lat)
+
+
+def sledrun_from_rodelbahnbox(value: OrderedDict, sledrun: object):
"""Takes a Rodelbahnbox as returned by rodelbahnbox_from_str (that is, an OrderedDict) and
updates the sledrun instance with all values present in the Rodelbahnbox. Other values are not
updated. Does not validate the arguments."""
# sledrun.page_id = None # this field is not updated because it is not present in the RodelbahnBox
# sledrun.page_title = None # this field is not updated because it is not present in the RodelbahnBox
# sledrun.name_url = None # this field is not updated because it is not present in the RodelbahnBox
- sledrun.position_longitude, sledrun.position_latitude = value['Position']
- sledrun.top_longitude, sledrun.top_latitude = value['Position oben']
+ sledrun.position_longitude, sledrun.position_latitude = split_lon_lat(value['Position'])
+ sledrun.top_longitude, sledrun.top_latitude = split_lon_lat(value['Position oben'])
sledrun.top_elevation = value['Höhe oben']
- sledrun.bottom_longitude, sledrun.bottom_latitude = value['Position unten']
+ sledrun.bottom_longitude, sledrun.bottom_latitude = split_lon_lat(value['Position unten'])
sledrun.bottom_elevation = value['Höhe unten']
sledrun.length = value['Länge']
sledrun.difficulty = value['Schwierigkeit']
"""Takes a sledrun instance that might come from the database and converts it to a OrderedDict ready
to be formatted as RodelbahnBox."""
value = collections.OrderedDict()
- value['Position'] = LonLat(sledrun.position_longitude, sledrun.position_latitude)
- value['Position oben'] = LonLat(sledrun.top_longitude, sledrun.top_latitude)
+ value['Position'] = join_lon_lat(sledrun.position_longitude, sledrun.position_latitude)
+ value['Position oben'] = join_lon_lat(sledrun.top_longitude, sledrun.top_latitude)
value['Höhe oben'] = sledrun.top_elevation
- value['Position unten'] = LonLat(sledrun.bottom_longitude, sledrun.bottom_latitude)
+ value['Position unten'] = join_lon_lat(sledrun.bottom_longitude, sledrun.bottom_latitude)
value['Höhe unten'] = sledrun.bottom_elevation
value['Länge'] = sledrun.length
value['Schwierigkeit'] = sledrun.difficulty
if v == '':
return None
return v
- inn.position_longitude, inn.position_latitude = value['Position']
+ inn.position_longitude, inn.position_latitude = split_lon_lat(value['Position'])
inn.position_elevation = value['Höhe']
inn.operator = value['Betreiber']
inn.seats = value['Sitzplätze']
def inn_to_gasthausbox(inn) -> collections.OrderedDict:
"""Converts an inn class to a dict of Gasthausbox properties. inn is an Inn instance."""
def convfromdb(val, key):
- v = '' if value is None else val
+ v = '' if val is None else val
return GASTHAUSBOX_DICT[key].from_str(v)
value = collections.OrderedDict()
- value['Position'] = LonLat(inn.position_longitude, inn.position_latitude)
+ value['Position'] = join_lon_lat(inn.position_longitude, inn.position_latitude)
value['Höhe'] = inn.position_elevation
value['Betreiber'] = inn.operator
value['Sitzplätze'] = inn.seats