Skip to content

Ensure recursive_dict works on legacy python. #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions rinse/tests/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Unit tests for rinse.util module."""

import unittest
import six
from lxml import etree

from ..util import recursive_dict


class TestRecursiveDict(unittest.TestCase):
def test_recursive_dict_function(self):
doc = etree.fromstring(six.b('''<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="https://example.com/soap/v3"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:authenticateResponse>
<return xsi:type="ns1:ApiAuthenticateResult">
<token xsi:type="xsd:string">1234567890</token>
<error xsi:type="ns1:ApiError">
<number xsi:type="xsd:int">0</number>
<description xsi:type="xsd:string">No Error</description>
</error>
</return>
</ns1:authenticateResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
'''))

recursive_dict(doc)
14 changes: 7 additions & 7 deletions rinse/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import defusedxml.lxml
import lxml.builder
from lxml import etree
import six

RINSE_DIR = os.path.dirname(__file__)
ENVELOPE_XSD = 'soap-1.1_envelope.xsd'
Expand Down Expand Up @@ -160,14 +161,13 @@ def recursive_dict(element):
for child
in element
)
if six.PY2:
kwargs = {'width': 10000}
else:
kwargs = {'compact': True, 'width': 10000}
return (
'{}{}'.format(
element.tag,
pprint.pformat(element.attrib, compact=True, width=10000),
),
dict(
map(recursive_dict, element)
) or element.text
'{}{}'.format(element.tag, pprint.pformat(element.attrib, **kwargs)),
dict(map(recursive_dict, element)) or element.text
)


Expand Down