]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blobdiff - wrpylib/wrvalidators.py
Implement DEM for South Tyrol.
[philipp/winterrodeln/wrpylib.git] / wrpylib / wrvalidators.py
index c73675069ab4b4d3f6d929ba897d66a830303705..a4fbdbf578ff3e142060f2d9499d9de37b3239fe 100644 (file)
@@ -1,7 +1,3 @@
-#!/usr/bin/python3.4
-# -*- coding: utf-8 -*-
-# $Id$
-# $HeadURL$
 """
 This module contains functions that convert winterrodeln specific strings (like geographic coordinates) from string
 to appropriate python types and the other way round.
@@ -15,6 +11,7 @@ import email.headerregistry
 import urllib.parse
 import re
 from collections import OrderedDict, namedtuple
+from email.errors import HeaderParseError
 
 import mwparserfromhell  # https://github.com/earwig/mwparserfromhell
 
@@ -305,7 +302,7 @@ opt_tristate_german_converter = FromToConverter(opt_tristate_german_from_str, op
 # -------------------------------
 
 def opt_tristate_german_comment_from_str(value):
-    """Ja, Nein or Vielleicht, optionally with comment in parenthesis."""
+    """Ja, Nein or Teilweise, optionally with comment in parenthesis."""
     return value_comment_from_str(value, opt_tristate_german_from_str, opt_str_from_str, True)
 
 
@@ -336,6 +333,14 @@ def url_to_str(value):
 # ---------------------
 
 def webauskunft_from_str(value):
+    """Converts a URL or 'Nein' to a tuple
+    'http://www.example.com/' -> (True, 'http://www.example.com/')
+    'Nein' -> (False, None)
+    '' -> (None, None)
+
+    :param value: URL or 'Nein'
+    :return: tuple
+    """
     return opt_no_german_from_str(value, url_from_str)
 
 
@@ -351,11 +356,11 @@ webauskunft_converter = FromToConverter(webauskunft_from_str, webauskunft_to_str
 
 def wikipage_from_str(value):
     """Validates wiki page name like '[[Birgitzer Alm]]'.
-    The page is not checked for existance.
+    The page is not checked for existence.
     An empty string is an error.
     '[[Birgitzer Alm]]' => '[[Birgitzer Alm]]'
     """
-    if not value.startswith('[[') or not value.endswith(']]'):
+    if re.match(r'\[\[[^\[\]]+]]$', value) is None:
         raise ValueError('No valid wiki page name "{}"'.format(value))
     return value
 
@@ -388,7 +393,7 @@ def email_from_str(value):
     """Takes an email address like 'office@example.com', checks it for correctness and returns it again as string."""
     try:
         email.headerregistry.Address(addr_spec=value)
-    except email.errors.HeaderParseError as e:
+    except HeaderParseError as e:
         raise ValueError('Invalid email address: {}'.format(value), e)
     return value
 
@@ -465,15 +470,15 @@ LonLat = namedtuple('LonLat', ['lon', 'lat'])
 lonlat_none = LonLat(None, None)
 
 
-def lonlat_from_str(value):
-    """Converts a winterrodeln geo string like '47.076207 N 11.453553 E' (being '<latitude> N <longitude> E'
-    to the LonLat(lon, lat) named  tupel."""
-    r = re.match('(\d+\.\d+) N (\d+\.\d+) E', value)
+def lonlat_from_str(value: str) -> LonLat:
+    """Converts a Winterrodeln geo string like '47.076207 N 11.453553 E' (being '<latitude> N <longitude> E'
+    to the LonLat(lon, lat) named  tuple."""
+    r = re.match(r'(\d+\.\d+) N (\d+\.\d+) E', value)
     if r is None: raise ValueError("Coordinates '{}' have not a format like '47.076207 N 11.453553 E'".format(value))
     return LonLat(float(r.groups()[1]), float(r.groups()[0]))
 
 
-def lonlat_to_str(value):
+def lonlat_to_str(value: LonLat) -> str:
     return '{:.6f} N {:.6f} E'.format(value.lat, value.lon)
 
 
@@ -700,8 +705,8 @@ def wikibox_to_template(value, name, converter_dict):
 
 def template_from_str(value, name):
     wikicode = mwparserfromhell.parse(value)
-    template_list = wikicode.filter_templates(name)
-    if len(name) == 0:
+    template_list = wikicode.filter_templates(recursive=False, matches=lambda t: t.name.strip() == name)
+    if len(template_list) == 0:
         raise ValueError('No "{}" template was found'.format(name))
     if len(template_list) > 1:
         raise ValueError('{} "{}" templates were found'.format(len(template_list), name))
@@ -756,7 +761,7 @@ def rodelbahnbox_from_template(template):
 
 
 def rodelbahnbox_to_template(value):
-    return wikibox_to_template(value, RODELBAHNBOX_DICT, RODELBAHNBOX_TEMPLATE_NAME)
+    return wikibox_to_template(value, RODELBAHNBOX_TEMPLATE_NAME, RODELBAHNBOX_DICT)
 
 
 def rodelbahnbox_from_str(value):