-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathdbus_proxy_sync_property.py
More file actions
138 lines (109 loc) · 4.38 KB
/
dbus_proxy_sync_property.py
File metadata and controls
138 lines (109 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# SPDX-License-Identifier: LGPL-2.1-or-later
# Copyright (C) 2020-2022 igo95862
# This file is part of python-sdbus
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from __future__ import annotations
from inspect import iscoroutinefunction
from types import FunctionType
from typing import TYPE_CHECKING, Generic, TypeVar, cast
from .dbus_common_elements import DbusMemberSync, DbusPropertyCommon
from .dbus_common_funcs import _check_sync_in_async_env
if TYPE_CHECKING:
from collections.abc import Callable
from typing import Any, Optional
from .dbus_proxy_sync_interface_base import DbusInterfaceBase
T = TypeVar('T')
class DbusPropertySync(DbusPropertyCommon, DbusMemberSync, Generic[T]):
def __init__(
self,
property_name: Optional[str],
property_signature: str,
property_getter: Callable[[DbusInterfaceBase],
T],
property_setter: Optional[
Callable[[DbusInterfaceBase, T],
None]],
flags: int,
) -> None:
assert isinstance(property_getter, FunctionType)
super().__init__(
property_name,
property_signature,
flags,
property_getter,
)
self.property_getter = property_getter
self.property_setter = property_setter
self.__doc__ = property_getter.__doc__
def __get__(self,
obj: DbusInterfaceBase,
obj_class: Optional[type[DbusInterfaceBase]] = None,
) -> T:
assert _check_sync_in_async_env(), (
"Used sync __get__ method in async environment. "
"This is probably an error as it will block "
"other asyncio methods for considerable time."
)
new_call_message = (
obj._dbus.attached_bus.new_property_get_message(
obj._dbus.service_name,
obj._dbus.object_path,
self.interface_name,
self.property_name,
)
)
reply_message = obj._dbus.attached_bus.call(new_call_message)
return cast(T, reply_message.get_contents()[1])
def __set__(self, obj: DbusInterfaceBase, value: T) -> None:
assert _check_sync_in_async_env(), (
"Used sync __set__ method in async environment. "
"This is probably an error as it will block "
"other asyncio methods for considerable time."
)
if not self.property_signature:
raise AttributeError('D-Bus property is read only')
new_call_message = (
obj._dbus.attached_bus.new_property_set_message(
obj._dbus.service_name,
obj._dbus.object_path,
self.interface_name,
self.property_name,
)
)
new_call_message.append_data(
'v', (self.property_signature, value))
obj._dbus.attached_bus.call(new_call_message)
def dbus_property(
property_signature: str = "",
flags: int = 0,
property_name: Optional[str] = None,
) -> Callable[
[Callable[[Any], T]],
DbusPropertySync[T]]:
assert not isinstance(property_signature, FunctionType), (
"Passed function to decorator directly. "
"Did you forget () round brackets?"
)
def property_decorator(
function: Callable[..., Any]
) -> DbusPropertySync[T]:
assert not iscoroutinefunction(function), "Expected regular function"
new_wrapper: DbusPropertySync[T] = DbusPropertySync(
property_name,
property_signature,
function,
None,
flags,
)
return new_wrapper
return property_decorator