]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Add some type annotations.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Sun, 28 Nov 2021 21:51:38 +0000 (22:51 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Sun, 28 Nov 2021 21:51:38 +0000 (22:51 +0100)
wrpylib/wrmwmarkup.py
wrpylib/wrvalidators.py

index 1c82c43fe9b76f65cdefade2b2f64c6b947536ae..5ef604c6f6f168b739aa9039b989c0d976adcd09 100644 (file)
@@ -149,14 +149,14 @@ def inn_to_gasthausbox(inn) -> collections.OrderedDict:
     return value
 
 
-def lonlat_ele_from_template(template) -> Tuple[LonLat, Optional[int]]:
+def lonlat_ele_from_template(template) -> Tuple[Optional[LonLat], Optional[int]]:
     """Template is a `mwparserfromhell.nodes.template.Template` instance. Returns (lonlat, ele)."""
     lonlat = opt_lonlat_from_str(template.params[0].strip())
     ele = opt_uint_from_str(template.params[1].strip())
     return lonlat, ele
 
 
-def latlon_ele_to_template(lonlat_ele, name) -> Template:
+def latlon_ele_to_template(lonlat_ele: Tuple[Optional[LonLat], Optional[int]], name: str) -> Template:
     lonlat, ele = lonlat_ele
     template = Template(name)
     template.add(1, opt_lonlat_to_str(lonlat))
index d48b2f229c4258383ae9f1ec2d0a4d43dd4f9c22..61b141568a29a4fc78af906086848463d18db1a7 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
 
@@ -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) -> mwparserfromhell.nodes.template.Template:
-    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))
 
 
@@ -844,12 +845,12 @@ 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)
 
 
@@ -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(')', '')