Skip to content

Make client info available #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,6 @@ dmypy.json

# vim
*.swp

# vscode
.vscode/
47 changes: 46 additions & 1 deletion dbus_next/message_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,57 @@
from .proxy_object import BaseProxyObject
from . import introspection as intr

import contextvars
import inspect
import socket
import logging
import xml.etree.ElementTree as ET
import traceback

from typing import Type, Callable, Optional, Union
from typing import Type, Callable, Optional, Union, Any


class ReadOnlyContextProxy:
"""
A convenience class for making a context variable accessible as though it
were a local. Any request for an attribute (other than `set_value`) on the
proxy will be passed through to the underlying variable. Attributes are
immutable.

:param name: The name of the context variable.
"""
def __init__(self, name: str):
self._obj = contextvars.ContextVar(name)

def __getattr__(self, name: str) -> Any:
proxy = self._obj.get()
return getattr(proxy, name)

def set_value(self, value: Any):
"""
Set the value of the underlying context variable.
"""
self._obj.set(value)


"""
The :class:`Message <dbus.message.Message>` object currently being handled.

Client code can use this to obtain access to details from the message without
modifying their public API. Typical use is:

```
from dbus_next.message_bus import current_message

@method()
def echo_sender() -> 's':
return current_message.sender
```

Attempts to access any attribute of `current_message` outside of a message context
will result in a `LookupError` being raised.
"""
current_message = ReadOnlyContextProxy("current_message")


class BaseMessageBus:
Expand Down Expand Up @@ -661,6 +705,7 @@ def send_error(self, exc):
return SendReply()

def _process_message(self, msg):
current_message.set_value(msg)
handled = False

for handler in self._user_message_handlers:
Expand Down
2 changes: 1 addition & 1 deletion dbus_next/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def echo_two(self, val1: 's', val2: 'u') -> 'su':
def decorator(fn):
@wraps(fn)
def wrapped(*args, **kwargs):
fn(*args, **kwargs)
return fn(*args, **kwargs)

fn_name = name if name else fn.__name__
wrapped.__dict__['__DBUS_METHOD'] = _Method(fn, fn_name, disabled=disabled)
Expand Down
8 changes: 8 additions & 0 deletions test/client/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from dbus_next.service import ServiceInterface, method
import dbus_next.introspection as intr
from dbus_next import aio, glib, DBusError
from dbus_next.message_bus import current_message
from test.util import check_gi_repository, skip_reason_no_gi

import pytest
Expand Down Expand Up @@ -37,6 +38,10 @@ def EchoThree(self, what1: 's', what2: 's', what3: 's') -> 'sss':
def ThrowsError(self):
raise DBusError('test.error', 'something went wrong')

@method()
def UsesCurrentMessage(self) -> 's':
return current_message.sender


@pytest.mark.asyncio
async def test_aio_proxy_object():
Expand Down Expand Up @@ -79,6 +84,9 @@ async def test_aio_proxy_object():
result = await interface.call_echo_string('no reply', flags=MessageFlag.NO_REPLY_EXPECTED)
assert result is None

result = await interface.call_uses_current_message()
assert result

with pytest.raises(DBusError):
try:
await interface.call_throws_error()
Expand Down
3 changes: 3 additions & 0 deletions test/client/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dbus_next import Message
from dbus_next.introspection import Node
from dbus_next.constants import RequestNameReply
from dbus_next.message_bus import current_message

import pytest

Expand Down Expand Up @@ -53,6 +54,7 @@ def single_handler(value):
nonlocal single_counter
nonlocal err
assert value == 'hello'
assert current_message.sender
single_counter += 1
except Exception as e:
err = e
Expand All @@ -65,6 +67,7 @@ def multiple_handler(value1, value2):
try:
assert value1 == 'hello'
assert value2 == 'world'
assert current_message.sender
multiple_counter += 1
except Exception as e:
err = e
Expand Down
8 changes: 6 additions & 2 deletions test/test_tcp_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ async def test_tcp_connection_with_forwarding(event_loop):

addr_info = parse_address(os.environ.get('DBUS_SESSION_BUS_ADDRESS'))
assert addr_info
assert 'abstract' in addr_info[0][1]
path = f'\0{addr_info[0][1]["abstract"]}'
if 'abstract' in addr_info[0][1]:
path = f'\0{addr_info[0][1]["abstract"]}'
elif 'path' in addr_info[0][1]:
path = addr_info[0][1]['path']

assert path

async def handle_connection(tcp_reader, tcp_writer):
unix_reader, unix_writer = await asyncio.open_unix_connection(path)
Expand Down