Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Fix unicode errors in Python 2.7.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krienbühl committed May 29, 2015
1 parent 8d903fd commit edf4719
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
Unreleased
~~~~~~~~~~

- Fix unicode errors in Python 2.7.
[href]

0.2.2 (2015-05-29)
~~~~~~~~~~~~~~~~~~~

Expand Down
12 changes: 6 additions & 6 deletions onegov/form/parser/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,24 +577,24 @@ def translate_to_yaml(text):

# the top level are the fieldsets
if match(elements.fieldset_title, line):
yield '- "{}":'.format(line.lstrip('# ').rstrip())
yield u'- "{}":'.format(line.lstrip(u'# ').rstrip())
continue

# fields are nested lists of dictionaries
parse_result = try_parse(elements.single_line_fields, line)
if parse_result is not None:
yield '{indent}- "{identifier}": !{type} "{definition}"'.format(
yield u'{indent}- "{identifier}": !{type} "{definition}"'.format(
indent=' ' * (4 + (len(line) - len(line.lstrip()))),
type=parse_result.type,
identifier=line.split('=')[0].strip(),
definition=line.split('=')[1].strip()
identifier=line.split(u'=')[0].strip(),
definition=line.split(u'=')[1].strip()
)
continue

# checkboxes/radios come without identifier
parse_result = try_parse(elements.boxes, line)
if parse_result is not None:
yield '{indent}- !{type} "{definition}":'.format(
yield u'{indent}- !{type} "{definition}":'.format(
indent=' ' * (4 + (len(line) - len(line.lstrip()))),
type=parse_result.type,
definition=line.strip()
Expand All @@ -603,7 +603,7 @@ def translate_to_yaml(text):

# identifiers which are alone contain nested checkboxes/radios
if match(elements.identifier, line):
yield '{indent}- "{identifier}":'.format(
yield u'{indent}- "{identifier}":'.format(
indent=' ' * (4 + (len(line) - len(line.lstrip()))),
identifier=line.strip()
)
Expand Down
20 changes: 20 additions & 0 deletions onegov/form/tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import pytest

from onegov.form import errors
Expand Down Expand Up @@ -40,6 +41,25 @@ def test_parse_text():
'<textarea id="comment" name="comment" rows="8"></textarea>')


def test_unicode():
text = dedent(u"""
# Persönliche Informationen
Bürgerort = ___
Geschlecht =
( ) Männlich
( ) Weiblich
""")

form = parse_form(text)()

assert form.personliche_informationen_burgerort.label.text == u'Bürgerort'
assert u'Persönliche Informationen' == form.fieldsets[0].label
assert form.personliche_informationen_geschlecht.choices == [
(u'Männlich', u'Männlich'),
(u'Weiblich', u'Weiblich')
]


def test_parse_fieldsets():
text = dedent("""
# Name
Expand Down

0 comments on commit edf4719

Please sign in to comment.