Skip to content

Commit

Permalink
Fix stripping for XMLObjectifyParser. Closes #29.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Dygalo committed Oct 20, 2015
1 parent 521febf commit 3d43e51
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,11 @@
History
-------

0.5.2 - 20.10.2015
----------------

* Fix incorrect stripping in XMLObjectifyParser (#29).

0.5.1 - 20.10.2015
----------------

Expand Down
17 changes: 16 additions & 1 deletion pyanyapi/interfaces.py
Expand Up @@ -182,10 +182,25 @@ def __getattribute__(self, item):
if item == '_parsed_content':
raise
try:
return self.maybe_strip(self.parsed_content.__getattribute__(item).text)
return self.maybe_strip(self.parsed_content.__getattribute__(item))
except AttributeError:
return None

def _strip_object(self, obj):
for key, value in obj.__dict__.items():
if isinstance(value, objectify.StringElement):
if value.text is not None:
setattr(obj, key, value.text.strip())
elif isinstance(value, objectify.ObjectifiedElement):
self._strip_object(value)

def maybe_strip(self, value):
if self.strip and isinstance(value, objectify.ObjectifiedElement):
self._strip_object(value)
if self.strip and isinstance(value, objectify.StringElement) and value.text is not None:
return value.text.strip()
return value


class DictInterface(BaseInterface):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -54,7 +54,7 @@ def run_tests(self):
setup(
name='pyanyapi',
url='https://github.com/Stranger6667/pyanyapi',
version='0.5.1',
version='0.5.2',
packages=['pyanyapi'],
license='MIT',
author='Dmitry Dygalo',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_parsers.py
Expand Up @@ -29,7 +29,7 @@
@lxml_is_supported
def test_xml_objectify_parser():
parsed = XMLObjectifyParser().parse('<xml><test>123</test></xml>')
assert parsed.test == '123'
assert parsed.test == 123
assert parsed.not_existing is None


Expand Down
20 changes: 17 additions & 3 deletions tests/test_strip.py
Expand Up @@ -5,7 +5,11 @@
JSON_CONTENT = '{"container":" 1 "}'
AJAX_CONTENT = '{"content": "<p> Pcontent </p>"}'
XML_CONTENT = '<p> Pcontent </p>'
OBJECTIFY_CONTENT = '<xml><test> abc </test></xml>'
OBJECTIFY_CONTENT = '''<xml>
<Messages><Message> abc </Message></Messages>
<test> bcd </test>
<first><second><third> inside </third></second></first>
</xml>'''


def test_strip_regexp_parser():
Expand Down Expand Up @@ -44,7 +48,17 @@ def test_class_override():
assert CustomParser(strip=False).parse(' 1 ').all == ' 1 '


@lxml_is_supported
def test_objectify_strip_default():
default = XMLObjectifyParser().parse(OBJECTIFY_CONTENT)
assert default.Messages.Message == ' abc '
assert default.test == ' bcd '
assert default.first.second.third == ' inside '


@lxml_is_supported
def test_objectify_strip():
assert XMLObjectifyParser().parse(OBJECTIFY_CONTENT).test == ' abc '
assert XMLObjectifyParser(strip=True).parse(OBJECTIFY_CONTENT).test == 'abc'
with_strip = XMLObjectifyParser(strip=True).parse(OBJECTIFY_CONTENT)
assert with_strip.Messages.Message == 'abc'
assert with_strip.test == 'bcd'
assert with_strip.first.second.third == 'inside'

0 comments on commit 3d43e51

Please sign in to comment.