Skip to content

Commit 2e3bb33

Browse files
committed
Add smithy-xml package
1 parent 9c9b35d commit 2e3bb33

18 files changed

Lines changed: 2005 additions & 0 deletions

File tree

packages/smithy-core/src/smithy_core/traits.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,43 @@ def name(self) -> str:
350350
@property
351351
def scheme(self) -> str | None:
352352
return self.document_value.get("scheme") # type: ignore
353+
354+
355+
@dataclass(init=False, frozen=True)
356+
class XmlNameTrait(Trait, id=ShapeID("smithy.api#xmlName")):
357+
document_value: str | None = None
358+
359+
def __post_init__(self):
360+
assert isinstance(self.document_value, str)
361+
362+
@property
363+
def value(self) -> str:
364+
return self.document_value # type: ignore
365+
366+
367+
@dataclass(init=False, frozen=True)
368+
class XmlNamespaceTrait(Trait, id=ShapeID("smithy.api#xmlNamespace")):
369+
def __post_init__(self):
370+
assert isinstance(self.document_value, Mapping)
371+
assert isinstance(self.document_value["uri"], str)
372+
assert isinstance(self.document_value.get("prefix"), str | None)
373+
374+
@property
375+
def uri(self) -> str:
376+
return self.document_value["uri"] # type: ignore
377+
378+
@property
379+
def prefix(self) -> str | None:
380+
return self.document_value.get("prefix") # type: ignore
381+
382+
383+
@dataclass(init=False, frozen=True)
384+
class XmlFlattenedTrait(Trait, id=ShapeID("smithy.api#xmlFlattened")):
385+
def __post_init__(self):
386+
assert self.document_value is None
387+
388+
389+
@dataclass(init=False, frozen=True)
390+
class XmlAttributeTrait(Trait, id=ShapeID("smithy.api#xmlAttribute")):
391+
def __post_init__(self):
392+
assert self.document_value is None

packages/smithy-xml/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Changelog

packages/smithy-xml/NOTICE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

packages/smithy-xml/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# smithy-xml
2+
3+
This package provides generic XML serialization and deserialization support
4+
for Smithy clients and servers.

packages/smithy-xml/pyproject.toml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[project]
2+
name = "smithy-xml"
3+
dynamic = ["version"]
4+
requires-python = ">=3.12"
5+
authors = [
6+
{name = "Amazon Web Services"},
7+
]
8+
description = "XML serialization and deserialization support for Smithy tooling."
9+
readme = "README.md"
10+
license = {text = "Apache License 2.0"}
11+
keywords = ["smithy", "sdk", "xml"]
12+
classifiers = [
13+
"Development Status :: 2 - Pre-Alpha",
14+
"Intended Audience :: Developers",
15+
"Intended Audience :: System Administrators",
16+
"Natural Language :: English",
17+
"License :: OSI Approved :: Apache Software License",
18+
"Operating System :: OS Independent",
19+
"Programming Language :: Python",
20+
"Programming Language :: Python :: 3 :: Only",
21+
"Programming Language :: Python :: 3",
22+
"Programming Language :: Python :: 3.12",
23+
"Programming Language :: Python :: 3.13",
24+
"Programming Language :: Python :: 3.14",
25+
"Programming Language :: Python :: Implementation :: CPython",
26+
"Topic :: Software Development :: Libraries"
27+
]
28+
dependencies = [
29+
"smithy-core",
30+
]
31+
32+
[project.urls]
33+
"Changelog" = "https://github.com/smithy-lang/smithy-python/blob/develop/packages/smithy-xml/CHANGELOG.md"
34+
"Code" = "https://github.com/smithy-lang/smithy-python/blob/develop/packages/smithy-xml/"
35+
"Issue tracker" = "https://github.com/smithy-lang/smithy-python/issues"
36+
37+
[build-system]
38+
requires = ["hatchling"]
39+
build-backend = "hatchling.build"
40+
41+
[tool.hatch.version]
42+
path = "src/smithy_xml/__init__.py"
43+
44+
[tool.hatch.build]
45+
exclude = [
46+
"tests",
47+
]
48+
49+
[tool.ruff]
50+
src = ["src"]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
from io import BytesIO
5+
from xml.etree.ElementTree import iterparse
6+
7+
from smithy_core.codecs import Codec
8+
from smithy_core.deserializers import ShapeDeserializer
9+
from smithy_core.interfaces import BytesReader, BytesWriter
10+
from smithy_core.serializers import ShapeSerializer
11+
from smithy_core.types import TimestampFormat
12+
13+
from ._private.deserializers import XMLShapeDeserializer as _XMLShapeDeserializer
14+
from ._private.readers import XMLEventReader as _XMLEventReader
15+
from ._private.serializers import XMLShapeSerializer as _XMLShapeSerializer
16+
from .settings import XMLSettings
17+
18+
__version__ = "0.0.1"
19+
__all__ = ("XMLCodec", "XMLSettings")
20+
21+
22+
class XMLCodec(Codec):
23+
"""A codec for converting shapes to/from XML."""
24+
25+
def __init__(
26+
self,
27+
default_timestamp_format: TimestampFormat = TimestampFormat.DATE_TIME,
28+
default_namespace: str | None = None,
29+
) -> None:
30+
self._settings = XMLSettings(
31+
default_timestamp_format=default_timestamp_format,
32+
default_namespace=default_namespace,
33+
)
34+
35+
@property
36+
def media_type(self) -> str:
37+
return "application/xml"
38+
39+
def create_serializer(self, sink: BytesWriter) -> ShapeSerializer:
40+
return _XMLShapeSerializer(sink=sink, settings=self._settings)
41+
42+
def create_deserializer(
43+
self,
44+
source: bytes | BytesReader,
45+
*,
46+
wrapper_elements: tuple[str, ...] = (),
47+
) -> ShapeDeserializer:
48+
if isinstance(source, bytes):
49+
source = BytesIO(source)
50+
reader = _XMLEventReader(
51+
iterparse(source, events=("start", "end")) # noqa: S314
52+
)
53+
return _XMLShapeDeserializer(
54+
settings=self._settings, reader=reader, wrapper_elements=wrapper_elements
55+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0

0 commit comments

Comments
 (0)