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

Use lxml instead of standard library xml to improve namespace identification #18

Merged
merged 3 commits into from
Sep 1, 2024
Merged
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 pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ keywords = [

[tool.poetry.dependencies]
python = ">=3.8"
lxml = ">=4.8.0"

[tool.poetry.group.dev.dependencies]
pycodestyle = "*"
Expand Down
8 changes: 4 additions & 4 deletions space_packet_parser/xtcedef.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import struct
from typing import Tuple, Union, Optional, Any, List, TextIO, Dict
import warnings
from xml.etree import ElementTree
# Installed
import lxml.etree as ElementTree

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -2322,7 +2323,6 @@ def __repr__(self):
class XtcePacketDefinition:
"""Object representation of the XTCE definition of a CCSDS packet object"""

_default_namespace = {'xtce': 'http://www.omg.org/space/xtce'}
_tag_to_type_template = {
'{{{xtce}}}StringParameterType': StringParameterType,
'{{{xtce}}}IntegerParameterType': IntegerParameterType,
Expand Down Expand Up @@ -2352,10 +2352,10 @@ def __init__(self, xtce_document: TextIO, ns: Optional[dict] = None):
self._sequence_container_cache = {} # Lookup for parsed sequence container objects
self._parameter_cache = {} # Lookup for parsed parameter objects
self._parameter_type_cache = {} # Lookup for parsed parameter type objects
self.ns = ns or self._default_namespace
self.tree = ElementTree.parse(xtce_document)
self.ns = ns or self.tree.getroot().nsmap
self.type_tag_to_object = {k.format(**self.ns): v for k, v in
self._tag_to_type_template.items()}
self.tree = ElementTree.parse(xtce_document)

self._populate_sequence_container_cache()

Expand Down
13 changes: 7 additions & 6 deletions tests/unit/test_xtcedef.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Tests for space_packet_parser.xtcedef"""
# Standard
from io import StringIO
import io
# Installed
import pytest
from xml.etree import ElementTree
import lxml.etree as ElementTree
# Local
from space_packet_parser import xtcedef, parser

Expand Down Expand Up @@ -38,9 +39,9 @@ def test_invalid_parameter_type_error(test_data_dir):
</xtce:TelemetryMetaData>
</xtce:SpaceSystem>
"""
x = StringIO(test_xtce_document)
x = io.TextIOWrapper(io.BytesIO(test_xtce_document.encode("utf-8")))
with pytest.raises(xtcedef.InvalidParameterTypeError):
xtcedef.XtcePacketDefinition(x, ns=TEST_NAMESPACE)
xtcedef.XtcePacketDefinition(x)


def test_unsupported_parameter_type_error(test_data_dir):
Expand Down Expand Up @@ -79,9 +80,9 @@ def test_unsupported_parameter_type_error(test_data_dir):
</xtce:TelemetryMetaData>
</xtce:SpaceSystem>
"""
x = StringIO(test_xtce_document)
x = io.TextIOWrapper(io.BytesIO(test_xtce_document.encode("utf-8")))
with pytest.raises(NotImplementedError):
xtcedef.XtcePacketDefinition(x, ns=TEST_NAMESPACE)
xtcedef.XtcePacketDefinition(x)


def test_attr_comparable():
Expand Down