Skip to content

Commit

Permalink
feat: cythonize unpack_variants (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 2, 2022
1 parent f6e4c3c commit 1587211
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 15 deletions.
1 change: 1 addition & 0 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def build(setup_kwargs):
dict(
ext_modules=cythonize(
[
"src/dbus_fast/_unpack.py",
"src/dbus_fast/_private/marshaller.py",
"src/dbus_fast/_private/unmarshaller.py",
]
Expand Down
45 changes: 45 additions & 0 deletions src/dbus_fast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
)
from .message import Message
from .signature import SignatureTree, SignatureType, Variant
from .unpack import unpack_variants
from .validators import (
assert_bus_name_valid,
assert_interface_name_valid,
Expand All @@ -37,3 +38,47 @@
is_member_name_valid,
is_object_path_valid,
)

__all__ = [
"aio",
"glib",
"introspection",
"message_bus",
"proxy_object",
"service",
"ArgDirection",
"BusType",
"ErrorType",
"MessageFlag",
"MessageType",
"NameFlag",
"PropertyAccess",
"ReleaseNameReply",
"RequestNameReply",
"AuthError",
"DBusError",
"InterfaceNotFoundError",
"InvalidAddressError",
"InvalidBusNameError",
"InvalidInterfaceNameError",
"InvalidIntrospectionError",
"InvalidMemberNameError",
"InvalidMessageError",
"InvalidObjectPathError",
"InvalidSignatureError",
"SignalDisabledError",
"SignatureBodyMismatchError",
"Message",
"SignatureTree",
"SignatureType",
"Variant",
"assert_bus_name_valid",
"assert_interface_name_valid",
"assert_member_name_valid",
"assert_object_path_valid",
"is_bus_name_valid",
"is_interface_name_valid",
"is_member_name_valid",
"is_object_path_valid",
"unpack_variants",
]
2 changes: 1 addition & 1 deletion src/dbus_fast/aio/proxy_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ..message_bus import BaseMessageBus
from ..proxy_object import BaseProxyInterface, BaseProxyObject
from ..signature import Variant
from ..signature import unpack_variants as unpack
from ..unpack import unpack_variants as unpack

if TYPE_CHECKING:
from .message_bus import MessageBus as AioMessageBus
Expand Down
2 changes: 1 addition & 1 deletion src/dbus_fast/glib/proxy_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..message_bus import BaseMessageBus
from ..proxy_object import BaseProxyInterface, BaseProxyObject
from ..signature import Variant
from ..signature import unpack_variants as unpack
from ..unpack import unpack_variants as unpack

# glib is optional
try:
Expand Down
2 changes: 1 addition & 1 deletion src/dbus_fast/proxy_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .constants import ErrorType, MessageType
from .errors import DBusError, InterfaceNotFoundError
from .message import Message
from .signature import unpack_variants as unpack
from .unpack import unpack_variants as unpack
from .validators import assert_bus_name_valid, assert_object_path_valid


Expand Down
11 changes: 0 additions & 11 deletions src/dbus_fast/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@
from .validators import is_object_path_valid


def unpack_variants(data: Any):
"""Unpack variants and remove signature info."""
if isinstance(data, Variant):
return unpack_variants(data.value)
if isinstance(data, dict):
return {k: unpack_variants(v) for k, v in data.items()}
if isinstance(data, list):
return [unpack_variants(item) for item in data]
return data


class SignatureType:
"""A class that represents a single complete type within a signature.
Expand Down
6 changes: 6 additions & 0 deletions src/dbus_fast/unpack.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""cdefs for unpack.py"""

import cython


cpdef unpack_variants(object data)
14 changes: 14 additions & 0 deletions src/dbus_fast/unpack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from typing import Any

from .signature import Variant


def unpack_variants(data: Any) -> Any:
"""Unpack variants and remove signature info."""
if isinstance(data, Variant):
return unpack_variants(data.value)
if isinstance(data, dict):
return {k: unpack_variants(v) for k, v in data.items()}
if isinstance(data, list):
return [unpack_variants(item) for item in data]
return data
3 changes: 2 additions & 1 deletion tests/test_unpack_variants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Test unpack variants."""
import pytest

from dbus_fast.signature import Variant, unpack_variants
from dbus_fast.signature import Variant
from dbus_fast.unpack import unpack_variants


@pytest.mark.asyncio
Expand Down

0 comments on commit 1587211

Please sign in to comment.