-#!/usr/bin/python3.4
-# $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.
import urllib.parse
import re
from collections import OrderedDict, namedtuple
+from email.errors import HeaderParseError
import mwparserfromhell # https://github.com/earwig/mwparserfromhell
# ---------------------
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)
An empty string is an error.
'[[Birgitzer Alm]]' => '[[Birgitzer Alm]]'
"""
- if re.match(r'\[\[[^\[\]]+\]\]$', value) is None:
+ if re.match(r'\[\[[^\[\]]+]]$', value) is None:
raise ValueError('No valid wiki page name "{}"'.format(value))
return 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
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)
def template_from_str(value, name):
wikicode = mwparserfromhell.parse(value)
- template_list = [template for template in wikicode.filter_templates(name) if template.name.strip() == name]
+ 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: