Skip to content

Commit

Permalink
Added on_unknown_member option to the properties_get_all_dict
Browse files Browse the repository at this point in the history
Specifies the action on what to do with unknow property
  • Loading branch information
igo95862 committed Aug 3, 2022
1 parent 2a1c397 commit 5f71dc4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
3 changes: 3 additions & 0 deletions docs/asyncio_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ Classes
interface but the member names are automatically translated to python
names. (internally calls it for each interface used in class definition)

:param str on_unknown_member: If an unknown D-Bus property was encountered
either raise an ``"error"`` (default), ``"ignore"`` the property
or ``"reuse"`` the D-Bus name for the member.
:return: dictionary of properties
:rtype: Dict[str, Any]

Expand Down
3 changes: 3 additions & 0 deletions docs/sync_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ Classes
interface but the member names are automatically translated to python
names. (internally calls it for each interface used in class definition)

:param str on_unknown_member: If an unknown D-Bus property was encountered
either raise an ``"error"`` (default), ``"ignore"`` the property
or ``"reuse"`` the D-Bus name for the member.
:return: dictionary of properties
:rtype: Dict[str, Any]

Expand Down
22 changes: 19 additions & 3 deletions src/sdbus/dbus_proxy_async_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from __future__ import annotations

from inspect import getmembers
from typing import Any, Dict, List, Optional, Tuple
from typing import Any, Dict, List, Literal, Optional, Tuple

from .dbus_common_funcs import get_default_bus
from .dbus_proxy_async_interface_base import DbusInterfaceBaseAsync
Expand Down Expand Up @@ -87,14 +87,30 @@ async def _properties_get_all(
self, interface_name: str) -> Dict[str, Tuple[str, Any]]:
raise NotImplementedError

async def properties_get_all_dict(self) -> Dict[str, Any]:
async def properties_get_all_dict(
self,
on_unknown_member: Literal['error', 'ignore', 'reuse'] = 'error',
) -> Dict[str, Any]:

properties: Dict[str, Any] = {}

for interface_name in self._dbus_served_interfaces_names:
dbus_properties_data = await self._properties_get_all(
interface_name)

for member_name, variant in dbus_properties_data.items():
python_name = self._dbus_to_python_name_map[member_name]
try:
python_name = self._dbus_to_python_name_map[member_name]
except KeyError:
if on_unknown_member == 'error':
raise
elif on_unknown_member == 'ignore':
continue
elif on_unknown_member == 'reuse':
python_name = member_name
else:
raise ValueError

properties[python_name] = variant[1]

return properties
Expand Down
20 changes: 17 additions & 3 deletions src/sdbus/dbus_proxy_sync_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from __future__ import annotations

from typing import Any, Dict, Tuple
from typing import Any, Dict, Literal, Tuple

from .dbus_proxy_sync_interface_base import DbusInterfaceBase
from .dbus_proxy_sync_method import dbus_method
Expand Down Expand Up @@ -61,14 +61,28 @@ def _properties_get_all(
self, interface_name: str) -> Dict[str, Tuple[str, Any]]:
raise NotImplementedError

def properties_get_all_dict(self) -> Dict[str, Any]:
def properties_get_all_dict(
self,
on_unknown_member: Literal['error', 'ignore', 'reuse'] = 'error',
) -> Dict[str, Any]:
properties: Dict[str, Any] = {}

for interface_name in self._dbus_served_interfaces_names:
dbus_properties_data = self._properties_get_all(
interface_name)
for member_name, variant in dbus_properties_data.items():
python_name = self._dbus_to_python_name_map[member_name]
try:
python_name = self._dbus_to_python_name_map[member_name]
except KeyError:
if on_unknown_member == 'error':
raise
elif on_unknown_member == 'ignore':
continue
elif on_unknown_member == 'reuse':
python_name = member_name
else:
raise ValueError

properties[python_name] = variant[1]

return properties
Expand Down

0 comments on commit 5f71dc4

Please sign in to comment.