Skip to content

Commit dffbfb0

Browse files
committed
DRAFT: Added get_current_message call
1 parent 35bebbf commit dffbfb0

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

src/sdbus/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
from .dbus_proxy_async_method import (
6565
dbus_method_async,
6666
dbus_method_async_override,
67+
get_current_message,
6768
)
6869
from .dbus_proxy_async_property import (
6970
dbus_property_async,
@@ -134,6 +135,7 @@
134135

135136
'dbus_property_async',
136137
'dbus_property_async_override',
138+
'get_current_message',
137139

138140
'dbus_signal_async',
139141

src/sdbus/dbus_proxy_async_method.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2020
from __future__ import annotations
2121

22+
from contextvars import ContextVar, copy_context
2223
from inspect import iscoroutinefunction
2324
from types import FunctionType
2425
from typing import (
@@ -42,6 +43,13 @@
4243
from .dbus_exceptions import DbusFailedError
4344
from .sd_bus_internals import DbusNoReplyFlag, SdBusMessage
4445

46+
CURRENT_MESSAGE: ContextVar[SdBusMessage] = ContextVar('CURRENT_MESSAGE')
47+
48+
49+
def get_current_message() -> SdBusMessage:
50+
return CURRENT_MESSAGE.get()
51+
52+
4553
T_input = TypeVar('T_input')
4654
T = TypeVar('T')
4755

@@ -118,24 +126,38 @@ def __call__(self, *args: Any, **kwargs: Any) -> Any:
118126
return self.dbus_method.original_method(
119127
interface, *args, **kwargs)
120128

129+
async def _call_method_from_dbus(
130+
self,
131+
request_message: SdBusMessage,
132+
interface: DbusInterfaceBaseAsync) -> Any:
133+
request_data = request_message.get_contents()
134+
135+
local_method = self.dbus_method.original_method.__get__(
136+
interface, None)
137+
138+
CURRENT_MESSAGE.set(request_message)
139+
140+
if isinstance(request_data, tuple):
141+
return await local_method(*request_data)
142+
elif request_data is None:
143+
return await local_method()
144+
else:
145+
return await local_method(request_data)
146+
121147
async def _call_from_dbus(
122148
self,
123149
request_message: SdBusMessage) -> None:
124150
interface = self.interface_ref()
125151
assert interface is not None
126152

127-
request_data = request_message.get_contents()
128-
129-
local_method = self.dbus_method.original_method.__get__(
130-
interface, None)
153+
call_context = copy_context()
131154

132155
try:
133-
if isinstance(request_data, tuple):
134-
reply_data = await local_method(*request_data)
135-
elif request_data is None:
136-
reply_data = await local_method()
137-
else:
138-
reply_data = await local_method(request_data)
156+
reply_data = await call_context.run(
157+
self._call_method_from_dbus,
158+
request_message,
159+
interface,
160+
)
139161
except DbusFailedError as e:
140162
if not request_message.expect_reply:
141163
return

test/test_sd_bus_async.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
dbus_property_async,
4949
dbus_property_async_override,
5050
dbus_signal_async,
51+
get_current_message,
5152
)
5253

5354

@@ -98,6 +99,11 @@ async def upper(self, string: str) -> str:
9899
"""Uppercase the input"""
99100
return string.upper()
100101

102+
@dbus_method_async(result_signature='s')
103+
async def get_sender(self) -> str:
104+
message = get_current_message()
105+
return message.sender or ''
106+
101107
@dbus_method_async(result_signature='x')
102108
async def test_int(self) -> int:
103109
return 1
@@ -281,6 +287,8 @@ async def test_method(self) -> None:
281287
test_object_connection.test_struct_return_workaround(), 0.5),
282288
)
283289

290+
self.assertTrue(await test_object_connection.get_sender())
291+
284292
async def test_subclass(self) -> None:
285293
test_object, test_object_connection = initialize_object()
286294

0 commit comments

Comments
 (0)