feat: add unpack variants option (#20)
This commit is contained in:
@@ -4,6 +4,7 @@ import dbus_fast.introspection as intr
|
||||
from dbus_fast import DBusError, aio, glib
|
||||
from dbus_fast.message import MessageFlag
|
||||
from dbus_fast.service import ServiceInterface, method
|
||||
from dbus_fast.signature import Variant
|
||||
from tests.util import check_gi_repository, skip_reason_no_gi
|
||||
|
||||
has_gi = check_gi_repository()
|
||||
@@ -33,6 +34,11 @@ class ExampleInterface(ServiceInterface):
|
||||
def EchoThree(self, what1: "s", what2: "s", what3: "s") -> "sss":
|
||||
return [what1, what2, what3]
|
||||
|
||||
@method()
|
||||
def GetComplex(self) -> "a{sv}":
|
||||
"""Return complex output."""
|
||||
return {"hello": Variant("s", "world")}
|
||||
|
||||
@method()
|
||||
def ThrowsError(self):
|
||||
raise DBusError("test.error", "something went wrong")
|
||||
@@ -81,6 +87,12 @@ async def test_aio_proxy_object():
|
||||
)
|
||||
assert result is None
|
||||
|
||||
result = await interface.call_get_complex()
|
||||
assert result == {"hello": Variant("s", "world")}
|
||||
|
||||
result = await interface.call_get_complex(unpack_variants=True)
|
||||
assert result == {"hello": "world"}
|
||||
|
||||
with pytest.raises(DBusError):
|
||||
try:
|
||||
await interface.call_throws_error()
|
||||
@@ -120,6 +132,12 @@ def test_glib_proxy_object():
|
||||
result = interface.call_echo_three_sync("hello", "there", "world")
|
||||
assert result == ["hello", "there", "world"]
|
||||
|
||||
result = interface.call_get_complex_sync()
|
||||
assert result == {"hello": Variant("s", "world")}
|
||||
|
||||
result = interface.call_get_complex_sync(unpack_variants=True)
|
||||
assert result == {"hello": "world"}
|
||||
|
||||
with pytest.raises(DBusError):
|
||||
try:
|
||||
result = interface.call_throws_error_sync()
|
||||
|
||||
@@ -2,6 +2,7 @@ import pytest
|
||||
|
||||
from dbus_fast import DBusError, Message, aio, glib
|
||||
from dbus_fast.service import PropertyAccess, ServiceInterface, dbus_property
|
||||
from dbus_fast.signature import Variant
|
||||
from tests.util import check_gi_repository, skip_reason_no_gi
|
||||
|
||||
has_gi = check_gi_repository()
|
||||
@@ -27,6 +28,11 @@ class ExampleInterface(ServiceInterface):
|
||||
def Int64Property(self) -> "x":
|
||||
return self._int64_property
|
||||
|
||||
@dbus_property(access=PropertyAccess.READ)
|
||||
def ComplexProperty(self) -> "a{sv}":
|
||||
"""Return complex output."""
|
||||
return {"hello": Variant("s", "world")}
|
||||
|
||||
@dbus_property()
|
||||
def ErrorThrowingProperty(self) -> "s":
|
||||
raise DBusError(self.error_name, self.error_text)
|
||||
@@ -59,6 +65,12 @@ async def test_aio_properties():
|
||||
await interface.set_some_property("different")
|
||||
assert service_interface._some_property == "different"
|
||||
|
||||
prop = await interface.get_complex_property()
|
||||
assert prop == {"hello": Variant("s", "world")}
|
||||
|
||||
prop = await interface.get_complex_property(unpack_variants=True)
|
||||
assert prop == {"hello": "world"}
|
||||
|
||||
with pytest.raises(DBusError):
|
||||
try:
|
||||
prop = await interface.get_error_throwing_property()
|
||||
@@ -102,6 +114,12 @@ def test_glib_properties():
|
||||
interface.set_some_property_sync("different")
|
||||
assert service_interface._some_property == "different"
|
||||
|
||||
prop = interface.get_complex_property_sync()
|
||||
assert prop == {"hello": Variant("s", "world")}
|
||||
|
||||
prop = interface.get_complex_property_sync(unpack_variants=True)
|
||||
assert prop == {"hello": "world"}
|
||||
|
||||
with pytest.raises(DBusError):
|
||||
try:
|
||||
prop = interface.get_error_throwing_property_sync()
|
||||
|
||||
@@ -2,10 +2,10 @@ import pytest
|
||||
|
||||
from dbus_fast import Message
|
||||
from dbus_fast.aio import MessageBus
|
||||
from dbus_fast.aio.proxy_object import ProxyInterface
|
||||
from dbus_fast.constants import RequestNameReply
|
||||
from dbus_fast.introspection import Node
|
||||
from dbus_fast.service import ServiceInterface, signal
|
||||
from dbus_fast.signature import Variant
|
||||
|
||||
|
||||
class ExampleInterface(ServiceInterface):
|
||||
@@ -20,6 +20,11 @@ class ExampleInterface(ServiceInterface):
|
||||
def SignalMultiple(self) -> "ss":
|
||||
return ["hello", "world"]
|
||||
|
||||
@signal()
|
||||
def SignalComplex(self) -> "a{sv}":
|
||||
"""Broadcast a complex signal."""
|
||||
return {"hello": Variant("s", "world")}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_signals():
|
||||
@@ -159,6 +164,69 @@ async def test_signals():
|
||||
bus3.disconnect()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_complex_signals():
|
||||
"""Test complex signals with and without signature removal."""
|
||||
bus1 = await MessageBus().connect()
|
||||
bus2 = await MessageBus().connect()
|
||||
|
||||
await bus1.request_name("test.signals.name")
|
||||
service_interface = ExampleInterface()
|
||||
bus1.export("/test/path", service_interface)
|
||||
|
||||
obj = bus2.get_proxy_object(
|
||||
"test.signals.name", "/test/path", bus1._introspect_export_path("/test/path")
|
||||
)
|
||||
interface = obj.get_interface(service_interface.name)
|
||||
|
||||
async def ping():
|
||||
await bus2.call(
|
||||
Message(
|
||||
destination=bus1.unique_name,
|
||||
interface="org.freedesktop.DBus.Peer",
|
||||
path="/test/path",
|
||||
member="Ping",
|
||||
)
|
||||
)
|
||||
|
||||
sig_handler_counter = 0
|
||||
sig_handler_err = None
|
||||
no_sig_handler_counter = 0
|
||||
no_sig_handler_err = None
|
||||
|
||||
def complex_handler_with_sig(value):
|
||||
nonlocal sig_handler_counter
|
||||
nonlocal sig_handler_err
|
||||
try:
|
||||
assert value == {"hello": Variant("s", "world")}
|
||||
sig_handler_counter += 1
|
||||
except AssertionError as ex:
|
||||
sig_handler_err = ex
|
||||
|
||||
def complex_handler_no_sig(value):
|
||||
nonlocal no_sig_handler_counter
|
||||
nonlocal no_sig_handler_err
|
||||
try:
|
||||
assert value == {"hello": "world"}
|
||||
no_sig_handler_counter += 1
|
||||
except AssertionError as ex:
|
||||
no_sig_handler_err = ex
|
||||
|
||||
interface.on_signal_complex(complex_handler_with_sig)
|
||||
interface.on_signal_complex(complex_handler_no_sig, unpack_variants=True)
|
||||
await ping()
|
||||
|
||||
service_interface.SignalComplex()
|
||||
await ping()
|
||||
assert sig_handler_err is None
|
||||
assert sig_handler_counter == 1
|
||||
assert no_sig_handler_err is None
|
||||
assert no_sig_handler_counter == 1
|
||||
|
||||
bus1.disconnect()
|
||||
bus2.disconnect()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_varargs_callback():
|
||||
"""Test varargs callback for signal."""
|
||||
|
||||
Reference in New Issue
Block a user