10
10
import socket
11
11
import struct
12
12
import sys
13
+ import weakref
13
14
14
15
PY3 = sys .version_info >= (3 ,0 )
15
16
if PY3 :
@@ -26,17 +27,21 @@ def debug(msg, data):
26
27
except :
27
28
debug = lambda * args : None
28
29
30
+ auto_reconnect = True
31
+ registry = []
32
+
29
33
class NMDbusInterface (object ):
30
34
bus = dbus .SystemBus ()
31
35
dbus_service = 'org.freedesktop.NetworkManager'
32
36
object_path = None
33
37
34
38
def __init__ (self , object_path = None ):
39
+ global registry
35
40
if isinstance (object_path , NMDbusInterface ):
36
41
object_path = object_path .object_path
37
42
self .object_path = self .object_path or object_path
38
- self .proxy = self . bus . get_object ( self . dbus_service , self . object_path )
39
- self .interface = dbus . Interface ( self . proxy , self . interface_name )
43
+ self .signals = []
44
+ self .connect ( )
40
45
41
46
properties = []
42
47
try :
@@ -50,6 +55,25 @@ def __init__(self, object_path=None):
50
55
if not hasattr (self .__class__ , p ):
51
56
setattr (self .__class__ , p , self ._make_property (p ))
52
57
58
+ registry .append (weakref .proxy (self ))
59
+
60
+ def connect (self ):
61
+ self .proxy = self .bus .get_object (self .dbus_service , self .object_path )
62
+ self .interface = dbus .Interface (self .proxy , self .interface_name )
63
+ signals = self .signals [:]
64
+ self .signals = []
65
+ for signal , handler , args , kwargs in signals :
66
+ self .connect_to_signal (signal , handler , * args , ** kwargs )
67
+
68
+ def reconnect (self , name , old , new ):
69
+ if str (new ) == "" or str (name ) != 'org.freedesktop.NetworkManager' :
70
+ return
71
+ for obj in registry :
72
+ try :
73
+ obj .connect ()
74
+ except ReferenceError :
75
+ pass
76
+
53
77
def _make_property (self , name ):
54
78
def get_func (self ):
55
79
data = self .proxy .Get (self .interface_name , name , dbus_interface = 'org.freedesktop.DBus.Properties' )
@@ -121,6 +145,7 @@ def proxy_call(*args, **kwargs):
121
145
return proxy_call
122
146
123
147
def connect_to_signal (self , signal , handler , * args , ** kwargs ):
148
+ self .signals .append ((signal , handler , args , kwargs ))
124
149
def helper (* args , ** kwargs ):
125
150
args = [self .unwrap (x ) for x in args ]
126
151
handler (* args , ** kwargs )
@@ -138,6 +163,11 @@ class NetworkManager(NMDbusInterface):
138
163
interface_name = 'org.freedesktop.NetworkManager'
139
164
object_path = '/org/freedesktop/NetworkManager'
140
165
166
+ def auto_reconnect (self ):
167
+ global auto_reconnect
168
+ auto_reconnect = True
169
+ self .bus .add_signal_receiver (self .reconnect , 'NameOwnerChanged' , 'org.freedesktop.DBus' )
170
+
141
171
def preprocess (self , name , args , kwargs ):
142
172
if name in ('AddConnection' , 'Update' , 'UpdateUnsaved' , 'AddAndActivateConnection' ):
143
173
settings = copy .deepcopy (args [0 ])
0 commit comments