Add DBUS communication in dbus.py, update README and add changes to sources

This commit is contained in:
Max Moser committed 2017-12-20 13:00:13 +01:00
1 parent d606210675
commit fb4cfa2413
4 files changed
+170 -18

No files matched your search

+34 -5
View File
@@ -24,12 +24,14 @@ show_introspect = True
def send_desktop_notification(title="Hello World", msg="pydbus works!"):
"""Send a notification to the desktop environment to display"""
bus = SessionBus()
notifications = bus.get('.Notifications')
notifications.Notify('test', 0, 'dialog-information', title, msg, [], {}, 5000)
def list_systemd_units():
"""Fetch all systemd units and print them"""
bus = SystemBus()
# systemd is now a proxy for the .systemd1 remote object
@@ -42,6 +44,7 @@ def list_systemd_units():
def stop_start_systemd_unit(name="ssh.service"):
"""Stop and restart a systemd unit"""
bus = SystemBus()
# systemd is now a proxy for the .systemd1 remote object
@@ -51,6 +54,7 @@ def stop_start_systemd_unit(name="ssh.service"):
def watch_for_new_systemd_jobs():
"""Wait for new systemd units and when they are created, print them out"""
bus = SystemBus()
# systemd is now a proxy for the .systemd1 remote object
@@ -76,8 +80,9 @@ def hibernate():
else:
print("Cannot hibernate")
except GLib.Error as e:
except Exception as ex:
print("Could not get PowerManager from DBUS")
print(str(ex))
def get_wg_plugin(bus_name="org.freedesktop.NetworkManager.wireguard",
@@ -98,15 +103,39 @@ def get_wg_plugin(bus_name="org.freedesktop.NetworkManager.wireguard",
def wg_disconnect(wg_plugin):
"""Disconnect the WG VPN plugin"""
wg_plugin.Disconnect()
def wg_connect(wg_plugin):
"""Send the Connect Command to the Wireguard Plugin"""
# these are the settings that are expected by Connect(a{sa{sv}}) for a VPN plugin
service_type = GLib.Variant("s", "service")
user_name = GLib.Variant("s", "wireguard")
persistent = GLib.Variant("b", False)
data = GLib.Variant("a{ss}", {"maxi": "cool"})
secrets = GLib.Variant("a{ss}", {"name": "maxi moser"})
timeout = GLib.Variant("u", 1337)
# The DBus type: a{sa{sv}}
# is a Dictionary with...
# Key: Type ("wireless", "wired", "vpn", ...) -- we want VPN
# Value: Dictionary with Key: Setting Name, Value: Setting Value
settings = {"vpn":
{"service-type": service_type,
"user-name": user_name,
"persistent": persistent,
"data": data,
"secrets": secrets,
"timeout": timeout}
}
wg_plugin.Connect(settings)
show_introspect = False
if __name__ == "__main__":
# send_desktop_notification("Guten Tag", "pydbus funktioniert, mein Herr!")
# list_systemd_units()
# hibernate()
try:
wg = get_wg_plugin()
@@ -114,7 +143,7 @@ if __name__ == "__main__":
print(wg.Introspect())
help(wg)
wg_disconnect(wg)
wg_connect(wg)
except Exception as ex:
print(str(ex))
print(str(ex))