Skip to content
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

Support for pretty xml dump added to dumps function. #8

Open
wants to merge 1 commit into
base: master
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
4 changes: 3 additions & 1 deletion simplexml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def dict_from_element(element, dic):

return dic

def dumps(data):
def dumps(data, pretty=False):

data_items = [(key, values) for key, values in data.items()]
rootName, rootValue = data_items[0]
Expand All @@ -130,6 +130,8 @@ def dumps(data):

element_from_dict(document, rootNode, rootValue)

if pretty == True:
return document.toprettyxml()
return document.toxml()

def loads(data):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,10 @@ def test_can_dumps_with_first_node_list():
response = simplexml.dumps(sometag)

assert '<someTags><someTag><nome>Should Be Nome</nome></someTag><someTag><nome>Should Be Nome</nome></someTag></someTags>' in response

def test_can_dumps_with_pretty():

sometag = {'someTags': [{'someTag': {'nome': 'Should Be Nome'}}, {'someTag': {'nome': 'Should Be Nome'}}]}
response = simplexml.dumps(sometag, pretty=True)

assert '\n<someTags>\n\t<someTag>\n\t\t<nome>Should Be Nome</nome>\n\t</someTag>\n\t<someTag>\n\t\t<nome>Should Be Nome</nome>\n\t</someTag>\n</someTags>\n' in response