From 1c074343f0b992106684b4696593b0da60ff3a7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomi=20Weckstr=C3=B6m?= Date: Thu, 12 Sep 2019 15:25:22 +0300 Subject: [PATCH] Support for pretty xml dump added to dumps function. If argument "pretty=True" given to dumps(), then out is based on doc.toprettyxml(). By default pretty argument is False to have backwars compatible function API. --- simplexml/core.py | 4 +++- tests/test_core.py | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/simplexml/core.py b/simplexml/core.py index de8af95..2958f28 100644 --- a/simplexml/core.py +++ b/simplexml/core.py @@ -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] @@ -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): diff --git a/tests/test_core.py b/tests/test_core.py index d0fc2fa..45ed7fa 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -108,3 +108,10 @@ def test_can_dumps_with_first_node_list(): response = simplexml.dumps(sometag) assert 'Should Be NomeShould Be Nome' 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\n\t\n\t\tShould Be Nome\n\t\n\t\n\t\tShould Be Nome\n\t\n\n' in response \ No newline at end of file