Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "feature",
"description": "Added initial support for XML deserialization in Smithy clients."
}
1 change: 1 addition & 0 deletions packages/smithy-xml/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Changelog
1 change: 1 addition & 0 deletions packages/smithy-xml/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 changes: 4 additions & 0 deletions packages/smithy-xml/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# smithy-xml

This package provides generic XML serialization and deserialization support
for Smithy clients and servers.
50 changes: 50 additions & 0 deletions packages/smithy-xml/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[project]
name = "smithy-xml"
dynamic = ["version"]
requires-python = ">=3.12"
authors = [
{name = "Amazon Web Services"},
]
description = "XML serialization and deserialization support for Smithy tooling."
readme = "README.md"
license = {text = "Apache License 2.0"}
keywords = ["smithy", "sdk", "xml"]
classifiers = [
"Development Status :: 2 - Pre-Alpha",
"Intended Audience :: Developers",
"Intended Audience :: System Administrators",
"Natural Language :: English",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.14",
"Programming Language :: Python :: Implementation :: CPython",
"Topic :: Software Development :: Libraries"
]
dependencies = [
"smithy-core",
]

[project.urls]
"Changelog" = "https://github.com/smithy-lang/smithy-python/blob/develop/packages/smithy-xml/CHANGELOG.md"
"Code" = "https://github.com/smithy-lang/smithy-python/blob/develop/packages/smithy-xml/"
"Issue tracker" = "https://github.com/smithy-lang/smithy-python/issues"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.version]
path = "src/smithy_xml/__init__.py"

[tool.hatch.build]
exclude = [
"tests",
]

[tool.ruff]
src = ["src"]
65 changes: 65 additions & 0 deletions packages/smithy-xml/src/smithy_xml/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

from io import BytesIO
from xml.etree.ElementTree import iterparse

from smithy_core.codecs import Codec
from smithy_core.deserializers import ShapeDeserializer
from smithy_core.interfaces import BytesReader, BytesWriter
from smithy_core.serializers import ShapeSerializer
from smithy_core.types import TimestampFormat

from ._private.deserializers import XMLShapeDeserializer as _XMLShapeDeserializer
from ._private.readers import XMLEventReader as _XMLEventReader
from .settings import XMLSettings

__version__ = "0.1.0"
__all__ = ("XMLCodec", "XMLSettings")


class XMLCodec(Codec):
"""A codec for converting shapes to/from XML."""

def __init__(
self,
use_timestamp_format: bool = True,
default_timestamp_format: TimestampFormat = TimestampFormat.DATE_TIME,
default_namespace: str | None = None,
) -> None:
"""Initializes an XMLCodec.

:param use_timestamp_format: Whether the codec should use the
`smithy.api#timestampFormat` trait, if present.
:param default_timestamp_format: The default timestamp format to use if the
`smithy.api#timestampFormat` trait is not enabled or not present.
:param default_namespace: Default XML namespace (`xmlns`) applied to the root
element during serialization.
"""
self._settings = XMLSettings(
use_timestamp_format=use_timestamp_format,
default_timestamp_format=default_timestamp_format,
default_namespace=default_namespace,
)

@property
def media_type(self) -> str:
return "application/xml"

def create_serializer(self, sink: BytesWriter) -> ShapeSerializer:
raise NotImplementedError("XML serialization is not supported")

def create_deserializer(
self,
source: bytes | BytesReader,
*,
wrapper_elements: tuple[str, ...] = (),
) -> ShapeDeserializer:
if isinstance(source, bytes):
source = BytesIO(source)
reader = _XMLEventReader(
iterparse(source, events=("start", "end")) # noqa: S314
)
return _XMLShapeDeserializer(
settings=self._settings, reader=reader, wrapper_elements=wrapper_elements
)
2 changes: 2 additions & 0 deletions packages/smithy-xml/src/smithy_xml/_private/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
Loading
Loading