#!/usr/bin/python3.4
-# -*- coding: utf-8 -*-
# $Id$
# $HeadURL$
"""
import re
from collections import OrderedDict, namedtuple
-import mwparserfromhell
+import mwparserfromhell # https://github.com/earwig/mwparserfromhell
from wrpylib.mwmarkup import format_template_table
# -------------------------------
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)
# ---------------------
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)
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
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))
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):