Skip to content

Commit

Permalink
Return async D-Bus elements directly when accessed from class
Browse files Browse the repository at this point in the history
Meaning it will be equivalent to how when function is accessed
from object it becomes method but is still function when accessed
from class.

This removes the *ClassBind classes and returns the D-Bus element
itself like DbusPropertyAsync or DbusMethodAsync.
  • Loading branch information
igo95862 committed Feb 24, 2024
1 parent 03593a1 commit aaf63a3
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 127 deletions.
35 changes: 8 additions & 27 deletions src/sdbus/autodoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@

from sphinx.ext.autodoc import AttributeDocumenter, MethodDocumenter

from .dbus_proxy_async_method import DbusMethodAsyncClassBind
from .dbus_proxy_async_property import (
DbusPropertyAsync,
DbusPropertyAsyncClassBind,
)
from .dbus_proxy_async_signal import DbusSignalAsync, DbusSignalAsyncClassBind
from .dbus_proxy_async_method import DbusMethodAsync
from .dbus_proxy_async_property import DbusPropertyAsync
from .dbus_proxy_async_signal import DbusSignalAsync

if TYPE_CHECKING:
from typing import Any, Dict
Expand All @@ -44,14 +41,12 @@ class DbusMethodDocumenter(MethodDocumenter):

@classmethod
def can_document_member(cls, member: Any, *args: Any) -> bool:
return isinstance(member, DbusMethodAsyncClassBind)
return isinstance(member, DbusMethodAsync)

def import_object(self, raiseerror: bool = False) -> bool:
self.objpath.append('dbus_method')
self.objpath.append('original_method')
ret = super().import_object(raiseerror)
self.objpath.pop()
self.objpath.pop()
return ret

def add_content(self,
Expand All @@ -68,20 +63,13 @@ def add_content(self,

class DbusPropertyDocumenter(AttributeDocumenter):

objtype = 'DbusPropertyAsyncClassBind'
objtype = 'DbusPropertyAsync'
directivetype = 'attribute'
priority = 100 + AttributeDocumenter.priority

@classmethod
def can_document_member(cls, member: Any, *args: Any) -> bool:
return isinstance(member, DbusPropertyAsyncClassBind)

def import_object(self, raiseerror: bool = False) -> bool:

self.objpath.append('dbus_property')
ret = super().import_object(raiseerror)
self.objpath.pop()
return ret
return isinstance(member, DbusPropertyAsync)

def add_content(self,
*args: Any, **kwargs: Any,
Expand Down Expand Up @@ -109,20 +97,13 @@ def add_content(self,

class DbusSignalDocumenter(AttributeDocumenter):

objtype = 'DbusSignalAsyncClassBind'
objtype = 'DbusSignalAsync'
directivetype = 'attribute'
priority = 100 + AttributeDocumenter.priority

@classmethod
def can_document_member(cls, member: Any, *args: Any) -> bool:
return isinstance(member, DbusSignalAsyncClassBind)

def import_object(self, raiseerror: bool = False) -> bool:

self.objpath.append('dbus_signal')
ret = super().import_object(raiseerror)
self.objpath.pop()
return ret
return isinstance(member, DbusSignalAsync)

def add_content(self,
*args: Any, **kwargs: Any,
Expand Down
15 changes: 5 additions & 10 deletions src/sdbus/dbus_proxy_async_interface_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,9 @@
DbusSomethingSync,
)
from .dbus_common_funcs import get_default_bus
from .dbus_proxy_async_method import (
DbusMethodAsync,
DbusMethodAsyncClassBind,
DbusMethodAsyncLocalBind,
)
from .dbus_proxy_async_method import DbusMethodAsync, DbusMethodAsyncLocalBind
from .dbus_proxy_async_property import (
DbusPropertyAsync,
DbusPropertyAsyncClassBind,
DbusPropertyAsyncLocalBind,
)
from .dbus_proxy_async_signal import DbusSignalAsync, DbusSignalAsyncLocalBind
Expand Down Expand Up @@ -162,12 +157,12 @@ def __new__(cls, name: str,

super_element = getattr(base, collision_name)
dbus_element_override: DbusSomethingAsync
if isinstance(super_element, DbusMethodAsyncClassBind):
dbus_element_override = copy(super_element.dbus_method)
if isinstance(super_element, DbusMethodAsync):
dbus_element_override = copy(super_element)
dbus_element_override.original_method = cast(
MethodType, override.original)
elif isinstance(super_element, DbusPropertyAsyncClassBind):
dbus_element_override = copy(super_element.dbus_property)
elif isinstance(super_element, DbusPropertyAsync):
dbus_element_override = copy(super_element)
dbus_element_override.property_getter = cast(
Callable[[DbusInterfaceBaseAsync], Any],
override.original)
Expand Down
39 changes: 25 additions & 14 deletions src/sdbus/dbus_proxy_async_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from contextvars import ContextVar, copy_context
from inspect import iscoroutinefunction
from types import FunctionType
from typing import TYPE_CHECKING, cast
from typing import TYPE_CHECKING, cast, overload
from weakref import ref as weak_ref

from .dbus_common_elements import (
Expand All @@ -36,7 +36,7 @@
from .sd_bus_internals import DbusNoReplyFlag

if TYPE_CHECKING:
from typing import Any, Callable, Optional, Sequence, Type, TypeVar
from typing import Any, Callable, Optional, Sequence, Type, TypeVar, Union

from .dbus_proxy_async_interface_base import DbusInterfaceBaseAsync
from .sd_bus_internals import SdBusMessage
Expand All @@ -53,18 +53,36 @@ def get_current_message() -> SdBusMessage:


class DbusMethodAsync(DbusMethodCommon, DbusSomethingAsync):
def __get__(self,
obj: Optional[DbusInterfaceBaseAsync],
obj_class: Optional[Type[DbusInterfaceBaseAsync]] = None,
) -> Callable[..., Any]:

@overload
def __get__(
self,
obj: None,
obj_class: Type[DbusInterfaceBaseAsync],
) -> DbusMethodAsync:
...

@overload
def __get__(
self,
obj: DbusInterfaceBaseAsync,
obj_class: Type[DbusInterfaceBaseAsync],
) -> Callable[..., Any]:
...

def __get__(
self,
obj: Optional[DbusInterfaceBaseAsync],
obj_class: Optional[Type[DbusInterfaceBaseAsync]] = None,
) -> Union[Callable[..., Any], DbusMethodAsync]:
if obj is not None:
dbus_meta = obj._dbus
if isinstance(dbus_meta, DbusRemoteObjectMeta):
return DbusMethodAsyncProxyBind(self, dbus_meta)
else:
return DbusMethodAsyncLocalBind(self, obj)
else:
return DbusMethodAsyncClassBind(self)
return self


class DbusMethodAsyncBaseBind(DbusBindedAsync):
Expand Down Expand Up @@ -214,13 +232,6 @@ async def _dbus_reply_call(
reply_message.send()


class DbusMethodAsyncClassBind(DbusMethodAsyncBaseBind):
def __init__(self, dbus_method: DbusMethodAsync):
self.dbus_method = dbus_method

self.__doc__ = dbus_method.__doc__


def dbus_method_async(
input_signature: str = "",
result_signature: str = "",
Expand Down
38 changes: 24 additions & 14 deletions src/sdbus/dbus_proxy_async_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from inspect import iscoroutinefunction
from types import FunctionType
from typing import TYPE_CHECKING, Awaitable, Generic, TypeVar, cast
from typing import TYPE_CHECKING, Awaitable, Generic, TypeVar, cast, overload
from weakref import ref as weak_ref

from .dbus_common_elements import (
Expand All @@ -33,7 +33,7 @@
)

if TYPE_CHECKING:
from typing import Any, Callable, Generator, Optional, Type
from typing import Any, Callable, Generator, Optional, Type, Union

from .dbus_proxy_async_interface_base import DbusInterfaceBaseAsync
from .sd_bus_internals import SdBusMessage
Expand Down Expand Up @@ -71,18 +71,35 @@ def __init__(

self.__doc__ = property_getter.__doc__

def __get__(self,
obj: Optional[DbusInterfaceBaseAsync],
obj_class: Optional[Type[DbusInterfaceBaseAsync]] = None,
) -> DbusPropertyAsyncBaseBind[T]:
@overload
def __get__(
self,
obj: None,
obj_class: Type[DbusInterfaceBaseAsync],
) -> DbusPropertyAsync[T]:
...

@overload
def __get__(
self,
obj: DbusInterfaceBaseAsync,
obj_class: Type[DbusInterfaceBaseAsync],
) -> DbusPropertyAsyncBaseBind[T]:
...

def __get__(
self,
obj: Optional[DbusInterfaceBaseAsync],
obj_class: Optional[Type[DbusInterfaceBaseAsync]] = None,
) -> Union[DbusPropertyAsyncBaseBind[T], DbusPropertyAsync[T]]:
if obj is not None:
dbus_meta = obj._dbus
if isinstance(dbus_meta, DbusRemoteObjectMeta):
return DbusPropertyAsyncProxyBind(self, dbus_meta)
else:
return DbusPropertyAsyncLocalBind(self, obj)
else:
return DbusPropertyAsyncClassBind(self)
return self

def setter(self,
new_set_function: Callable[
Expand Down Expand Up @@ -254,13 +271,6 @@ def _dbus_reply_set(self, message: SdBusMessage) -> None:
)


class DbusPropertyAsyncClassBind(DbusPropertyAsyncBaseBind[T]):
def __init__(self, dbus_property: DbusPropertyAsync[T]):
self.dbus_property = dbus_property

self.__doc__ = dbus_property.__doc__


def dbus_property_async(
property_signature: str = "",
flags: int = 0,
Expand Down

0 comments on commit aaf63a3

Please sign in to comment.