-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathparse.py
More file actions
270 lines (218 loc) · 7.77 KB
/
parse.py
File metadata and controls
270 lines (218 loc) · 7.77 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# SPDX-License-Identifier: LGPL-2.1-or-later
# Copyright (C) 2023 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 typing import TYPE_CHECKING
from ..dbus_common_funcs import _parse_properties_vardict
from ..dbus_proxy_async_interface_base import (
DBUS_CLASS_TO_META,
DBUS_INTERFACE_NAME_TO_CLASS,
DbusInterfaceBaseAsync,
)
if TYPE_CHECKING:
from typing import (
Any,
Dict,
FrozenSet,
Iterable,
List,
Literal,
Optional,
Tuple,
Type,
Union,
)
from ..dbus_proxy_async_interfaces import DBUS_PROPERTIES_CHANGED_TYPING
InterfacesInputElements = Union[
DbusInterfaceBaseAsync,
Type[DbusInterfaceBaseAsync],
]
InterfacesInput = Union[
InterfacesInputElements,
Iterable[InterfacesInputElements],
]
InterfacesToClassMap = Dict[FrozenSet[str], Type[DbusInterfaceBaseAsync]]
OnUnknownMember = Literal['error', 'ignore', 'reuse']
OnUnknownInterface = Literal['error', 'none']
ParseGetManaged = Dict[
str,
Tuple[Optional[Type[DbusInterfaceBaseAsync]], Dict[str, Any]],
]
def parse_properties_changed(
interface: InterfacesInputElements,
properties_changed_data: DBUS_PROPERTIES_CHANGED_TYPING,
on_unknown_member: OnUnknownMember = 'error',
) -> Dict[str, Any]:
interface_name, changed_properties, invalidated_properties = (
properties_changed_data
)
meta = DBUS_CLASS_TO_META[DBUS_INTERFACE_NAME_TO_CLASS[interface_name]]
for invalidated_property in invalidated_properties:
changed_properties[invalidated_property] = ('0', None)
return _parse_properties_vardict(
meta.dbus_member_to_python_attr,
properties_changed_data[1],
on_unknown_member,
)
SKIP_INTERFACES = frozenset((
'org.freedesktop.DBus.Properties',
'org.freedesktop.DBus.Introspectable',
'org.freedesktop.DBus.Peer',
'org.freedesktop.DBus.ObjectManager',
))
def _create_interfaces_map(
interfaces: InterfacesInput,
) -> InterfacesToClassMap:
if isinstance(interfaces,
(DbusInterfaceBaseAsync, type)):
interfaces_iter = iter((interfaces, ))
else:
interfaces_iter = iter(interfaces)
interfaces_to_class_map: InterfacesToClassMap = {}
for interface in interfaces_iter:
interface_names_set = frozenset(
interface_name for interface_name, _ in
interface._dbus_iter_interfaces_meta()
if interface_name not in SKIP_INTERFACES
)
interfaces_to_class_map[interface_names_set] = (
interface if isinstance(interface, type)
else type(interface)
)
return interfaces_to_class_map
def _get_class_from_interfaces(
interfaces_to_class_map: InterfacesToClassMap,
interface_names_iter: Iterable[str],
raise_key_error: bool,
) -> Optional[Type[DbusInterfaceBaseAsync]]:
class_set = frozenset(interface_names_iter) - SKIP_INTERFACES
try:
return interfaces_to_class_map[class_set]
except KeyError:
if raise_key_error:
raise
return None
def _get_member_map_from_class(
python_class: Optional[Type[DbusInterfaceBaseAsync]],
) -> Dict[str, Dict[str, str]]:
if python_class is None:
return {}
else:
return {
interface_name: meta.dbus_member_to_python_attr
for interface_name, meta in
python_class._dbus_iter_interfaces_meta()
}
def _translate_and_merge_members(
properties_data: Dict[str, Dict[str, Any]],
dbus_to_python_map: Dict[str, Dict[str, str]],
on_unknown_member: OnUnknownMember,
) -> Dict[str, Any]:
python_properties: Dict[str, Any] = {}
for interface_name, properties in properties_data.items():
interface_member_map = dbus_to_python_map.get(
interface_name, {},
)
python_properties.update(
_parse_properties_vardict(
interface_member_map,
properties,
on_unknown_member,
)
)
return python_properties
def parse_interfaces_added(
interfaces: InterfacesInput,
interfaces_added_data: Tuple[str, Dict[str, Dict[str, Any]]],
on_unknown_interface: OnUnknownInterface = 'error',
on_unknown_member: OnUnknownMember = 'error',
) -> Tuple[str, Optional[Type[DbusInterfaceBaseAsync]], Dict[str, Any]]:
interfaces_to_class_map = _create_interfaces_map(interfaces)
path, properties_data = interfaces_added_data
python_class = (
_get_class_from_interfaces(
interfaces_to_class_map,
properties_data.keys(),
on_unknown_interface == "error",
)
)
dbus_to_python_member_map = _get_member_map_from_class(python_class)
python_properties: Dict[str, Any] = {}
for interface_name, properties in properties_data.items():
interface_member_map = dbus_to_python_member_map.get(
interface_name, {},
)
python_properties.update(
_parse_properties_vardict(
interface_member_map,
properties,
on_unknown_member,
)
)
return (
path,
python_class,
_translate_and_merge_members(
properties_data,
dbus_to_python_member_map,
on_unknown_member,
),
)
def parse_interfaces_removed(
interfaces: InterfacesInput,
interfaces_removed_data: Tuple[str, List[str]],
on_unknown_interface: OnUnknownInterface = 'error',
) -> Tuple[str, Optional[Type[DbusInterfaceBaseAsync]]]:
interfaces_to_class_map = _create_interfaces_map(interfaces)
path, interfaces_removed = interfaces_removed_data
python_class = (
_get_class_from_interfaces(
interfaces_to_class_map,
interfaces_removed,
on_unknown_interface == "error",
)
)
return path, python_class
def parse_get_managed_objects(
interfaces: InterfacesInput,
managed_objects_data: Dict[str, Dict[str, Dict[str, Any]]],
on_unknown_interface: OnUnknownInterface = 'error',
on_unknown_member: OnUnknownMember = 'error',
) -> ParseGetManaged:
interfaces_to_class_map = _create_interfaces_map(interfaces)
managed_objects_map: ParseGetManaged = {}
for path, properties_data in managed_objects_data.items():
python_class = (
_get_class_from_interfaces(
interfaces_to_class_map,
properties_data.keys(),
on_unknown_interface == "error",
)
)
dbus_to_python_member_map = _get_member_map_from_class(python_class)
managed_objects_map[path] = (
python_class,
_translate_and_merge_members(
properties_data,
dbus_to_python_member_map,
on_unknown_member,
),
)
return managed_objects_map
__all__ = (
'parse_properties_changed',
'parse_interfaces_added',
'parse_interfaces_removed',
'parse_get_managed_objects',
)