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

add partial attributes support for simplexml.loads #5

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ nosetests.xml
.mr.developer.cfg
.project
.pydevproject
.idea
22 changes: 19 additions & 3 deletions simplexml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ def isNodeList(elem):
return False
return True

def dict_from_attributes(attributes, nodeValue):
if len(attributes.keys()):
dic = {
'_attrs' : {},
}
for a, v in attributes.items():
dic['_attrs'][a] = v
if nodeValue:
dic['_value'] = nodeValue
return dic
else:
return nodeValue


def dict_from_element(element, dic):

if element.hasChildNodes():
Expand All @@ -99,19 +113,21 @@ def dict_from_element(element, dic):
else:
for node in element.childNodes:
if node.nodeType == node.TEXT_NODE:
dic[element.nodeName] = node.nodeValue
dic[element.nodeName] = dict_from_attributes(element.attributes, node.nodeValue)
elif node.nodeType == node.CDATA_SECTION_NODE:
dic = node.nodeValue
dic = dict_from_attributes(element.attributes, node.nodeValue)
else:
if node.hasChildNodes and len(node.childNodes) == 1 and node.childNodes[0].nodeType == node.TEXT_NODE:
dic[node.nodeName] = node.childNodes[0].nodeValue
dic[node.nodeName] = dict_from_attributes(node.attributes, node.childNodes[0].nodeValue)
else:
if node.nodeName in dic:
if type(dic[node.nodeName]) != type([]):
dic[node.nodeName] = [dic[node.nodeName]]
dic[node.nodeName].append(dict_from_element(node, {}))
else:
dic[node.nodeName] = dict_from_element(node, {})
elif len(element.attributes.keys()):
dic[element.nodeName] = dict_from_attributes(element.attributes, None)

return dic

Expand Down
20 changes: 16 additions & 4 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ def test_can_loads():
assert type(response['someTag']['itens']) == list
assert response['someTag']['itens'][0]['type'] == 'Should Be Type item1'

def test_can_loads_attributes():

docxml = u'<?xml version="1.0" ?><tags><tag attr="should be tag value"/><tag attr="value2">value3</tag></tags>'
response = simplexml.loads(docxml)

assert type(response['tags']) == list
assert type(response['tags'][0]) == dict

assert response['tags'][0]['tag']['_attrs']['attr'] == 'should be tag value'
assert response['tags'][1]['tag']['_attrs']['attr'] == 'value2'
assert response['tags'][1]['tag']['_value'] == 'value3'

final_docxml = simplexml.dumps(response)
assert docxml == final_docxml

def test_can_loads_list_plural():

Expand Down Expand Up @@ -52,7 +66,6 @@ def test_can_dumps():
assert '<name>Should be name</name>' in response
assert '<child><id>90</id></child>' in response


def test_can_dumps_diff_cdata():

sometag = {'someTag': {'value': 'hello world', 'scaped': 'Should Be <b>bold</b>'}}
Expand Down Expand Up @@ -86,10 +99,9 @@ def test_can_dumps_with_list_non_plural():

def test_can_dumps_with_list_non_plural_with_attrs():

sometag = {'someTag': {'name': 'Should be name', 'child': {'id': 90}, 'item': [{'_attrs': {'attr': 'value'}, 'type': 'Should Be Type item1'}, {'type': 'Should Be Type item2'}]}}
sometag = {'someTag': {'name': 'Should be name', 'child': {'id': 90}, 'item': [{'_attrs': {'attr': 'value'}, '_value':'item value'}, {'type': 'Should Be Type item2'}]}}
response = simplexml.dumps(sometag)

assert '<item attr="value"><type>Should Be Type item1</type></item><item><type>Should Be Type item2</type></item>' in response
assert '<item attr="value">item value</item><item><type>Should Be Type item2</type></item>' in response


def test_can_dumps_with_node_value():
Expand Down