Skip to content

Commit

Permalink
Merge pull request #2 from couling/V2
Browse files Browse the repository at this point in the history
Moved to etree.ElementTree removing simple_node
  • Loading branch information
couling authored Dec 5, 2022
2 parents 4e2c922 + dd44c88 commit 46036a0
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 420 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ venv
*.egg-info
dist
build
*.pyc
9 changes: 4 additions & 5 deletions examples/rss/feed.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from xsbe import transform, simple_node
from xsbe import transform
import requests
from os import path


def main():
reader = FeedReader()
feed_content = reader.read_feed("https://podcasts.files.bbci.co.uk/p05299nl.rss")
Expand All @@ -16,13 +15,13 @@ def main():
class FeedReader:
def __init__(self, schema_file: str = f"{path.dirname(__file__)}/rss_schema.xml"):
with open(schema_file, "r") as file:
self._parser = transform.create_transformer(simple_node.load(file), ignore_unexpected=True)
self._parser = transform.create_transformer(file, ignore_unexpected=True)

def read_feed(self, url: str) -> dict:
response = requests.get(url)
response.raise_for_status()
response_xml = simple_node.loads(response.content.decode())
return self._parser.transform_from_xml(response_xml)
response_xml = response.content
return self._parser.loads(response_xml)


if __name__ == '__main__':
Expand Down
23 changes: 23 additions & 0 deletions source/tests/test_formatting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from io import StringIO

from xsbe import transform


def test_basic_formatting():
schema = """
<person id="20" xsbe:type="flatten" xmlns:xsbe="http://xsbe.couling.uk">
<name>Philip</name>
</person>
"""

data = {
'id': 21,
'name': 'Alan'
}

expected_result = "<?xml version='1.0' encoding='utf-8'?>\n<person id=\"21\"><name>Alan</name></person>"

parser = transform.create_transformer(StringIO(schema), ignore_unexpected=True)
document = parser.dumps(data)

assert document == expected_result
161 changes: 85 additions & 76 deletions source/tests/test_transform.py → source/tests/test_parse.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
import pytest
import datetime
from xsbe import simple_node
from xsbe import transform

from io import StringIO


def test_flatten():
schema = """
<xsbe:schema-by-example xmlns:xsbe="http://xsbe.couling.uk">
<xsbe:root>
<person id="20" xsbe:type="flatten">
<name>Philip</name>
</person>
<people>
<flattened xsbe:type="flatten">
<person id="20" xsbe:type="repeating">
<name>Philip</name>
</person>
</flattened>
</people>
</xsbe:root>
</xsbe:schema-by-example>
"""

document = """
<person id="21">
<name>Alan</name>
</person>
<people>
<flattened>
<person id="21">
<name>Alan</name>
</person>
</flattened>
</people>
"""

parser = transform.create_transformer(
simple_node.loads(schema),
ignore_unexpected=True
)
doc_node = simple_node.loads(document)
data = parser.transform_from_xml(doc_node)
parser = transform.create_transformer(StringIO(schema), ignore_unexpected=True)
data = parser.loads(document)

assert data == {
'id': 21,
'name': 'Alan'
'person': [
{
'id': 21,
'name': 'Alan'
}
]
}


Expand All @@ -53,12 +60,28 @@ def test_repeating():
</people>
"""

parser = transform.create_transformer(
simple_node.loads(schema),
ignore_unexpected=True
)
doc_node = simple_node.loads(document)
data = parser.transform_from_xml(doc_node)
parser = transform.create_transformer(StringIO(schema), ignore_unexpected=True)
data = parser.loads(document)

assert data =={'people': ['Alan', 'Also Alan']}


def test_non_xsbe_root_node():
schema = """
<people xmlns:xsbe="http://xsbe.couling.uk">
<person xsbe:type="repeating" xsbe:name="people">Philip</person>
</people>
"""

document = """
<people>
<person>Alan</person>
<person>Also Alan</person>
</people>
"""

parser = transform.create_transformer(StringIO(schema), ignore_unexpected=True)
data = parser.loads(document)

assert data =={'people': ['Alan', 'Also Alan']}

Expand Down Expand Up @@ -87,12 +110,8 @@ def test_repeating_flatten():
</people>
"""

parser = transform.create_transformer(
simple_node.loads(schema),
ignore_unexpected=True
)
doc_node = simple_node.loads(document)
data = parser.transform_from_xml(doc_node)
parser = transform.create_transformer(StringIO(schema), ignore_unexpected=True)
data = parser.loads(document)

assert data == {
'people': [
Expand Down Expand Up @@ -125,12 +144,8 @@ def test_friendly_name():
</people>
"""

parser = transform.create_transformer(
simple_node.loads(schema),
ignore_unexpected=True
)
doc_node = simple_node.loads(document)
data = parser.transform_from_xml(doc_node)
parser = transform.create_transformer(StringIO(schema), ignore_unexpected=True)
data = parser.loads(document)

assert data == {'person': 'Alan'}

Expand All @@ -153,14 +168,10 @@ def test_friendly_name_duplicates_error():
</people>
"""

parser = transform.create_transformer(
simple_node.loads(schema),
ignore_unexpected=True
)
doc_node = simple_node.loads(document)
parser = transform.create_transformer(StringIO(schema), ignore_unexpected=True)

with pytest.raises(transform.DuplicateElement):
parser.transform_from_xml(doc_node)
parser.loads(document)


def test_int():
Expand All @@ -180,12 +191,8 @@ def test_int():
</person>
"""

parser = transform.create_transformer(
simple_node.loads(schema),
ignore_unexpected=True
)
doc_node = simple_node.loads(document)
data = parser.transform_from_xml(doc_node)
parser = transform.create_transformer(StringIO(schema), ignore_unexpected=True)
data = parser.loads(document)

assert data == {
'value': 28,
Expand All @@ -209,14 +216,10 @@ def test_int_catch_error():
</person>
"""

parser = transform.create_transformer(
simple_node.loads(schema),
ignore_unexpected=True
)
doc_node = simple_node.loads(document)
parser = transform.create_transformer(StringIO(schema), ignore_unexpected=True)

with pytest.raises(ValueError):
parser.transform_from_xml(doc_node)
parser.loads(document)


def test_float():
Expand All @@ -236,12 +239,8 @@ def test_float():
</person>
"""

parser = transform.create_transformer(
simple_node.loads(schema),
ignore_unexpected=True
)
doc_node = simple_node.loads(document)
data = parser.transform_from_xml(doc_node)
parser = transform.create_transformer(StringIO(schema), ignore_unexpected=True)
data = parser.loads(document)

assert data == {'value': 1.41421356237}

Expand All @@ -263,38 +262,48 @@ def test_string():
</person>
"""

parser = transform.create_transformer(
simple_node.loads(schema),
ignore_unexpected=True
)
doc_node = simple_node.loads(document)
data = parser.transform_from_xml(doc_node)
parser = transform.create_transformer(StringIO(schema), ignore_unexpected=True)
data = parser.loads(document)

assert data == {'value': '+44012345678910'}


def test_date():
schema = """
def _tz(**kwargs) -> datetime.timezone:
return datetime.timezone(datetime.timedelta(**kwargs))


@pytest.mark.parametrize(
('value', 'result'), [
('2020-12-31', datetime.datetime(2020, 12, 31)),
('2020-12-31T11:45:23', datetime.datetime(2020, 12, 31, 11, 45, 23)),
('2020-12-31 11:45:23', datetime.datetime(2020, 12, 31, 11, 45, 23)),
('2020-12-31 11:45:23+01:00', datetime.datetime(2020, 12, 31, 11, 45, 23, tzinfo=_tz(hours=1))),
('2020-12-31T11:45:23+01:00', datetime.datetime(2020, 12, 31, 11, 45, 23, tzinfo=_tz(hours=1))),
('2020-12-31T11:45:23Z', datetime.datetime(2020, 12, 31, 11, 45, 23, tzinfo=_tz(hours=0))),
('Mon, 16 Nov 2009 13:32:02 +0400', datetime.datetime(2009, 11, 16, 13, 32, 2, tzinfo=_tz(hours=4))),
('Mon, 16 Nov 2009 13:32:02', datetime.datetime(2009, 11, 16, 13, 32, 2)),
])
def test_date(value: str, result: datetime):
schema = f"""
<xsbe:schema-by-example xmlns:xsbe="http://xsbe.couling.uk">
<xsbe:root>
<person xsbe:type="flatten">
<value>2020-12-30</value>
<value>{value}</value>
</person>
</xsbe:root>
</xsbe:schema-by-example>
"""

document = """
document = f"""
<person>
<value>2020-12-31</value>
<value>{value}</value>
</person>
"""

parser = transform.create_transformer(
simple_node.loads(schema),
ignore_unexpected=True
)
doc_node = simple_node.loads(document)
data = parser.transform_from_xml(doc_node)
parser = transform.create_transformer(StringIO(schema), ignore_unexpected=True)
data = parser.loads(document)
assert list(data.keys()) == ['value']
assert data['value'] == result
assert data['value'].tzinfo == result.tzinfo


assert data == {'value': datetime.datetime(2020, 12, 31)}
Loading

0 comments on commit 46036a0

Please sign in to comment.