import re
from collections import OrderedDict, namedtuple
from email.errors import HeaderParseError
-from typing import Tuple, Optional, List, Callable, Union, TypeVar, Dict
+from typing import Tuple, Optional, List, Callable, Union, TypeVar, Dict, NamedTuple
import mwparserfromhell # https://github.com/earwig/mwparserfromhell
+from mwparserfromhell.nodes import Template
from wrpylib.mwmarkup import format_template_table
# longitude/latitude converter
# ----------------------------
-LonLat = namedtuple('LonLat', ['lon', 'lat'])
-
-
-lonlat_none = LonLat(None, None)
+class LonLat(NamedTuple):
+ lon: float
+ lat: float
def lonlat_from_str(value: str) -> LonLat:
return f'{value.lat:.6f} N {value.lon:.6f} E'
-def opt_lonlat_from_str(value: str) -> LonLat:
- return opt_from_str(value, lonlat_from_str, lonlat_none)
+def opt_lonlat_from_str(value: str) -> Optional[LonLat]:
+ return opt_from_str(value, lonlat_from_str, None)
-def opt_lonlat_to_str(value: LonLat) -> str:
- return opt_to_str(value, lonlat_to_str, lonlat_none)
+def opt_lonlat_to_str(value: Optional[LonLat]) -> str:
+ return opt_to_str(value, lonlat_to_str, None)
opt_lonlat_converter = FromToConverter(opt_lonlat_from_str, opt_lonlat_to_str)
return ' '.join(value)
-def cachet_german_from_str(value):
+def cachet_german_from_str(value) -> Optional[List[Tuple[str, str, str]]]:
"""Converts a "Gütesiegel":
'' => None
'Nein' => []
pass
-def wikibox_from_template(template, converter_dict):
+def wikibox_from_template(template: Template, converter_dict: dict) -> dict:
"""Returns an ordered dict."""
result = OrderedDict()
exceptions_dict = OrderedDict()
return result
-def wikibox_to_template(value, name, converter_dict):
- template = mwparserfromhell.nodes.template.Template(name)
+def wikibox_to_template(value: dict, name: str, converter_dict: dict) -> Template:
+ template = Template(name)
for key, converter in converter_dict.items():
template.add(key, converter.to_str(value[key]))
return template
-def template_from_str(value, name):
+def template_from_str(value: str, name: str) -> Template:
wikicode = mwparserfromhell.parse(value)
template_list = wikicode.filter_templates(recursive=False, matches=lambda t: t.name.strip() == name)
if len(template_list) == 0:
return template_list[0]
-def wikibox_from_str(value, name, converter_dict):
+def wikibox_from_str(value: str, name: str, converter_dict: dict) -> dict:
template = template_from_str(value, name)
return wikibox_from_template(template, converter_dict)
-def wikibox_to_str(value, name, converter_dict):
+def wikibox_to_str(value: dict, name: str, converter_dict: dict) -> str:
return str(wikibox_to_template(value, name, converter_dict))
('Länge', opt_uint_converter), # 3500
('Schwierigkeit', opt_difficulty_german_converter), # 'mittel'
('Lawinen', opt_avalanches_german_converter), # 'kaum'
- ('Betreiber', opt_str_converter), # 'Max Mustermann'
+ ('Betreiber', opt_no_or_str_converter), # 'Max Mustermann'
('Öffentliche Anreise', opt_public_transport_german_converter), # 'Mittelmäßig'
('Aufstieg möglich', opt_bool_german_converter), # 'Ja'
('Aufstieg getrennt', opt_tristate_german_comment_converter), # 'Ja'
])
-def rodelbahnbox_from_template(template):
+def rodelbahnbox_from_template(template: Template) -> dict:
"""Returns an ordered dict."""
return wikibox_from_template(template, RODELBAHNBOX_DICT)
-def rodelbahnbox_to_template(value):
+def rodelbahnbox_to_template(value: dict) -> Template:
return wikibox_to_template(value, RODELBAHNBOX_TEMPLATE_NAME, RODELBAHNBOX_DICT)
-def rodelbahnbox_from_str(value):
+def rodelbahnbox_from_str(value: str) -> Dict:
"""Returns an ordered dict."""
return wikibox_from_str(value, RODELBAHNBOX_TEMPLATE_NAME, RODELBAHNBOX_DICT)
-def rodelbahnbox_to_str(value):
+def rodelbahnbox_to_str(value: Dict) -> str:
template = rodelbahnbox_to_template(value)
format_template_table(template, 20)
return str(template)
('Rodelbahnen', opt_wikipage_enum_converter)])
-def gasthausbox_from_template(template):
+def gasthausbox_from_template(template: Template) -> dict:
"""Returns an ordered dict."""
return wikibox_from_template(template, GASTHAUSBOX_DICT)
-def gasthausbox_to_template(value):
+def gasthausbox_to_template(value: dict) -> Template:
return wikibox_to_template(value, GASTHAUSBOX_TEMPLATE_NAME, GASTHAUSBOX_DICT)
-def gasthausbox_from_str(value):
+def gasthausbox_from_str(value: str) -> dict:
"""Returns an ordered dict."""
return wikibox_from_str(value, GASTHAUSBOX_TEMPLATE_NAME, GASTHAUSBOX_DICT)
-def gasthausbox_to_str(value):
+def gasthausbox_to_str(value: dict) -> str:
template = gasthausbox_to_template(value)
format_template_table(template, 17)
return str(template)
# Helper function to make page title pretty
# -----------------------------------------
-def sledrun_page_title_to_pretty_url(page_title):
+def sledrun_page_title_to_pretty_url(page_title: str) -> str:
"""Converts a page_title from the page_title column of wrsledruncache to name_url.
name_url is not used by MediaWiki but by new applications like wrweb."""
return page_title.lower().replace(' ', '-').replace('_', '-').replace('(', '').replace(')', '')