diff --git a/CHANGELOG.md b/CHANGELOG.md index 00314a0..9019a2c 100644 --- a/CHANGELOG.md +++ b/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 ---------------- diff --git a/pyanyapi/interfaces.py b/pyanyapi/interfaces.py index ef1acea..67b12e7 100644 --- a/pyanyapi/interfaces.py +++ b/pyanyapi/interfaces.py @@ -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): """ diff --git a/setup.py b/setup.py index c6dba22..7f4d773 100644 --- a/setup.py +++ b/setup.py @@ -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', diff --git a/tests/test_parsers.py b/tests/test_parsers.py index 63926d8..e13a167 100644 --- a/tests/test_parsers.py +++ b/tests/test_parsers.py @@ -29,7 +29,7 @@ @lxml_is_supported def test_xml_objectify_parser(): parsed = XMLObjectifyParser().parse('123') - assert parsed.test == '123' + assert parsed.test == 123 assert parsed.not_existing is None diff --git a/tests/test_strip.py b/tests/test_strip.py index c196366..6344131 100644 --- a/tests/test_strip.py +++ b/tests/test_strip.py @@ -5,7 +5,11 @@ JSON_CONTENT = '{"container":" 1 "}' AJAX_CONTENT = '{"content": "

Pcontent

"}' XML_CONTENT = '

Pcontent

' -OBJECTIFY_CONTENT = ' abc ' +OBJECTIFY_CONTENT = ''' + abc + bcd + inside +''' def test_strip_regexp_parser(): @@ -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'