Skip to content

Commit

Permalink
Added internal D-Bus to Python name mapping to proxy classes
Browse files Browse the repository at this point in the history
  • Loading branch information
igo95862 committed Jul 30, 2022
1 parent 2ae39ae commit fb97f75
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/sdbus/dbus_proxy_async_interface_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def __new__(cls, name: str,
serving_enabled: bool = True,
) -> DbusInterfaceMetaAsync:

dbus_to_python_name_map: Dict[str, str] = {}
declared_interfaces = set()
# Set interface name
for key, value in namespace.items():
Expand All @@ -77,14 +78,26 @@ def __new__(cls, name: str,
value.serving_enabled = serving_enabled
declared_interfaces.add(key)

if isinstance(value, DbusMethodAsync):
dbus_to_python_name_map[value.method_name] = key
elif isinstance(value, DbusPropertyAsync):
dbus_to_python_name_map[value.property_name] = key
elif isinstance(value, DbusSignalAsync):
dbus_to_python_name_map[value.signal_name] = key

super_declared_interfaces = set()
for base in bases:
if issubclass(base, DbusInterfaceBaseAsync):
super_declared_interfaces.update(
base._dbus_declared_interfaces)

dbus_to_python_name_map.update(
base._dbus_to_python_name_map
)

for key in super_declared_interfaces & namespace.keys():
value = namespace[key]

if isinstance(value, DbusOverload):
for base in bases:
try:
Expand Down Expand Up @@ -114,7 +127,7 @@ def __new__(cls, name: str,
" without using @dbus_overload decorator")

namespace['_dbus_declared_interfaces'] = declared_interfaces

namespace['_dbus_to_python_name_map'] = dbus_to_python_name_map
namespace['_dbus_interface_name'] = interface_name
namespace['_dbus_serving_enabled'] = serving_enabled
new_cls = super().__new__(
Expand All @@ -130,6 +143,7 @@ class DbusInterfaceBaseAsync(metaclass=DbusInterfaceMetaAsync):
_dbus_declared_interfaces: Set[str]
_dbus_interface_name: Optional[str]
_dbus_serving_enabled: bool
_dbus_to_python_name_map: Dict[str, str]

def __init__(self) -> None:
self._activated_interfaces: List[SdBusInterface] = []
Expand Down
14 changes: 14 additions & 0 deletions src/sdbus/dbus_proxy_sync_interface_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
DbusSomethingSync,
)
from .dbus_common_funcs import get_default_bus
from .dbus_proxy_sync_method import DbusMethodSync
from .dbus_proxy_sync_property import DbusPropertySync
from .sd_bus_internals import SdBus


Expand All @@ -38,6 +40,7 @@ def __new__(cls, name: str,
serving_enabled: bool = True,
) -> DbusInterfaceMetaSync:

dbus_to_python_name_map: Dict[str, str] = {}
declared_interfaces = set()
# Set interface name
for key, value in namespace.items():
Expand All @@ -50,17 +53,27 @@ def __new__(cls, name: str,
value.serving_enabled = serving_enabled
declared_interfaces.add(key)

if isinstance(value, DbusMethodSync):
dbus_to_python_name_map[value.method_name] = key
elif isinstance(value, DbusPropertySync):
dbus_to_python_name_map[value.property_name] = key

super_declared_interfaces = set()
for base in bases:
if issubclass(base, DbusInterfaceBase):
super_declared_interfaces.update(
base._dbus_declared_interfaces)

dbus_to_python_name_map.update(
base._dbus_to_python_name_map
)

for key in super_declared_interfaces & namespace.keys():
raise TypeError("Attempted to overload dbus definition"
" sync interfaces do not support overloading")

namespace['_dbus_declared_interfaces'] = declared_interfaces
namespace['_dbus_to_python_name_map'] = dbus_to_python_name_map

new_cls = super().__new__(
cls, name, bases, namespace,
Expand All @@ -74,6 +87,7 @@ def __new__(cls, name: str,
class DbusInterfaceBase(metaclass=DbusInterfaceMetaSync):
_dbus_declared_interfaces: Set[str]
_dbus_serving_enabled: bool
_dbus_to_python_name_map: Dict[str, str]

def __init__(
self,
Expand Down
16 changes: 16 additions & 0 deletions test/test_sd_bus_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,22 @@ def test_property_setter(self, var: str) -> None:
self.assertEqual(test_var[0], await test_subclass.test_property)
self.assertEqual('12345', await test_subclass.test_property)

with self.subTest('Test dbus to python mapping'):
self.assertIn(
test_object.properties_changed.dbus_signal.signal_name,
test_object._dbus_to_python_name_map,
)

self.assertIn(
test_subclass.properties_changed.dbus_signal.signal_name,
test_subclass._dbus_to_python_name_map,
)

self.assertIn(
test_subclass.test_property.dbus_property.property_name,
test_subclass._dbus_to_python_name_map,
)

async def test_bad_subclass(self) -> None:
def bad_call() -> None:
class TestInheritnce(TestInterface):
Expand Down
6 changes: 6 additions & 0 deletions test/test_sd_bus_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ def test_invalid_assignment() -> None:

self.assertTrue(s.get_name_owner('org.example.test'))

with self.subTest('Test dbus to python name map'):
self.assertEqual(
'features',
s._dbus_to_python_name_map['Features'],
)

def test_docstring(self) -> None:
from pydoc import getdoc

Expand Down

0 comments on commit fb97f75

Please sign in to comment.