1
+ "Convenience wrappers around dbus-python"
2
+
1
3
import dbus
2
4
import functools
3
5
from func import *
4
6
5
7
def object_path (o ):
8
+ """Return the object path of o.
9
+
10
+ If o is a proxy object, use its appropriate attribute.
11
+ Otherwise assume that o already is an object path.
12
+ """
6
13
if isinstance (o , dbus .proxies .ProxyObject ):
7
14
return o .object_path
8
15
# hope it is ok
9
16
return o
10
17
11
18
class DBusMio (dbus .proxies .ProxyObject ):
12
- """Multi-interface object
19
+ """Multi-interface object.
20
+
21
+ Will look into introspection data to find which interface
22
+ to use for a method or a property, obviating the need for
23
+ dbus.proxies.Interface.
24
+ If introspection is not available, provide default_interface
25
+ to the constructor.
13
26
14
27
BUGS: 1st method call will block with introspection"""
15
28
16
29
def __init__ (self , conn = None , bus_name = None , object_path = None , introspect = True , follow_name_owner_changes = False , ** kwargs ):
30
+ """Constructor.
31
+
32
+ kwargs may contain default_interface, to be used
33
+ if introspection does not provide it for a method/property
34
+ """
35
+
17
36
# FIXME common for this class, all classes?
18
37
self .__default_interface = kwargs .pop ("default_interface" , None )
19
38
super (DBusMio , self ).__init__ (conn , bus_name , object_path , introspect , follow_name_owner_changes , ** kwargs )
20
39
21
40
def __getattr__ (self , name ):
41
+ """Proxied DBus methods.
42
+
43
+ Uses introspection or default_interface to find the interface.
44
+ """
22
45
# TODO cache
23
46
# iface = self._interface_cache.get(name)
24
47
# if iface == None:
@@ -37,12 +60,30 @@ def __getattr__(self, name):
37
60
38
61
# properties
39
62
def __getitem__ (self , key ):
63
+ """Proxies DBus properties as dictionary items.
64
+
65
+ a = DBusMio(...)
66
+ p = a["Prop"]
67
+
68
+ Uses default_interface (because dbus.proxies.ProxyObject
69
+ does not store introspection data for properties, boo. TODO.)
70
+ """
71
+
40
72
iface = self .__default_interface # TODO cache
41
73
# TODO _introspect_property_map
42
74
pmi = dbus .Interface (self , "org.freedesktop.DBus.Properties" )
43
75
return pmi .Get (iface , key )
44
76
45
77
def __setitem__ (self , key , value ):
78
+ """Proxies DBus properties as dictionary items.
79
+
80
+ a = DBusMio(...)
81
+ a["Prop"] = "Hello"
82
+
83
+ Uses default_interface (because dbus.proxies.ProxyObject
84
+ does not store introspection data for properties, boo. TODO.)
85
+ """
86
+
46
87
iface = self .__default_interface # TODO cache
47
88
# TODO _introspect_property_map
48
89
pmi = dbus .Interface (self , "org.freedesktop.DBus.Properties" )
@@ -69,7 +110,10 @@ class DBusClient(DBusMio):
69
110
def _get_adaptor (cls , kind , name ):
70
111
# print "GET", cls, kind, name
71
112
try :
72
- return cls ._adaptors [kind ][name ]
113
+ a = cls ._adaptors [kind ][name ]
114
+ # print ">", a
115
+ # TODO cache somehow?
116
+ return a
73
117
except KeyError :
74
118
scls = cls .__mro__ [1 ] # can use "super"? how?
75
119
try :
@@ -96,7 +140,8 @@ def _add_adaptor(cls, kind, name, adaptor):
96
140
@classmethod
97
141
def _add_adaptors (cls , * args , ** kwargs ):
98
142
"""
99
- either
143
+ a nested dictionary of kind:name:adaptor,
144
+ either as kwargs (new) or as a single dict arg (old, newest)
100
145
"""
101
146
if not cls .__dict__ .has_key ("_adaptors" ):
102
147
# do not use inherited attribute
0 commit comments