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
40 changes: 40 additions & 0 deletions packages/smithy-core/src/smithy_core/traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,43 @@ def name(self) -> str:
@property
def scheme(self) -> str | None:
return self.document_value.get("scheme") # type: ignore


@dataclass(init=False, frozen=True)
class XmlNameTrait(Trait, id=ShapeID("smithy.api#xmlName")):
document_value: str | None = None

def __post_init__(self):
assert isinstance(self.document_value, str)

@property
def value(self) -> str:
return self.document_value # type: ignore


@dataclass(init=False, frozen=True)
class XmlNamespaceTrait(Trait, id=ShapeID("smithy.api#xmlNamespace")):
def __post_init__(self):
assert isinstance(self.document_value, Mapping)
assert isinstance(self.document_value["uri"], str)
assert isinstance(self.document_value.get("prefix"), str | None)

@property
def uri(self) -> str:
return self.document_value["uri"] # type: ignore

@property
def prefix(self) -> str | None:
return self.document_value.get("prefix") # type: ignore


@dataclass(init=False, frozen=True)
class XmlFlattenedTrait(Trait, id=ShapeID("smithy.api#xmlFlattened")):
def __post_init__(self):
assert self.document_value is None


@dataclass(init=False, frozen=True)
class XmlAttributeTrait(Trait, id=ShapeID("smithy.api#xmlAttribute")):
def __post_init__(self):
assert self.document_value is None
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"]
55 changes: 55 additions & 0 deletions packages/smithy-xml/src/smithy_xml/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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 ._private.serializers import XMLShapeSerializer as _XMLShapeSerializer
from .settings import XMLSettings

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


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

def __init__(
self,
default_timestamp_format: TimestampFormat = TimestampFormat.DATE_TIME,
default_namespace: str | None = None,
) -> None:
self._settings = XMLSettings(
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:
return _XMLShapeSerializer(sink=sink, settings=self._settings)

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