]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blobdiff - wrpylib/wrvalidators.py
Parse cachet.
[philipp/winterrodeln/wrpylib.git] / wrpylib / wrvalidators.py
index f48bb30f5db1d03bf9549f5b7a3bd1bebe140747..3b0ae5f84156d130f0ba4aa916a44ebf85e7c9c4 100644 (file)
@@ -15,6 +15,7 @@ from email.errors import HeaderParseError
 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
 
@@ -693,7 +694,7 @@ def single_cachet_german_to_str(value: Tuple[str, str, str]) -> 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' => []
@@ -763,7 +764,7 @@ class ValueErrorList(ValueError):
     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()
@@ -785,14 +786,14 @@ def wikibox_from_template(template, converter_dict):
     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:
@@ -802,12 +803,12 @@ def template_from_str(value, name):
     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))
 
 
@@ -826,7 +827,7 @@ RODELBAHNBOX_DICT = OrderedDict([
     ('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'
@@ -844,21 +845,21 @@ RODELBAHNBOX_DICT = OrderedDict([
 ])
 
 
-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)
@@ -886,21 +887,21 @@ GASTHAUSBOX_DICT = OrderedDict([
     ('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)
@@ -909,7 +910,7 @@ def gasthausbox_to_str(value):
 # 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(')', '')