Skip to content

Commit 3670687

Browse files
author
Arzaroth Lekva
committed
tranfering attributes to dictnodes
1 parent 45de0a4 commit 3670687

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

rapidxml/c_ext/src/document_object.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <common.h>
2020

2121
static void rapidxml_DocumentObject_dealloc(rapidxml_DocumentObject* self) {
22-
delete self->base.base.underlying_obj;
22+
delete self->base.base.document;
2323
Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
2424
}
2525

rapidxml/c_ext/src/node_object.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,6 @@ static PyObject* rapidxml_NodeObject_remove_all_attributes(rapidxml_NodeObject*
404404
static PyObject* rapidxml_NodeObject_unparse(rapidxml_NodeObject* self,
405405
PyObject* args,
406406
PyObject* kwds) {
407-
int pretty = 0;
408407
PyObject* pretty_obj = NULL;
409408
std::string xml;
410409
char kw_pretty[] = "pretty";

rapidxml/rapidxml.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,49 @@
99
import _rapidxml
1010

1111
class DictNodeIterator(object):
12-
def __init__(self, iterable):
12+
def __init__(self,
13+
iterable,
14+
attribute_prefix,
15+
cdata_key,
16+
always_aslist):
1317
self._iter = iterable
18+
self.attribute_prefix = attribute_prefix
19+
self.cdata_key = cdata_key
20+
self.always_aslist = always_aslist
1421

1522
def __next__(self):
1623
return self.next()
1724

1825
def next(self):
19-
return DictNode().copy(next(self._iter))
26+
return DictNode(self.attribute_prefix,
27+
self.cdata_key,
28+
self.always_aslist).copy(next(self._iter))
2029

2130
def __iter__(self):
2231
return self
2332

2433

2534
class DictNode(_rapidxml.Node):
26-
def __init__(self, attribute_prefix='@', cdata_key='#text'):
35+
def __init__(self, attribute_prefix='@', cdata_key='#text', always_aslist=False):
2736
_rapidxml.Node.__init__(self)
2837
self.attribute_prefix = attribute_prefix
2938
self.cdata_key = cdata_key
39+
self.always_aslist = always_aslist
3040

3141
def get_nodes(self, name):
3242
node = self.first_node(name)
3343
if node is None:
3444
return None
35-
res = [DictNode(self.attribute_prefix, self.cdata_key).copy(node)]
45+
res = [DictNode(self.attribute_prefix,
46+
self.cdata_key,
47+
self.always_aslist).copy(node)]
3648
node = node.next_sibling(name)
37-
if node is None:
49+
if node is None and not self.always_aslist:
3850
return res[0]
3951
while node is not None:
40-
res.append(DictNode(self.attribute_prefix, self.cdata_key).copy(node))
52+
res.append(DictNode(self.attribute_prefix,
53+
self.cdata_key,
54+
self.always_aslist).copy(node))
4155
node = node.next_sibling(name)
4256
return res
4357

@@ -47,7 +61,7 @@ def get_attributes(self, name):
4761
return None
4862
res = [attribute]
4963
attribute = attribute.next_attribute(name)
50-
if attribute is None:
64+
if attribute is None and not self.always_aslist:
5165
return res[0]
5266
while attribute is not None:
5367
res.append(attribute)
@@ -74,17 +88,23 @@ def __contains__(self, name):
7488
return True
7589

7690
def __iter__(self):
77-
return DictNodeIterator(self.children)
91+
return DictNodeIterator(self.children,
92+
self.attribute_prefix,
93+
self.cdata_key,
94+
self.always_aslist)
7895

7996

8097
class RapidXml(DictNode, _rapidxml.Document):
8198
def __init__(self,
8299
text="",
83100
from_file=False,
84101
attribute_prefix='@',
85-
cdata_key='#text'):
86-
DictNode.__init__(self, attribute_prefix, cdata_key)
102+
cdata_key='#text',
103+
always_aslist=False):
104+
DictNode.__init__(self, attribute_prefix, cdata_key, always_aslist)
87105
_rapidxml.Document.__init__(self, text, from_file)
88106

89107
def allocate_node(self, *args):
90-
return DictNode().copy(super(RapidXml, self).allocate_node(*args))
108+
return DictNode(self.attribute_prefix,
109+
self.cdata_key,
110+
self.always_aslist).copy(super(RapidXml, self).allocate_node(*args))

0 commit comments

Comments
 (0)