-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add xml and lxml tree serializers
Resolves #849
- Loading branch information
Showing
11 changed files
with
264 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Element Tree Serializer | ||
|
||
The element tree serializers will render an object into an element tree, that you can | ||
use to run XPATH evaluations or XSLT transformations. | ||
|
||
There are two implementations based on lxml | ||
[LxmlTreeSerializer][xsdata.formats.dataclass.serializers.LxmlTreeSerializer] and native | ||
python [XmlTreeSerializer][xsdata.formats.dataclass.serializers.XmlTreeSerializer]. | ||
|
||
## xml.etree.ElementTree.Element | ||
|
||
```python | ||
>>> from xml.etree import ElementTree | ||
>>> from tests.fixtures.books.fixtures import books | ||
>>> from xsdata.formats.dataclass.serializers import XmlTreeSerializer | ||
... | ||
>>> serializer = XmlTreeSerializer() | ||
>>> result = serializer.render(books) | ||
... | ||
>>> result.find(".//title").text | ||
'The First Book' | ||
|
||
``` | ||
|
||
## lxml.etree.Element | ||
|
||
```python | ||
>>> from lxml import etree | ||
>>> from tests.fixtures.books.fixtures import books | ||
>>> from xsdata.formats.dataclass.serializers import LxmlTreeSerializer | ||
... | ||
>>> serializer = LxmlTreeSerializer() | ||
>>> result = serializer.render(books) | ||
... | ||
>>> etree.indent(result) | ||
>>> actual = etree.tostring(result) | ||
>>> print(actual.decode()) | ||
<ns0:books xmlns:ns0="urn:books"> | ||
<book> | ||
<author>Hightower, Kim</author> | ||
<title>The First Book</title> | ||
<genre>Fiction</genre> | ||
<price>44.95</price> | ||
<pub_date>2000-10-01</pub_date> | ||
<review>An amazing story of nothing.</review> | ||
</book> | ||
<book> | ||
<author>Nagata, Suanne</author> | ||
<title>Becoming Somebody</title> | ||
<genre>Biography</genre> | ||
<price>33.95</price> | ||
<pub_date>2001-01-10</pub_date> | ||
<review>A masterpiece of the fine art of gossiping.</review> | ||
</book> | ||
</ns0:books> | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from unittest import TestCase | ||
|
||
from lxml import etree | ||
|
||
from tests.fixtures.books.fixtures import books | ||
from xsdata.formats.dataclass.serializers import LxmlTreeSerializer | ||
|
||
|
||
class LxmlTreeSerializerTests(TestCase): | ||
def test_render(self): | ||
serializer = LxmlTreeSerializer() | ||
result = serializer.render(books) | ||
|
||
etree.indent(result) | ||
actual = etree.tostring(result) | ||
expected = ( | ||
'<ns0:books xmlns:ns0="urn:books">\n' | ||
" <book>\n" | ||
" <author>Hightower, Kim</author>\n" | ||
" <title>The First Book</title>\n" | ||
" <genre>Fiction</genre>\n" | ||
" <price>44.95</price>\n" | ||
" <pub_date>2000-10-01</pub_date>\n" | ||
" <review>An amazing story of nothing.</review>\n" | ||
" </book>\n" | ||
" <book>\n" | ||
" <author>Nagata, Suanne</author>\n" | ||
" <title>Becoming Somebody</title>\n" | ||
" <genre>Biography</genre>\n" | ||
" <price>33.95</price>\n" | ||
" <pub_date>2001-01-10</pub_date>\n" | ||
" <review>A masterpiece of the fine art of gossiping.</review>\n" | ||
" </book>\n" | ||
"</ns0:books>" | ||
) | ||
self.assertEqual(expected, actual.decode()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from unittest import TestCase, mock | ||
from xml.etree.ElementTree import TreeBuilder | ||
|
||
from tests.fixtures.books.fixtures import books | ||
from xsdata.exceptions import XmlHandlerError | ||
from xsdata.formats.dataclass.serializers.tree.mixins import TreeSerializer | ||
|
||
|
||
class TreeSerializerTests(TestCase): | ||
@mock.patch.object(TreeSerializer, "generate") | ||
def test_build_with_unknown_event(self, mock_generate): | ||
mock_generate.return_value = [ | ||
("foobar", True), | ||
] | ||
|
||
builder = TreeBuilder() | ||
serializer = TreeSerializer() | ||
with self.assertRaises(XmlHandlerError) as cm: | ||
serializer.build(books, builder) | ||
|
||
self.assertEqual("Unhandled event: `foobar`.", str(cm.exception)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import sys | ||
from unittest import TestCase | ||
from xml.etree import ElementTree | ||
|
||
import pytest | ||
|
||
from tests.fixtures.books.fixtures import books | ||
from xsdata.formats.dataclass.serializers import XmlTreeSerializer | ||
|
||
|
||
class XmlTreeSerializerTests(TestCase): | ||
@pytest.mark.skipif(sys.version_info < (3, 9), reason="requires python3.9") | ||
def test_render(self): | ||
serializer = XmlTreeSerializer() | ||
result = serializer.render(books) | ||
|
||
ElementTree.indent(result) | ||
actual = ElementTree.tostring(result) | ||
expected = ( | ||
'<ns0:books xmlns:ns0="urn:books">\n' | ||
" <book>\n" | ||
" <author>Hightower, Kim</author>\n" | ||
" <title>The First Book</title>\n" | ||
" <genre>Fiction</genre>\n" | ||
" <price>44.95</price>\n" | ||
" <pub_date>2000-10-01</pub_date>\n" | ||
" <review>An amazing story of nothing.</review>\n" | ||
" </book>\n" | ||
" <book>\n" | ||
" <author>Nagata, Suanne</author>\n" | ||
" <title>Becoming Somebody</title>\n" | ||
" <genre>Biography</genre>\n" | ||
" <price>33.95</price>\n" | ||
" <pub_date>2001-01-10</pub_date>\n" | ||
" <review>A masterpiece of the fine art of gossiping.</review>\n" | ||
" </book>\n" | ||
"</ns0:books>" | ||
) | ||
self.assertEqual(expected, actual.decode()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from dataclasses import dataclass | ||
|
||
from lxml.etree import Element, TreeBuilder | ||
|
||
from xsdata.formats.bindings import T | ||
from xsdata.formats.dataclass.serializers.tree.mixins import TreeSerializer | ||
|
||
|
||
@dataclass | ||
class LxmlTreeSerializer(TreeSerializer): | ||
def render(self, obj: T) -> Element: | ||
"""Serialize the input model instance to element tree. | ||
Args: | ||
obj: The input model instance to serialize | ||
Returns: | ||
The generated element tree instance. | ||
""" | ||
builder = TreeBuilder() | ||
self.build(obj, builder) | ||
return builder.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import abc | ||
from dataclasses import dataclass | ||
from typing import Any, Dict, Protocol | ||
|
||
from xsdata.exceptions import XmlHandlerError | ||
from xsdata.formats.bindings import T | ||
from xsdata.formats.dataclass.serializers.mixins import EventGenerator, XmlWriterEvent | ||
from xsdata.models.enums import EventType | ||
|
||
|
||
class TreeBuilder(Protocol): | ||
def start(self, tag: str, attrs: Dict[str, Any]): ... # pragma: no cover | ||
|
||
def end(self, tag: str): ... # pragma: no cover | ||
|
||
def data(self, data: str): ... # pragma: no cover | ||
|
||
|
||
@dataclass | ||
class TreeSerializer(EventGenerator): | ||
@abc.abstractmethod | ||
def render(self, obj: T) -> Any: | ||
"""Serialize the input model instance to element tree. | ||
Args: | ||
obj: The input model instance to serialize | ||
Returns: | ||
The generated element tree instance. | ||
""" | ||
|
||
def build(self, obj: T, builder: TreeBuilder): | ||
"""Feed the builder with events from input model instance. | ||
Args: | ||
obj: The input model instance to serialize | ||
builder: The tree builder instance | ||
""" | ||
pending_tag = None | ||
pending_attrs: Dict[str, Any] = {} | ||
for event, *element in self.generate(obj): | ||
if pending_tag is not None: | ||
builder.start(pending_tag, pending_attrs) | ||
pending_tag = None | ||
pending_attrs = {} | ||
|
||
if event == XmlWriterEvent.START: | ||
pending_tag = element[0] | ||
pending_attrs = {} | ||
elif event == XmlWriterEvent.ATTR: | ||
key, value = element | ||
pending_attrs[key] = value | ||
|
||
elif event == EventType.END: | ||
builder.end(*element) | ||
elif event == XmlWriterEvent.DATA: | ||
builder.data(*element) | ||
else: | ||
raise XmlHandlerError(f"Unhandled event: `{event}`.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from dataclasses import dataclass | ||
from xml.etree.ElementTree import Element, TreeBuilder | ||
|
||
from xsdata.formats.bindings import T | ||
from xsdata.formats.dataclass.serializers.tree.mixins import TreeSerializer | ||
|
||
|
||
@dataclass | ||
class XmlTreeSerializer(TreeSerializer): | ||
def render(self, obj: T) -> Element: | ||
"""Serialize the input model instance to element tree. | ||
Args: | ||
obj: The input model instance to serialize | ||
Returns: | ||
The generated element tree instance. | ||
""" | ||
builder = TreeBuilder() | ||
self.build(obj, builder) | ||
return builder.close() |