import re
import formencode
import wrpylib.wrvalidators
+import wrpylib.mwmarkup
def _conv(fnct, value, fieldname):
"""Internal function.
Like one of the to_xxx functions (e.g. to_bool), but adds the field name to the error message"""
try: return fnct(value)
- except formencode.Invalid as e: raise formencode.Invalid(u"Conversion error in field '%s': %s" % (fieldname, unicode_e(e)), e.value, e.state)
+ except formencode.Invalid as e: raise formencode.Invalid(u"Conversion error in field '%s': %s" % (fieldname, unicode(e)), e.value, e.state)
def rodelbahnbox_to_sledrun(wikitext, sledrun=None):
to a sledrun. sledrun may be an instance of WrSledrunCache or an "empty" class (object()) (default).
Raises a formencode.Invalid exception if the format is not OK or the Rodelbahnbox is not found.
:return: (start, end, sledrun) tuple of the Rodelbahnbox."""
- if sledrun is None: sledrun = object()
+ if sledrun is None:
+ class Sledrun(object): pass
+ sledrun = Sledrun()
# match Rodelbahnbox
start, end = wrpylib.mwmarkup.find_template(wikitext, u'Rodelbahnbox')
values.append(wrpylib.wrvalidators.GermanBoolNone().from_python(sledrun.show_in_overview))
keys.append(u'Forumid')
values.append(wrpylib.wrvalidators.UnsignedNeinNone().from_python(sledrun.forum_id))
- result = [u'{{Rodelbahnbox']
- for i in xrange(len(keys)): result.append(u'| %-20s = %s' % (keys[i], values[i]))
- result.append('}}')
- return '\n'.join(result)
+ return wrpylib.mwmarkup.create_template(u'Rodelbahnbox', [], keys, values, True, 20)
-
-def gasthausbox_to_inn(wikitext, inn):
+def gasthausbox_to_inn(wikitext, inn=None):
"""Converts a inn wiki page containing a {{Gasthausbox}} to an inn.
raises a formencode.Invalid exception if an error occurs.
- :return: (start, end) tuple."""
+ :return: (start, end, inn) tuple."""
+ if inn is None:
+ class Inn(object): pass
+ inn = Inn()
+
# Match Gasthausbox
- regexp = re.compile(u"\{\{(Gasthausbox[^\}]*)\}\}", re.DOTALL)
- match = regexp.search(wikitext)
- if not match:
- raise formencode.Invalid(u"No 'Gasthausbox' found")
- box = match.group(1)
-
- # Process Gashausbox
- for property in box.split('|'):
- property = property.strip()
- if property == u'Gasthausbox': continue
- key_value = property.split('=')
- if len(key_value) != 2:
- raise formencode.Invalid(u"Property '%s' has unexpected format" % key_value, wikitext, None)
- key = key_value[0].strip()
- value = key_value[1].strip()
- if key == u'Position': inn.position_latitude, inn.position_longitude = _conv(wrpylib.wrvalidators.GeoNone().to_python, value, key) # '47.583333 N 15.75 E'
+ 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])
+
+ # Process properties
+ for key, value in properties.iteritems():
+ if key == u'Position': inn.position_latitude, inn.position_longitude = _conv(wrpylib.wrvalidators.GeoNone().to_python, value, key) # '47.583333 N 15.75 E'
elif key == u'Höhe': inn.position_elevation = _conv(wrpylib.wrvalidators.UnsignedNone().to_python, value, key)
elif key == u'Betreiber': inn.operator = _conv(wrpylib.wrvalidators.UnicodeNone().to_python, value, key)
elif key == u'Sitzplätze': inn.seats = _conv(wrpylib.wrvalidators.UnsignedNone().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)
else: raise formencode.Invalid(u"Unbekannte Eigenschaft der Gasthausbox: '%s' (mit Wert '%s')" % (key, value), value, None)
- return match.span()
+ return start, end, inn
def inn_to_gasthausbox(inn):
keys.append(u'Rodelbahnen')
values.append(wrpylib.wrvalidators.WikiPageListLoopNone().from_python(inn.sledding_list))
result = [u'{{Gasthausbox']
- for i in xrange(len(keys)): result.append(u'| %-17s = %s' % (keys[i], values[i]))
- result.append('}}\n')
- return '\n'.join(result)
+ return wrpylib.mwmarkup.create_template(u'Gasthausbox', [], keys, values, True)