/
ChangeConnection.py
72 lines (51 loc) · 1.97 KB
/
ChangeConnection.py
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
from cuttlefish.params import SelectParam
from cuttlefish.plugins import CuttlePlugin
from cuttlefish.actions import CuttleAction
import dbus
import subprocess
import logging
logger = logging.getLogger('cuttlefish.plugins.ChangeConnection')
DBUS_ADDR_NM = 'org.freedesktop.NetworkManager'
DBUS_PATH_NM = '/org/freedesktop/NetworkManager'
def get_known_connections():
connections = {}
bus = dbus.SystemBus()
settings_obj = bus.get_object(DBUS_ADDR_NM, DBUS_PATH_NM + '/Settings')
for connection_settings_path in settings_obj.ListConnections():
connection_settings_obj = bus.get_object(DBUS_ADDR_NM, connection_settings_path)
try:
connection_options = connection_settings_obj.GetSettings()
connection_id = str(connection_options['connection']['id'])
connections[connection_id] = connection_id
except:
continue
return connections
class ChangeConnection(CuttleAction):
CATEGORY = 'Network'
PARAMS = {
'connection_id' : ''
}
class Editor(CuttlePlugin.Editor):
def begin(self):
known_connections = get_known_connections()
return {
'connection_id' : SelectParam('Connection', known_connections, str)
}
def __init__(self, active):
CuttleAction.__init__(self)
self._active = active
def execute(self):
action = 'up' if self._active else 'down'
subprocess.check_output(['nmcli', 'con', action, 'id', self._params['connection_id']])
class ActivateConnection(ChangeConnection, CuttlePlugin):
NAME = 'Activate Network Connection'
DESCP = 'Activate a network connection which has been saved by NetworkManager.'
def __init__(self):
ChangeConnection.__init__(self, True)
CuttlePlugin.__init__(self)
class DeactivateConnection(ChangeConnection, CuttlePlugin):
NAME = 'Deactivate Network Connection'
DESCP = 'Deactivate a network connection which has been saved by NetworkManager.'
def __init__(self):
ChangeConnection.__init__(self, False)
CuttlePlugin.__init__(self)