-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap backend-specific exceptions in SerializationError/Deserializatio…
…nError (#57)
- Loading branch information
Showing
15 changed files
with
143 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,30 @@ | ||
Writing new serializer backends | ||
=============================== | ||
|
||
If you wish to implement an alternate method of serialization, you can do so by subclassing | ||
the :class:`~asphalt.serialization.api.Serializer` class. | ||
There are three methods implementors must override: | ||
.. py:currentmodule:: asphalt.serialization | ||
* :meth:`~asphalt.serialization.api.Serializer.serialize` | ||
* :meth:`~asphalt.serialization.api.Serializer.deserialize` | ||
* :meth:`~asphalt.serialization.api.Serializer.mimetype` | ||
If you wish to implement an alternate method of serialization, you can do so by | ||
subclassing the :class:`~Serializer` class. There are three methods implementors must | ||
override: | ||
|
||
The ``mimetype`` method is a ``@property`` that simply returns the MIME type appropriate for the | ||
serialization scheme. This property is used by certain other components. If you cannot find an | ||
applicable MIME type, you can use ``application/octet-stream``. | ||
* :meth:`~Serializer.serialize` | ||
* :meth:`~Serializer.deserialize` | ||
* :meth:`~Serializer.mimetype` | ||
|
||
The ``mimetype`` method is a ``@property`` that simply returns the MIME type appropriate | ||
for the serialization scheme. This property is used by certain other components. If you | ||
cannot find an applicable MIME type, you can use ``application/octet-stream``. | ||
|
||
.. note:: Serializers must always serialize to bytes; never serialize to strings! | ||
|
||
If you want your serializer to be available as a backend for | ||
:class:`~asphalt.serialization.component.SerializationComponent`, you need to add the corresponding | ||
entry point for it. Suppose your serializer class is named ``AwesomeSerializer``, lives in the | ||
package ``foo.bar.awesome`` and you want to give it the alias ``awesome``, add this line to your | ||
project's ``setup.py`` under the ``entry_points`` argument in the | ||
``asphalt.serialization.serializers`` namespace: | ||
|
||
.. code-block:: python | ||
setup( | ||
# (...other arguments...) | ||
entry_points={ | ||
'asphalt.serialization.serializers': [ | ||
'awesome = foo.bar.awesome:AwesomeSerializer' | ||
] | ||
} | ||
) | ||
:class:`~asphalt.serialization.SerializationComponent`, you need to add the | ||
corresponding entry point for it. Suppose your serializer class is named | ||
``AwesomeSerializer``, lives in the package ``foo.bar.awesome`` and you want to give it | ||
the alias ``awesome``, add this line to your project's ``pyproject.toml`` under the | ||
``entry_points`` argument in the ``asphalt.serialization.serializers`` namespace: | ||
|
||
.. code-block:: toml | ||
[project.entry-points."asphalt.serialization.serializers"] | ||
awesome = "foo.bar.awesome:AwesomeSerializer" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
from typing import Any | ||
|
||
from ._api import CustomizableSerializer as CustomizableSerializer | ||
from ._api import CustomTypeCodec as CustomTypeCodec | ||
from ._api import Serializer as Serializer | ||
from ._component import SerializationComponent as SerializationComponent | ||
from ._exceptions import DeserializationError as DeserializationError | ||
from ._exceptions import SerializationError as SerializationError | ||
from ._marshalling import default_marshaller as default_marshaller | ||
from ._marshalling import default_unmarshaller as default_unmarshaller | ||
from ._object_codec import DefaultCustomTypeCodec as DefaultCustomTypeCodec | ||
|
||
# Re-export imports, so they look like they live directly in this package | ||
key: str | ||
value: Any | ||
for key, value in list(locals().items()): | ||
if getattr(value, "__module__", "").startswith(f"{__name__}."): | ||
value.__module__ = __name__ | ||
for __value in list(locals().values()): | ||
if getattr(__value, "__module__", "").startswith(f"{__name__}."): | ||
__value.__module__ = __name__ | ||
|
||
del __value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class SerializationError(Exception): | ||
"""Raised when serialization fails.""" | ||
|
||
|
||
class DeserializationError(Exception): | ||
"""Raised when deserialization fails.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.