1 """This module contains winterrodeln specific functions that are processing the MediaWiki markup.
4 import xml.etree.ElementTree
6 from typing import Tuple, Optional, List, OrderedDict, Union, Dict, Any
9 from mwparserfromhell.nodes import Template, Wikilink
11 import wrpylib.wrvalidators
12 import wrpylib.mwmarkup
14 from wrpylib.lib_sledrun_wikitext_from_json import strip_eol
15 from wrpylib.wrgeojson import simplify_ways, DEFAULT_MAX_DIST_ERROR_M
16 from wrpylib.wrvalidators import LonLat, opt_lonlat_from_str, opt_lonlat_to_str, opt_uint_from_str, opt_uint_to_str, \
17 opt_str_opt_comment_enum_to_str, lift_german_to_str, webauskunft_to_str, cachet_german_to_str, \
18 opt_phone_comment_enum_to_str, lift_german_from_str, GASTHAUSBOX_DICT, opt_difficulty_german_from_str, \
19 opt_avalanches_german_from_str, opt_public_transport_german_from_str, \
20 opt_tristate_german_comment_from_str, rodelbahnbox_to_str, lonlat_to_str, opt_no_or_str_to_str, \
21 opt_no_or_str_from_str, opt_tristate_german_from_str, tristate_german_from_str
24 def split_lon_lat(value: Optional[LonLat]) -> Union[LonLat, Tuple[None, None]]:
30 def join_lon_lat(lon: Optional[float], lat: Optional[float]) -> Optional[LonLat]:
31 if lon is None or lat is None:
33 return LonLat(lon, lat)
36 def sledrun_from_rodelbahnbox(value: OrderedDict, sledrun: object):
37 """Takes a Rodelbahnbox as returned by rodelbahnbox_from_str (that is, an OrderedDict) and
38 updates the sledrun instance with all values present in the Rodelbahnbox. Other values are not
39 updated. Does not validate the arguments."""
40 # sledrun.page_id = None # this field is not updated because it is not present in the RodelbahnBox
41 # sledrun.page_title = None # this field is not updated because it is not present in the RodelbahnBox
42 # sledrun.name_url = None # this field is not updated because it is not present in the RodelbahnBox
43 sledrun.position_longitude, sledrun.position_latitude = split_lon_lat(value['Position'])
44 sledrun.top_longitude, sledrun.top_latitude = split_lon_lat(value['Position oben'])
45 sledrun.top_elevation = value['Höhe oben']
46 sledrun.bottom_longitude, sledrun.bottom_latitude = split_lon_lat(value['Position unten'])
47 sledrun.bottom_elevation = value['Höhe unten']
48 sledrun.length = value['Länge']
49 sledrun.difficulty = value['Schwierigkeit']
50 sledrun.avalanches = value['Lawinen']
51 sledrun.operator = opt_no_or_str_to_str(value['Betreiber'])
52 sledrun.public_transport = value['Öffentliche Anreise']
53 sledrun.walkup_possible = value['Aufstieg möglich']
54 sledrun.walkup_time = value['Gehzeit']
55 sledrun.walkup_separate, sledrun.walkup_separate_comment = value['Aufstieg getrennt']
56 sledrun.lift = None if value['Aufstiegshilfe'] is None else len(value['Aufstiegshilfe']) > 0
57 sledrun.lift_details = lift_german_to_str(value['Aufstiegshilfe'])
58 sledrun.night_light, sledrun.night_light_comment = value['Beleuchtungsanlage']
59 sledrun.night_light_days, sledrun.night_light_days_comment = value['Beleuchtungstage']
60 sledrun.sled_rental = None if value['Rodelverleih'] is None else len(value['Rodelverleih']) > 0
61 sledrun.sled_rental_comment = opt_str_opt_comment_enum_to_str(value['Rodelverleih'])
62 sledrun.cachet = cachet_german_to_str(value['Gütesiegel'])
63 sledrun.information_web = webauskunft_to_str(value['Webauskunft'])
64 sledrun.information_phone = opt_phone_comment_enum_to_str(value['Telefonauskunft'])
65 sledrun.image = value['Bild']
66 sledrun.show_in_overview = value['In Übersichtskarte']
67 sledrun.forum_id = value['Forumid']
68 # sledrun.under_construction = None # this field is not updated because it is not present in the RodelbahnBox
72 def sledrun_to_rodelbahnbox(sledrun) -> collections.OrderedDict:
73 """Takes a sledrun instance that might come from the database and converts it to a OrderedDict ready
74 to be formatted as RodelbahnBox."""
75 value = collections.OrderedDict()
76 value['Position'] = join_lon_lat(sledrun.position_longitude, sledrun.position_latitude)
77 value['Position oben'] = join_lon_lat(sledrun.top_longitude, sledrun.top_latitude)
78 value['Höhe oben'] = sledrun.top_elevation
79 value['Position unten'] = join_lon_lat(sledrun.bottom_longitude, sledrun.bottom_latitude)
80 value['Höhe unten'] = sledrun.bottom_elevation
81 value['Länge'] = sledrun.length
82 value['Schwierigkeit'] = sledrun.difficulty
83 value['Lawinen'] = sledrun.avalanches
84 value['Betreiber'] = opt_no_or_str_from_str(sledrun.operator)
85 value['Öffentliche Anreise'] = sledrun.public_transport
86 value['Aufstieg möglich'] = sledrun.walkup_possible
87 value['Gehzeit'] = sledrun.walkup_time
88 value['Aufstieg getrennt'] = sledrun.walkup_separate, sledrun.walkup_separate_comment
89 value['Aufstiegshilfe'] = lift_german_from_str(sledrun.lift_details)
90 value['Beleuchtungsanlage'] = sledrun.night_light, sledrun.night_light_comment
91 value['Beleuchtungstage'] = sledrun.night_light_days, sledrun.night_light_days_comment
92 value['Rodelverleih'] = sledrun.sled_rental, sledrun.sled_rental_comment
93 value['Gütesiegel'] = sledrun.cachet
94 value['Webauskunft'] = sledrun.information_web
95 value['Telefonauskunft'] = sledrun.information_phone
96 value['Bild'] = sledrun.image
97 value['In Übersichtskarte'] = sledrun.show_in_overview
98 value['Forumid'] = sledrun.forum_id
102 def inn_from_gasthausbox(value, inn):
103 """Converts a dict with Gasthausbox properties to a Inn class. Does no validation.
104 value is a dict of properties as returned by gasthausbox_from_str."""
106 # page_id = None # this field is not updated because it is not present in the Gasthausbox
107 # page_title = None # this field is not updated because it is not present in the Gasthausbox
108 def convtodb(val, key):
109 v = GASTHAUSBOX_DICT[key].to_str(val[key])
114 inn.position_longitude, inn.position_latitude = split_lon_lat(value['Position'])
115 inn.position_elevation = value['Höhe']
116 inn.operator = value['Betreiber']
117 inn.seats = value['Sitzplätze']
118 inn.overnight, inn.overnight_comment = value['Übernachtung']
119 inn.smoker_area = None if value['Rauchfrei'] is None else value['Rauchfrei'] < 0.9
120 inn.nonsmoker_area = None if value['Rauchfrei'] is None else value['Rauchfrei'] > 0.1
121 inn.sled_rental, inn.sled_rental_comment = value['Rodelverleih']
122 inn.mobile_provider = convtodb(value, 'Handyempfang')
123 inn.homepage = convtodb(value, 'Homepage')
124 inn.email_list = convtodb(value, 'E-Mail')
125 inn.phone_list = convtodb(value, 'Telefon')
126 inn.image = value['Bild']
127 inn.sledding_list = convtodb(value, 'Rodelbahnen')
128 # under_construction = None # this field is not updated because it is not present in the GasthausBox
132 def inn_to_gasthausbox(inn) -> collections.OrderedDict:
133 """Converts an inn class to a dict of Gasthausbox properties. inn is an Inn instance."""
135 def convfromdb(val, key):
136 v = '' if val is None else val
137 return GASTHAUSBOX_DICT[key].from_str(v)
139 value = collections.OrderedDict()
140 value['Position'] = join_lon_lat(inn.position_longitude, inn.position_latitude)
141 value['Höhe'] = inn.position_elevation
142 value['Betreiber'] = inn.operator
143 value['Sitzplätze'] = inn.seats
144 value['Übernachtung'] = (inn.overnight, inn.overnight_comment)
145 value['Rauchfrei'] = {(False, True): 0.0, (True, True): 0.5, (True, False): 1.0} \
146 .get((inn.nonsmoker_area, inn.smoker_area), None)
147 value['Rodelverleih'] = (inn.sled_rental, inn.sled_rental_comment)
148 value['Handyempfang'] = convfromdb(inn.mobile_provider, 'Handyempfang')
149 value['Homepage'] = convfromdb(inn.homepage, 'Homepage')
150 value['E-Mail'] = convfromdb(inn.email_list, 'E-Mail')
151 value['Telefon'] = convfromdb(inn.phone_list, 'Telefon')
152 value['Bild'] = inn.image
153 value['Rodelbahnen'] = convfromdb(inn.sledding_list, 'Rodelbahnen')
157 def lonlat_ele_from_template(template) -> Tuple[Optional[LonLat], Optional[int]]:
158 """Template is a `mwparserfromhell.nodes.template.Template` instance. Returns (lonlat, ele)."""
159 lonlat = opt_lonlat_from_str(template.params[0].strip())
160 ele = opt_uint_from_str(template.params[1].strip())
164 def latlon_ele_to_template(lonlat_ele: Tuple[Optional[LonLat], Optional[int]], name: str) -> Template:
165 lonlat, ele = lonlat_ele
166 template = Template(name)
167 template.add(1, opt_lonlat_to_str(lonlat))
168 template.add(2, opt_uint_to_str(ele))
169 wrpylib.mwmarkup.format_template_oneline(template)
173 def lonlat_to_json(lonlat: LonLat) -> dict:
174 return {'longitude': lonlat.lon, 'latitude': lonlat.lat}
177 def lonlat_ele_to_json(lonlat: Optional[LonLat], ele: Optional[int]) -> dict:
179 if lonlat is not None:
180 result['position'] = lonlat_to_json(lonlat)
182 result['elevation'] = ele
186 class ParseError(RuntimeError):
187 """Exception used by some of the functions"""
191 def parse_wrmap_coordinates(coords: str) -> List[List[float]]:
192 """gets a string coordinates and returns an array of lon/lat coordinate pairs, e.g.
196 [[11.87, 47.12], [11.70, 47.13]]"""
199 for match in re.finditer(r'\s*(\d+\.?\d*)\s*N?\s+(\d+\.?\d*)\s*E?\s*', coords):
200 if match.start() != pos:
202 result.append([float(match.groups()[1]), float(match.groups()[0])])
205 if pos == len(coords):
207 raise RuntimeError(f'Wrong coordinate format: {coords}')
210 WRMAP_POINT_TYPES = ['gasthaus', 'haltestelle', 'parkplatz', 'achtung', 'foto', 'verleih', 'punkt']
211 WRMAP_LINE_TYPES = ['rodelbahn', 'gehweg', 'alternative', 'lift', 'anfahrt', 'linie']
214 def parse_wrmap(wikitext: str) -> dict:
215 """Parses the '<wrmap ...>content</wrmap>' of the Winterrodeln wrmap extension.
216 If wikitext does not contain the <wrmap> tag or if the <wrmap> tag contains
217 invalid formatted lines, a ParseError is raised.
218 Use wrpylib.mwmarkup.find_tag(wikitext, 'wrmap') to find the wrmap tag within an arbitrary
219 wikitext before using this function.
221 :param wikitext: wikitext containing only the template. Example:
224 <wrmap lat="47.2417134" lon="11.21408895" zoom="14" width="700" height="400">
225 <gasthaus name="Rosskogelhütte" wiki="Rosskogelhütte">47.240689 11.190454</gasthaus>
226 <parkplatz>47.245789 11.238971</parkplatz>
227 <haltestelle name="Oberperfuss Rangger Köpfl Lift">47.245711 11.238283</haltestelle>
235 :returns: GeoJSON as nested Python datatype
239 wrmap_xml = xml.etree.ElementTree.fromstring(wikitext)
240 except xml.etree.ElementTree.ParseError as e:
241 row, column = e.position
242 raise ParseError(f"XML parse error on row {row}, column {column}: {e}")
243 if wrmap_xml.tag not in ['wrmap', 'wrgmap']:
244 raise ParseError('No valid tag name')
246 # convert XML to geojson (http://www.geojson.org/geojson-spec.html)
248 for feature in wrmap_xml:
249 # determine feature type
250 is_point = feature.tag in WRMAP_POINT_TYPES
251 is_line = feature.tag in WRMAP_LINE_TYPES
252 if not is_point and not is_line:
253 raise ParseError(f'Unknown element <{feature.tag}>.')
257 properties = {'type': feature.tag}
258 allowed_properties = {'name', 'wiki'}
259 wrong_properties = set(feature.attrib.keys()) - allowed_properties
260 if len(wrong_properties) > 0:
261 raise ParseError(f"The attribute '{list(wrong_properties)[0]}' is not allowed at <{feature.tag}>.")
262 properties.update(feature.attrib)
263 coordinates = parse_wrmap_coordinates(feature.text)
264 if len(coordinates) != 1:
265 raise ParseError(f'The element <{feature.tag}> has to have exactly one coordinate pair.')
266 json_features.append({
268 'geometry': {'type': 'Point', 'coordinates': coordinates[0]},
269 'properties': properties})
273 properties = {'type': feature.tag}
274 allowed_properties = {'farbe', 'dicke'}
275 wrong_properties = set(feature.attrib.keys()) - allowed_properties
276 if len(wrong_properties) > 0:
277 raise ParseError(f"The attribute '{list(wrong_properties)[0]}' is not allowed at <{feature.tag}>.")
278 if 'farbe' in feature.attrib:
279 if not re.match('#[0-9a-fA-F]{6}$', feature.attrib['farbe']):
280 raise ParseError('The attribute "farbe" has to have a format like "#a0bb43".')
281 properties['strokeColor'] = feature.attrib['farbe'] # e.g. #a200b7
282 if 'dicke' in feature.attrib:
284 properties['strokeWidth'] = int(feature.attrib['dicke']) # e.g. 6
286 raise ParseError('The attribute "dicke" has to be an integer.')
287 json_features.append({
289 'geometry': {'type': 'LineString', 'coordinates': parse_wrmap_coordinates(feature.text)},
290 'properties': properties})
294 for k, v in wrmap_xml.attrib.items():
295 if k in ['lat', 'lon']:
297 properties[k] = float(v)
299 raise ParseError(f'Attribute "{k}" has to be a float value.')
300 elif k in ['zoom', 'width', 'height']:
302 properties[k] = int(v)
304 raise ParseError(f'Attribute "{k}" has to be an integer value.')
306 raise ParseError(f'Unknown attribute "{k}".')
309 'type': 'FeatureCollection',
310 'features': json_features,
311 'properties': properties}
316 def create_wrmap_coordinates(coords):
319 result.append(f'{coord[1]:.6f} N {coord[0]:.6f} E')
320 return '\n'.join(result)
323 def create_wrmap(geojson: Dict) -> str:
324 """Creates a <wrmap> wikitext from geojson (as python types)."""
325 wrmap_xml = xml.etree.ElementTree.Element('wrmap')
326 wrmap_xml.text = '\n\n'
327 for k, v in geojson['properties'].items():
328 if k in ['lon', 'lat']:
329 wrmap_xml.attrib[k] = f'{v:.6f}'
331 wrmap_xml.attrib[k] = str(v)
333 assert geojson['type'] == 'FeatureCollection'
334 json_features = geojson['features']
335 last_json_feature = None
336 for json_feature in json_features:
337 feature_xml = xml.etree.ElementTree.SubElement(wrmap_xml, json_feature['properties']['type'])
338 geo = json_feature['geometry']
339 if geo['type'] == 'Point':
340 feature_xml.text = create_wrmap_coordinates([geo['coordinates']])
341 if last_json_feature is not None:
342 last_json_feature.tail = '\n'
344 if last_json_feature is not None:
345 last_json_feature.tail = '\n\n'
346 feature_xml.text = '\n' + create_wrmap_coordinates(geo['coordinates']) + '\n'
347 last_json_feature = feature_xml
348 feature_xml.attrib = json_feature['properties'].copy()
349 del feature_xml.attrib['type']
351 if last_json_feature is not None:
352 last_json_feature.tail = '\n\n'
353 return xml.etree.ElementTree.tostring(wrmap_xml, encoding='utf-8').decode('utf-8')
356 def german_bool(value: Union[bool, jinja2.Undefined]) -> Union[str, jinja2.Undefined]:
357 if jinja2.is_undefined(value):
359 return wrpylib.wrvalidators.bool_german_to_str(value)
362 def prepend(value: str, before: str) -> str:
363 return before + value
366 def is_vvv_ext_id(ext_id: str) -> bool:
367 return re.match(r'^48\d{5}00$', ext_id) is not None
371 def create_wrmap(self, geojson: Dict) -> str:
372 return create_wrmap(geojson)
374 def json_position(self, value: dict) -> str:
375 lon_lat = LonLat(value['longitude'], value['latitude'])
376 return lonlat_to_str(lon_lat)
378 def json_pos_ele_position(self, value: dict) -> str:
379 pos = value.get('position')
382 return self.json_position(pos)
384 def json_pos_ele_elevation(self, value: dict) -> str:
385 return value.get('elevation', '')
387 def json_wr_page(self, value: dict) -> str:
388 return str(Wikilink(value['title'], value.get('text')))
390 def list_template(self, name: str, value: List[str]) -> str:
391 return str(wrpylib.mwmarkup.create_template(name, value))
393 def key_value_template(self, name: str, value: Dict[str, Any], keep_empty: bool = False) -> str:
394 value = {k: str(v) for k, v in value.items()
395 if keep_empty or (v is not None and not isinstance(v, jinja2.Undefined) and str(v).strip() != '')}
396 return str(wrpylib.mwmarkup.create_template(name, [], value))
398 def json_template(self, value) -> str:
401 for p in value.get('parameter', []):
402 v = p.get('value', '')
407 return str(wrpylib.mwmarkup.create_template(value['name'], args, kwargs))
409 def public_transport_massage_stops(self, origins: List[str], directions: List[str]) -> \
410 Tuple[List[str], List[str], List[str]]:
411 """Takes public transport stops from origins and directions list and returns a tuple of unique sorted
412 lists of origin only, direction only, and both."""
413 origins = set(origins)
414 directions = set(directions)
415 both = origins.intersection(directions)
416 origins_only = origins.difference(both)
417 directions_only = directions.difference(both)
418 return sorted(origins_only), sorted(directions_only), sorted(both)
421 def create_sledrun_wiki(sledrun_json: Dict, map_json: Optional[Dict], impressions_title: Optional[str] = None) -> str:
422 env = jinja2.Environment(
423 loader=jinja2.PackageLoader("wrpylib"),
424 autoescape=jinja2.select_autoescape(),
426 env.filters["german_bool"] = german_bool
427 env.filters["prepend"] = prepend
428 env.tests["vvv_ext_id"] = is_vvv_ext_id
429 template = env.get_template("sledrun_wikitext.txt")
431 def position_to_lon_lat(value: Optional[dict]) -> Optional[LonLat]:
432 if value is not None:
433 lon = value.get('longitude')
434 lat = value.get('latitude')
435 if lon is not None and lat is not None:
436 return LonLat(lon, lat)
439 def position_ele_to_lon_lat(value: Optional[dict]) -> Optional[LonLat]:
440 if value is not None:
441 return position_to_lon_lat(value.get("position"))
444 def position_ele_to_ele(value: Optional[dict]) -> Optional[int]:
445 if value is not None:
446 ele = value.get('elevation')
451 def aufstiegshilfe() -> Optional[List[Tuple[str, Optional[str]]]]:
452 ws = sledrun_json.get('walkup_supports')
455 return [(w['type'], w.get('note')) for w in ws]
457 def beleuchtungstage(sledrun_json: Dict) -> Tuple[Optional[int], Optional[str]]:
458 weekdays_count = sledrun_json.get('nightlight_weekdays_count')
459 note = sledrun_json.get('nightlight_weekdays_note')
460 weekdays = sledrun_json.get('nightlight_weekdays')
461 if weekdays is not None:
462 assert isinstance(weekdays, list)
463 if weekdays_count is None:
464 weekdays_count = len(weekdays)
466 note = ', '.join(w[:2] for w in weekdays)
467 return weekdays_count, note
469 def rodelverleih() -> Optional[List[Tuple[str, Optional[str]]]]:
470 v = sledrun_json.get('sled_rental')
472 d = sledrun_json.get('sled_rental_direct')
474 return [("Ja", None)]
481 c = c.replace(';', ',')
483 link = x.get('weblink')
485 n = Jinja2Tools().json_wr_page(p)
486 if n is None and link is not None:
492 def cachet() -> Optional[List]:
493 v = sledrun_json.get('cachet')
499 def webauskunft() -> Tuple[Optional[bool], Optional[str]]:
500 info_web = sledrun_json.get('info_web')
503 if len(info_web) == 0:
505 return True, info_web[0]['url']
507 def telefonauskunft() -> Optional[List[Tuple[str, str]]]:
508 info_phone = sledrun_json.get('info_phone')
509 if info_phone is None:
511 return [(pc['phone'], pc['name']) for pc in info_phone]
513 def betreiber() -> str:
514 has_operator = sledrun_json.get('has_operator')
515 if has_operator is None:
516 return sledrun_json.get('operator')
518 return sledrun_json.get('operator')
521 sledrun_rbb_json = collections.OrderedDict([
522 ('Position', position_to_lon_lat(sledrun_json.get('position'))),
523 ('Position oben', position_ele_to_lon_lat(sledrun_json.get('top'))),
524 ('Höhe oben', position_ele_to_ele(sledrun_json.get('top'))),
525 ('Position unten', position_ele_to_lon_lat(sledrun_json.get('bottom'))),
526 ('Höhe unten', position_ele_to_ele(sledrun_json.get('bottom'))),
527 ('Länge', sledrun_json.get('length')),
528 ('Schwierigkeit', opt_difficulty_german_from_str(sledrun_json.get('difficulty', ''))),
529 ('Lawinen', opt_avalanches_german_from_str(sledrun_json.get('avalanches', ''))),
530 ('Betreiber', (sledrun_json.get('has_operator', True if 'operator' in sledrun_json else None),
531 sledrun_json.get('operator'))),
532 ('Öffentliche Anreise', opt_public_transport_german_from_str(sledrun_json.get('public_transport', ''))),
533 ('Aufstieg möglich', sledrun_json.get('walkup_possible')),
534 ('Aufstieg getrennt', (
535 tristate_german_from_str(sledrun_json['walkup_separate']) if 'walkup_separate' in sledrun_json else None,
536 sledrun_json.get('walkup_note'))),
537 ('Gehzeit', sledrun_json.get('walkup_time')),
538 ('Aufstiegshilfe', aufstiegshilfe()),
539 ('Beleuchtungsanlage', (opt_tristate_german_from_str(sledrun_json.get('nightlight_possible', '')),
540 sledrun_json.get('nightlight_possible_note'))),
541 ('Beleuchtungstage', beleuchtungstage(sledrun_json)),
542 ('Rodelverleih', rodelverleih()),
543 ('Gütesiegel', cachet()),
544 ('Webauskunft', webauskunft()),
545 ('Telefonauskunft', telefonauskunft()),
546 ('Bild', sledrun_json.get('image')),
547 ('In Übersichtskarte', sledrun_json.get('show_in_overview')),
548 ('Forumid', sledrun_json.get('forum_id'))
551 if map_json is not None:
552 if not map_json['properties'].get('simplified', False):
553 simplify_ways(map_json, DEFAULT_MAX_DIST_ERROR_M)
554 map_json['properties'].pop('simplified')
556 rodelbahnbox = rodelbahnbox_to_str(sledrun_rbb_json)
558 text = template.render(sledrun_json=sledrun_json,
559 rodelbahnbox=rodelbahnbox,
560 map_json=map_json, impressions_title=impressions_title,
561 h=Jinja2Tools(), **sledrun_json)
562 return strip_eol(text)