-#!/usr/bin/python2.6
+#!/usr/bin/python2.7
# -*- coding: iso-8859-15 -*-
# $Id$
# $HeadURL$
# match Rodelbahnbox
start, end = wrpylib.mwmarkup.find_template(wikitext, u'Rodelbahnbox')
if start is None: raise formencode.Invalid(u"Rodelbahnbox nicht gefunden", wikitext, None)
- template_name, properties = wrpylib.mwmarkup.split_template(wikitext[start:end])
+ template_title, properties = wrpylib.mwmarkup.split_template(wikitext[start:end])
# process properties
for key, value in properties.iteritems():
elif key == u'Lawinen': sledrun.avalanches = _conv(wrpylib.wrvalidators.GermanAvalanches().to_python, value, key) # 'kaum'
elif key == u'Betreiber': sledrun.operator = _conv(wrpylib.wrvalidators.UnicodeNone().to_python, value, key) # 'Max Mustermann'
elif key == u'Öffentliche Anreise': sledrun.public_transport = _conv(wrpylib.wrvalidators.GermanPublicTransport().to_python, value, key) # 'Mittelmäßig'
- elif key == u'Gehzeit': sledrun.walkup_time = _conv(wrpylib.wrvalidators.UnsignedNone().to_python, value, key) # 90
elif key == u'Aufstieg möglich': sledrun.walkup_possible = _conv(wrpylib.wrvalidators.GermanBoolNone().to_python, value, key) # 'Ja'
elif key == u'Aufstieg getrennt': sledrun.walkup_separate, sledrun.walkup_separate_comment = _conv(wrpylib.wrvalidators.GermanTristateFloatComment().to_python, value, key) # 'Ja'
+ elif key == u'Gehzeit': sledrun.walkup_time = _conv(wrpylib.wrvalidators.UnsignedNone().to_python, value, key) # 90
elif key == u'Aufstiegshilfe': sledrun.lift, sledrun.lift_details = _conv(wrpylib.wrvalidators.GermanLift().to_python, value, key) # 'Gondel (unterer Teil)'
elif key == u'Beleuchtungsanlage': sledrun.night_light, sledrun.night_light_comment = _conv(wrpylib.wrvalidators.GermanTristateFloatComment().to_python, value, key)
elif key == u'Beleuchtungstage': sledrun.night_light_days, sledrun.night_light_days_comment = _conv(wrpylib.wrvalidators.UnsignedCommentNone(7).to_python, value, key) # '3 (Montag, Mittwoch, Freitag)'
if version == '1.4':
keys.append(u'Aufstieg möglich')
values.append(wrpylib.wrvalidators.GermanBoolNone().from_python(sledrun.walkup_possible))
- keys.append(u'Gehzeit')
- values.append(wrpylib.wrvalidators.UnsignedNone().from_python(sledrun.walkup_time))
keys.append(u'Aufstieg getrennt')
values.append(wrpylib.wrvalidators.GermanTristateFloatComment().from_python((sledrun.walkup_separate, sledrun.walkup_separate_comment)))
+ keys.append(u'Gehzeit')
+ values.append(wrpylib.wrvalidators.UnsignedNone().from_python(sledrun.walkup_time))
keys.append(u'Aufstiegshilfe')
values.append(wrpylib.wrvalidators.GermanLift().from_python((sledrun.lift, sledrun.lift_details)))
keys.append(u'Beleuchtungsanlage')
# Match Gasthausbox
start, end = wrpylib.mwmarkup.find_template(wikitext, u'Gasthausbox')
if start is None: raise formencode.Invalid(u"No 'Gasthausbox' found", wikitext, None)
- template_name, properties = wrpylib.mwmarkup.split_template(wikitext[start:end])
+ template_title, properties = wrpylib.mwmarkup.split_template(wikitext[start:end])
# Process properties
for key, value in properties.iteritems():
elif key == u'Rodelverleih': inn.sled_rental, inn.sled_rental_comment = _conv(wrpylib.wrvalidators.BoolUnicodeTupleValidator().to_python, value, key)
elif key == u'Handyempfang': inn.mobile_provider = _conv(wrpylib.wrvalidators.ValueCommentListNeinLoopNone().to_python, value, key)
elif key == u'Homepage': inn.homepage = _conv(wrpylib.wrvalidators.UrlNeinNone().to_python, value, key)
- elif key == u'E-Mail': inn.email_list = _conv(wrpylib.wrvalidators.EmailCommentListNeinLoopNone().to_python, value, key)
+ elif key == u'E-Mail': inn.email_list = _conv(wrpylib.wrvalidators.EmailCommentListNeinLoopNone(allow_masked_email=True).to_python, value, key)
elif key == u'Telefon': inn.phone_list = _conv(wrpylib.wrvalidators.PhoneCommentListNeinLoopNone(comments_are_optional=True).to_python, value, key)
elif key == u'Bild': inn.image = _conv(wrpylib.wrvalidators.UnicodeNone().to_python, value, key)
elif key == u'Rodelbahnen': inn.sledding_list = _conv(wrpylib.wrvalidators.WikiPageListLoopNone().to_python, value, key)
keys.append(u'Homepage')
values.append(wrpylib.wrvalidators.UrlNeinNone().from_python(inn.homepage))
keys.append(u'E-Mail')
- values.append(wrpylib.wrvalidators.EmailCommentListNeinLoopNone().from_python(inn.email_list))
+ values.append(wrpylib.wrvalidators.EmailCommentListNeinLoopNone(allow_masked_email=True).from_python(inn.email_list))
keys.append(u'Telefon')
values.append(wrpylib.wrvalidators.PhoneCommentListNeinLoopNone(comments_are_optional=True).from_python(inn.phone_list))
keys.append(u'Bild')
return wrpylib.mwmarkup.create_template(u'Gasthausbox', [], keys, values, True)
+def find_template_latlon_ele(wikitext, template_title):
+ """Finds the first occurance of the '{{template_title|47.076207 N 11.453553 E|1890}}' template
+ and returns the tuple (start, end, lat, lon, ele) or (None, None, None, None, None) if the
+ template was not found. If the template has no valid format, an exception is thrown."""
+ start, end = wrpylib.mwmarkup.find_template(wikitext, template_title)
+ if start is None: return (None,) * 5
+ title, params = wrpylib.mwmarkup.split_template(wikitext[start:end])
+ lat, lon = wrpylib.wrvalidators.GeoNone().to_python(params[u'1'].strip())
+ ele = wrpylib.wrvalidators.UnsignedNone().to_python(params[u'2'].strip())
+ return start, end, lat, lon, ele
+
+
+def create_template_latlon_ele(template_title, lat, lon, ele):
+ geo = wrpylib.wrvalidators.GeoNone().from_python((lat, lon))
+ if len(geo) == 0: geo = u' '
+ ele = wrpylib.wrvalidators.UnsignedNone().from_python(ele)
+ if len(ele) == 0: ele = u' '
+ return wrpylib.mwmarkup.create_template(template_title, [geo, ele])
+
+
+def find_template_PositionOben(wikitext):
+ """Same as find_template_latlon_ele with template '{{Position oben|47.076207 N 11.453553 E|1890}}'"""
+ return find_template_latlon_ele(wikitext, u'Position oben')
+
+
+def create_template_PositionOben(lat, lon, ele):
+ return create_template_latlon_ele(u'Position, oben', lat, lon, ele)
+
+
+def find_template_PositionUnten(wikitext):
+ """Same as find_template_latlon_ele with template '{{Position unten|47.076207 N 11.453553 E|1890}}'"""
+ return find_template_latlon_ele(wikitext, u'Position unten')
+
+
+def find_template_unsigned(wikitext, template_title):
+ """Finds the first occurance of the '{{template_title|1890}}' template
+ and returns the tuple (start, end, unsigned_value) or (None, None, None) if the
+ template was not found. If the template has no valid format, an exception is thrown."""
+ start, end = wrpylib.mwmarkup.find_template(wikitext, template_title)
+ if start is None: return (None,) * 3
+ title, params = wrpylib.mwmarkup.split_template(wikitext[start:end])
+ unsigned_value = wrpylib.wrvalidators.UnsignedNone().to_python(params[u'1'].strip())
+ return start, end, unsigned_value
+
+
+def create_template_unsigned(template_title, unsigned):
+ unsigned = wrpylib.wrvalidators.UnsignedNone().from_python(unsigned)
+ if len(unsigned) == 0: unsigned = u' '
+ return wrpylib.mwmarkup.create_template(template_title, [unsigned])
+
+
+def find_template_Hoehenunterschied(wikitext):
+ """Same as find_template_unsigned with template '{{Höhenunterschied|350}}'"""
+ return find_template_unsigned(wikitext, u'Höhenunterschied')
+
+
+def create_template_Hoehenunterschied(ele_diff):
+ return create_template_unsigned(u'Höhenunterschied', ele_diff)
+
+
+def find_template_Bahnlaenge(wikitext):
+ """Same as find_template_unsigned with template '{{Bahnlänge|4500}}'"""
+ return find_template_unsigned(wikitext, u'Bahnlänge')
+
+
+def create_template_Bahnlaenge(length):
+ return create_template_unsigned(u'Bahnlänge', length)
+
+
+def find_template_Gehzeit(wikitext):
+ """Same as find_template_unsigned with template '{{Gehzeit|60}}'"""
+ return find_template_unsigned(wikitext, u'Gehzeit')
+
+
+def create_template_Gehzeit(walkup_time):
+ return create_template_unsigned(u'Gehzeit', walkup_time)
+
+
+def find_template_Forumlink(wikitext):
+ """Same as find_template_unsigned with template '{{Forumlink|26}}'"""
+ start, end = wrpylib.mwmarkup.find_template(wikitext, u'Forumlink')
+ if start is None: return (None,) * 3
+ title, params = wrpylib.mwmarkup.split_template(wikitext[start:end])
+ forumid = params[u'1'].strip()
+ if forumid == u'<nummer einfügen>': unsigned_value = None
+ else: unsigned_value = wrpylib.wrvalidators.UnsignedNone().to_python(forumid)
+ return start, end, unsigned_value
+ # return find_template_unsigned(wikitext, u'Forumlink')
+
+
+def find_template_Parkplatz(wikitext):
+ """Same as find_template_latlon_ele with template '{{Parkplatz|47.076207 N 11.453553 E|1890}}'"""
+ return find_template_latlon_ele(wikitext, u'Parkplatz')
+
+
+def find_template_Haltestelle(wikitext):
+ """Finds the first occurance of the '{{Haltestelle|Ortsname|Haltestellenname|47.076207 N 11.453553 E|1890}}' template
+ and returns the tuple (start, end, city, stop, lat, lon, ele) or (None, None, None, None, None, None, None) if the
+ template was not found. If the template has no valid format, an exception is thrown."""
+ start, end = wrpylib.mwmarkup.find_template(wikitext, u'Haltestelle')
+ if start is None: return (None,) * 7
+ title, params = wrpylib.mwmarkup.split_template(wikitext[start:end])
+ city = wrpylib.wrvalidators.UnicodeNone().to_python(params[u'1'].strip())
+ stop = wrpylib.wrvalidators.UnicodeNone().to_python(params[u'2'].strip())
+ lat, lon = wrpylib.wrvalidators.GeoNone().to_python(params[u'3'].strip())
+ ele = wrpylib.wrvalidators.UnsignedNone().to_python(params[u'4'].strip())
+ return start, end, city, stop, lat, lon, ele
+
+
+def find_all_templates(wikitext, find_func):
+ """Returns a list of return values of find_func that searches for a template.
+ Example:
+ >>> find_all_tempaltes(wikitext, find_template_Haltestelle)
+ Returns an empty list if the template was not found at all.
+ """
+ results = []
+ result = find_func(wikitext)
+ start, end = result[:2]
+ while start is not None:
+ results.append(result)
+ result = find_func(wikitext[end:])
+ if result[0] is None:
+ start = None
+ else:
+ start = result[0] + end
+ end += result[1]
+ result = (start, end) + result[2:]
+ return results
+