#!/usr/bin/python2.6
# -*- coding: iso-8859-15 -*-
-import wrpylib.wrmwmarkup
-import formencode
+import wrpylib.mwmarkup
-def test_rodelbahnbox_to_sledrun():
+def test_find_template():
wikitext = u'''== Allgemeines ==
{{Rodelbahnbox
| Position = 47.309820 N 9.986508 E
| Forumid = 72
}}
Die Rodelbahn zur Bergkristallhütte ist durchaus abwechslungsreich.'''
- class Sledrun(object): pass
- sledrun = Sledrun()
- wrpylib.wrmwmarkup.rodelbahnbox_to_sledrun(wikitext, sledrun)
- wrpylib.wrmwmarkup.sledrun_to_rodelbahnbox(sledrun, '1.3')
-
-
-
-def test_gasthausbox_to_inn():
- wikitext = u'''{{Gasthausbox
- | Position = 47.295549 N 9.986970 E
- | Höhe = 1250
- | Betreiber =
- | Sitzplätze =
- | Übernachtung =
- | Rauchfrei = Nein
- | Rodelverleih =
- | Handyempfang = A1; T-Mobile/Telering
- | Homepage = http://www.bergkristallhuette.com/
- | E-Mail = bergkristallhuette@gmx.at
- | Telefon = +43-664-1808482
- | Bild = Bergkritsallhütte 2009-02-07.JPG
- | Rodelbahnen = [[Bergkristallhütte]]
+ start, end = wrpylib.mwmarkup.find_template(wikitext, u'Rodelbahnbox')
+ assert start == wikitext.find(u'{{')
+ assert end == wikitext.find(u'}}')+2
+
+
+def test_split_template():
+ wikitext = u'''== Allgemeines ==
+ {{Rodelbahnbox
+ | Position = 47.309820 N 9.986508 E
+ | Position oben =
+ | Höhe oben = 1244
+ | Position unten =
+ | Höhe unten = 806
+ | Länge = 5045
+ | Schwierigkeit =
+ | Lawinen = gelegentlich
+ | Betreiber =
+ | Öffentliche Anreise = Ja
+ | Gehzeit = 105
+ | Aufstieg getrennt = Nein
+ | Aufstiegshilfe = Nein
+ | Beleuchtungsanlage = Nein
+ | Beleuchtungstage =
+ | Rodelverleih = Ja
+ | Gütesiegel =
+ | Webauskunft =
+ | Telefonauskunft = +43-664-1808482 (Bergkristallhütte)
+ | Bild = Rodelbahn Bergkristallhütte 2009-03-03.jpg
+ | In Übersichtskarte = Ja
+ | Forumid = 72
}}
- Die Bergkristallhütte ist Teil des Boden-Vorsäß.'''
- class Inn(object): pass
- inn = Inn()
- wrpylib.wrmwmarkup.gasthausbox_to_inn(wikitext, inn)
- wrpylib.wrmwmarkup.inn_to_gasthausbox(inn)
+ Die Rodelbahn zur Bergkristallhütte ist durchaus abwechslungsreich.'''
+ start, end = wrpylib.mwmarkup.find_template(wikitext, u'Rodelbahnbox')
+ template_title, parameters = wrpylib.mwmarkup.split_template(wikitext[start:end])
+ assert template_title == u'Rodelbahnbox'
+ assert len(parameters) == 22
+ assert parameters['Position'] == u'47.309820 N 9.986508 E'
+ assert parameters['Telefonauskunft'] == u'+43-664-1808482 (Bergkristallhütte)'
+ assert parameters['Schwierigkeit'] == u''
+
+
+def test_create_template():
+ wikitext = u'''{{Nicetemplate|Red|Bold|Position=Top|Alignment=Right}}'''
+ template_title, parameters = wrpylib.mwmarkup.split_template(wikitext)
+ print parameters
+ keys = [u'1', u'2', u'Position', u'Alignment']
+ values = [parameters[k] for k in keys]
+ wikitext_generated = wrpylib.mwmarkup.create_template(template_title, values[:2], keys[2:], values[2:])
+ wikitext_table = wrpylib.mwmarkup.create_template(template_title, values[:2], keys[2:], values[2:], True)
+ assert wikitext_generated == wikitext
+
#!/usr/bin/python2.6
# -*- coding: iso-8859-15 -*-
-import wrpylib.mwmarkup
+import wrpylib.wrmwmarkup
+import formencode
-def test_find_template():
+def test_rodelbahnbox_to_sledrun():
wikitext = u'''== Allgemeines ==
{{Rodelbahnbox
| Position = 47.309820 N 9.986508 E
| Forumid = 72
}}
Die Rodelbahn zur Bergkristallhütte ist durchaus abwechslungsreich.'''
- start, end = wrpylib.mwmarkup.find_template(wikitext, u'Rodelbahnbox')
- assert start == wikitext.find(u'{{')
- assert end == wikitext.find(u'}}')+2
+ start, end, sledrun = wrpylib.wrmwmarkup.rodelbahnbox_to_sledrun(wikitext)
+ wrpylib.wrmwmarkup.sledrun_to_rodelbahnbox(sledrun, '1.3')
-def test_split_template():
- wikitext = u'''== Allgemeines ==
- {{Rodelbahnbox
- | Position = 47.309820 N 9.986508 E
- | Position oben =
- | Höhe oben = 1244
- | Position unten =
- | Höhe unten = 806
- | Länge = 5045
- | Schwierigkeit =
- | Lawinen = gelegentlich
- | Betreiber =
- | Öffentliche Anreise = Ja
- | Gehzeit = 105
- | Aufstieg getrennt = Nein
- | Aufstiegshilfe = Nein
- | Beleuchtungsanlage = Nein
- | Beleuchtungstage =
- | Rodelverleih = Ja
- | Gütesiegel =
- | Webauskunft =
- | Telefonauskunft = +43-664-1808482 (Bergkristallhütte)
- | Bild = Rodelbahn Bergkristallhütte 2009-03-03.jpg
- | In Übersichtskarte = Ja
- | Forumid = 72
- }}
- Die Rodelbahn zur Bergkristallhütte ist durchaus abwechslungsreich.'''
- start, end = wrpylib.mwmarkup.find_template(wikitext, u'Rodelbahnbox')
- template_title, parameters = wrpylib.mwmarkup.split_template(wikitext[start:end])
- assert template_title == u'Rodelbahnbox'
- assert len(parameters) == 22
- assert parameters['Position'] == u'47.309820 N 9.986508 E'
- assert parameters['Telefonauskunft'] == u'+43-664-1808482 (Bergkristallhütte)'
- assert parameters['Schwierigkeit'] == u''
-
-def test_create_template():
- wikitext = u'''{{Nicetemplate|Red|Bold|Position=Top|Alignment=Right}}'''
- template_title, parameters = wrpylib.mwmarkup.split_template(wikitext)
- print parameters
- keys = [u'1', u'2', u'Position', u'Alignment']
- values = [parameters[k] for k in keys]
- wikitext_generated = wrpylib.mwmarkup.create_template(template_title, values[:2], keys[2:], values[2:])
- wikitext_table = wrpylib.mwmarkup.create_template(template_title, values[:2], keys[2:], values[2:], True)
- assert wikitext_generated == wikitext
+def test_gasthausbox_to_inn():
+ wikitext = u'''{{Gasthausbox
+ | Position = 47.295549 N 9.986970 E
+ | Höhe = 1250
+ | Betreiber =
+ | Sitzplätze =
+ | Übernachtung =
+ | Rauchfrei = Nein
+ | Rodelverleih =
+ | Handyempfang = A1; T-Mobile/Telering
+ | Homepage = http://www.bergkristallhuette.com/
+ | E-Mail = bergkristallhuette@gmx.at
+ | Telefon = +43-664-1808482
+ | Bild = Bergkritsallhütte 2009-02-07.JPG
+ | Rodelbahnen = [[Bergkristallhütte]]
+ }}
+ Die Bergkristallhütte ist Teil des Boden-Vorsäß.'''
+ start, end, inn = wrpylib.wrmwmarkup.gasthausbox_to_inn(wikitext)
+ wrpylib.wrmwmarkup.inn_to_gasthausbox(inn)
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)
-
-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)