From 7baf615eaa27ec38755fcaaf7430b0075087dc60 Mon Sep 17 00:00:00 2001 From: Sayed Shah Date: Tue, 30 Jun 2020 16:21:00 -0400 Subject: all: changes from python 2 syntax to python3 and removing dead code There were python 2 methods that are not used anymore so now it's in python 3. Added helper fuctions so that the code is more clear https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/540 --- examples/python/dbus/add-connection-compat.py | 9 +-- examples/python/dbus/add-connection.py | 4 +- examples/python/dbus/add-wifi-eap-connection.py | 3 +- examples/python/dbus/add-wifi-psk-connection.py | 3 +- examples/python/dbus/checkpoint.py | 16 +++--- examples/python/dbus/create-bond.py | 4 +- .../python/dbus/get-active-connection-uuids.py | 23 +++----- examples/python/dbus/is-wwan-default.py | 65 +++------------------- examples/python/dbus/list-connections.py | 1 - examples/python/dbus/list-devices.py | 14 ++++- examples/python/dbus/nm-state.py | 3 +- examples/python/dbus/show-bssids.py | 1 - examples/python/dbus/update-secrets.py | 3 +- examples/python/dbus/vpn.py | 10 ++-- examples/python/dbus/wifi-hotspot.py | 31 ++++++----- 15 files changed, 70 insertions(+), 120 deletions(-) diff --git a/examples/python/dbus/add-connection-compat.py b/examples/python/dbus/add-connection-compat.py index 6acec734bc..2661eccb40 100755 --- a/examples/python/dbus/add-connection-compat.py +++ b/examples/python/dbus/add-connection-compat.py @@ -16,16 +16,14 @@ import socket, struct, dbus, uuid -# Helper functions + def ip_to_int(ip_string): return struct.unpack("=I", socket.inet_aton(ip_string))[0] -def int_to_ip(ip_int): - return socket.inet_ntoa(struct.pack("=I", ip_int)) - - +# full duplex s_wired = dbus.Dictionary({"duplex": "full"}) + s_con = dbus.Dictionary( {"type": "802-3-ethernet", "uuid": str(uuid.uuid4()), "id": "MyConnectionExample"} ) @@ -47,7 +45,6 @@ con = dbus.Dictionary( {"802-3-ethernet": s_wired, "connection": s_con, "ipv4": s_ip4, "ipv6": s_ip6} ) - print("Creating connection:", s_con["id"], "-", s_con["uuid"]) bus = dbus.SystemBus() diff --git a/examples/python/dbus/add-connection.py b/examples/python/dbus/add-connection.py index ee2deb2633..a7c8947c3e 100755 --- a/examples/python/dbus/add-connection.py +++ b/examples/python/dbus/add-connection.py @@ -18,11 +18,13 @@ import dbus, uuid s_wired = dbus.Dictionary({"duplex": "full"}) + s_con = dbus.Dictionary( {"type": "802-3-ethernet", "uuid": str(uuid.uuid4()), "id": "MyConnectionExample"} ) addr1 = dbus.Dictionary({"address": "10.1.2.3", "prefix": dbus.UInt32(8)}) + s_ip4 = dbus.Dictionary( { "address-data": dbus.Array([addr1], signature=dbus.Signature("a{sv}")), @@ -37,9 +39,7 @@ con = dbus.Dictionary( {"802-3-ethernet": s_wired, "connection": s_con, "ipv4": s_ip4, "ipv6": s_ip6} ) - print("Creating connection:", s_con["id"], "-", s_con["uuid"]) - bus = dbus.SystemBus() proxy = bus.get_object( "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings" diff --git a/examples/python/dbus/add-wifi-eap-connection.py b/examples/python/dbus/add-wifi-eap-connection.py index 0386084da1..23962e768a 100755 --- a/examples/python/dbus/add-wifi-eap-connection.py +++ b/examples/python/dbus/add-wifi-eap-connection.py @@ -50,10 +50,9 @@ con = dbus.Dictionary( "ipv6": s_ip6, } ) - +print("Creating connection:", s_con["id"], "-", s_con["uuid"]) bus = dbus.SystemBus() - proxy = bus.get_object( "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings" ) diff --git a/examples/python/dbus/add-wifi-psk-connection.py b/examples/python/dbus/add-wifi-psk-connection.py index 3e4f9da56f..66d42e8204 100755 --- a/examples/python/dbus/add-wifi-psk-connection.py +++ b/examples/python/dbus/add-wifi-psk-connection.py @@ -30,10 +30,9 @@ con = dbus.Dictionary( "ipv6": s_ip6, } ) - +print("Creating connection:", s_con["id"], "-", s_con["uuid"]) bus = dbus.SystemBus() - proxy = bus.get_object( "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings" ) diff --git a/examples/python/dbus/checkpoint.py b/examples/python/dbus/checkpoint.py index 54b450daf3..6518584831 100755 --- a/examples/python/dbus/checkpoint.py +++ b/examples/python/dbus/checkpoint.py @@ -3,14 +3,14 @@ # # Copyright (C) 2016 Red Hat, Inc. # - -import dbus, sys - # This example takes a list of device interface names as a parameter # and tells NetworkManager to create a checkpoint on those devices. It # is then possible to restore or destroy the checkpoint. # Get a proxy for the base NetworkManager object + +import dbus, sys + bus = dbus.SystemBus() proxy = bus.get_object( "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager" @@ -20,7 +20,7 @@ allDevs = manager.GetDevices() def Usage(): - print "Usage: %s [INTERFACE]..." % sys.argv[0] + print("Usage: %s [INTERFACE]..." % sys.argv[0]) sys.exit(1) @@ -54,12 +54,12 @@ for arg in sys.argv[2:]: checkpoint = manager.CheckpointCreate(devList, interval, 1) # DESTROY_ALL -choice = raw_input("Do you want to rollback [y/n]? ").lower() +choice = input("Do you want to rollback [y/n]? ").lower() if choice == "y": - print "Rollback checkpoint" + print("Rollback checkpoint") results = manager.CheckpointRollback(checkpoint) for d in results: - print " - device %s: result %u" % (d, results[d]) + print(" - device %s: result %u" % (d, results[d])) else: - print "Destroy checkpoint" + print("Destroy checkpoint") manager.CheckpointDestroy(checkpoint) diff --git a/examples/python/dbus/create-bond.py b/examples/python/dbus/create-bond.py index 458c9c0618..77c2ece417 100755 --- a/examples/python/dbus/create-bond.py +++ b/examples/python/dbus/create-bond.py @@ -13,7 +13,7 @@ import dbus, sys, uuid from dbus.mainloop.glib import DBusGMainLoop -from gi.repository import GObject +from gi.repository import GLib DBusGMainLoop(set_as_default=True) @@ -94,7 +94,7 @@ ac = manager.ActivateConnection(bond_path, "/", "/") print("Activating bond: %s (%s)" % (bond_name, ac)) # Monitor the active bond connection -loop = GObject.MainLoop() +loop = GLib.MainLoop() def properties_changed(props): diff --git a/examples/python/dbus/get-active-connection-uuids.py b/examples/python/dbus/get-active-connection-uuids.py index e138ce60a3..3e80b603dc 100755 --- a/examples/python/dbus/get-active-connection-uuids.py +++ b/examples/python/dbus/get-active-connection-uuids.py @@ -6,9 +6,8 @@ import dbus -# This example takes a device interface name as a parameter and tells -# NetworkManager to disconnect that device, closing down any network -# connection it may have +# This example lists all of the active connections +# the system is connected to and prints it out bus = dbus.SystemBus() @@ -16,23 +15,15 @@ bus = dbus.SystemBus() m_proxy = bus.get_object( "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager" ) -manager = dbus.Interface(m_proxy, "org.freedesktop.NetworkManager") mgr_props = dbus.Interface(m_proxy, "org.freedesktop.DBus.Properties") -s_proxy = bus.get_object( - "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings" -) -settings = dbus.Interface(s_proxy, "org.freedesktop.NetworkManager.Settings") - -# Find the device the user wants to disconnect +# Find all active connections active = mgr_props.Get("org.freedesktop.NetworkManager", "ActiveConnections") + for a in active: a_proxy = bus.get_object("org.freedesktop.NetworkManager", a) - # Get the UUID directly; apps could use this to perform certain operations - # based on which network you're connected too a_props = dbus.Interface(a_proxy, "org.freedesktop.DBus.Properties") - uuid = a_props.Get("org.freedesktop.NetworkManager.Connection.Active", "Uuid") # Grab the connection object path so we can get all the connection's settings connection_path = a_props.Get( @@ -45,7 +36,11 @@ for a in active: settings = connection.GetSettings() print( "%s (%s) - %s" - % (settings["connection"]["id"], uuid, settings["connection"]["type"]) + % ( + settings["connection"]["id"], + settings["connection"]["uuid"], + settings["connection"]["type"], + ) ) if len(active) == 0: diff --git a/examples/python/dbus/is-wwan-default.py b/examples/python/dbus/is-wwan-default.py index 38dc1735c6..18279db0a3 100755 --- a/examples/python/dbus/is-wwan-default.py +++ b/examples/python/dbus/is-wwan-default.py @@ -8,30 +8,24 @@ import dbus, sys # This example indicates whether the default network connection is known to be WWAN -NM_DEVICE_TYPE_MODEM = 8 -NM_DEVICE_TYPE_BLUETOOTH = 5 -NM_SERVICE_NAME = "org.freedesktop.NetworkManager" -NM_MANAGER_IFACE = "org.freedesktop.NetworkManager" -DBUS_PROPS_IFACE = "org.freedesktop.DBus.Properties" - -NM_ACTIVE_CONNECTION_INTERFACE = "org.freedesktop.NetworkManager.Connection.Active" - bus = dbus.SystemBus() # Exit early if NetworkManager is not running proxy = bus.get_object("org.freedesktop.DBus", "/org/freedesktop/DBus") busdaemon = dbus.Interface(proxy, "org.freedesktop.DBus") -if not busdaemon.NameHasOwner(NM_SERVICE_NAME): +if not busdaemon.NameHasOwner("org.freedesktop.NetworkManager"): print("NetworkManager not running") sys.exit(1) # Get a proxy for the NetworkManager object -proxy = bus.get_object(NM_SERVICE_NAME, "/org/freedesktop/NetworkManager") -manager = dbus.Interface(proxy, NM_MANAGER_IFACE) -props = dbus.Interface(proxy, DBUS_PROPS_IFACE) +proxy = bus.get_object( + "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager" +) +props = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") - -def found_connection_type(ctype): +# Shortcut #1, for NM 1.0 +try: + ctype = props.Get("org.freedesktop.NetworkManager", "PrimaryConnectionType") if ctype == "": print("No active connection") elif ctype in ["gsm", "cdma", "bluetooth"]: @@ -40,48 +34,5 @@ def found_connection_type(ctype): print("WWAN is not default") sys.exit(0) - -# Shortcut #1, for NM 1.0 -try: - ctype = props.Get(NM_MANAGER_IFACE, "PrimaryConnectionType") - found_connection_type(ctype) -except KeyError: - pass - -# Shortcut #2, for NM 0.9.10 -try: - primary = props.Get(NM_MANAGER_IFACE, "PrimaryConnection") - if not primary: - found_connection_type("") - primary_proxy = bus.get_object(NM_SERVICE_NAME, primary) - primary_props = dbus.Interface(primary_proxy, DBUS_PROPS_IFACE) - ctype = primary_props.Get(NM_ACTIVE_CONNECTION_INTERFACE, "Type") - found_connection_type(ctype) except KeyError: pass - -# Fallback for NM 0.9.8 and earlier; look through all active network -# connections for the default one -default_is_wwan = False -active = props.Get(NM_MANAGER_IFACE, "ActiveConnections") -for a in active: - a_proxy = bus.get_object(NM_SERVICE_NAME, a) - a_props = dbus.Interface(a_proxy, DBUS_PROPS_IFACE) - all_props = a_props.GetAll(NM_ACTIVE_CONNECTION_INTERFACE) - - # Ignore this network connection if it's not default for IPv4 or IPv6 - if all_props["Default"] == False and all_props["Default6"] == False: - continue - - dev_path = all_props["Devices"][0] - dev_proxy = bus.get_object(NM_SERVICE_NAME, dev_path) - dev_props = dbus.Interface(dev_proxy, DBUS_PROPS_IFACE) - devtype = dev_props.Get("org.freedesktop.NetworkManager.Device", "DeviceType") - if devtype == NM_DEVICE_TYPE_MODEM or devtype == NM_DEVICE_TYPE_BLUETOOTH: - default_is_wwan = True - break - -if default_is_wwan: - print("WWAN is default") -else: - print("WWAN is not default") diff --git a/examples/python/dbus/list-connections.py b/examples/python/dbus/list-connections.py index 2e096b632c..81b32896ac 100755 --- a/examples/python/dbus/list-connections.py +++ b/examples/python/dbus/list-connections.py @@ -18,7 +18,6 @@ def merge_secrets(proxy, config, setting_name): # returns a dict of dicts mapping name::setting, where setting is a dict # mapping key::value. Each member of the 'setting' dict is a secret secrets = proxy.GetSecrets(setting_name) - # Copy the secrets into our connection config for setting in secrets: for key in secrets[setting]: diff --git a/examples/python/dbus/list-devices.py b/examples/python/dbus/list-devices.py index 882841e6a0..910bc4617f 100755 --- a/examples/python/dbus/list-devices.py +++ b/examples/python/dbus/list-devices.py @@ -4,11 +4,17 @@ # Copyright (C) 2011 - 2012 Red Hat, Inc. # -import dbus +import dbus, socket, struct # This example lists basic information about network interfaces known to NM # For the types see include/NetworkManager.h + + +def int_to_ip(ip_int): + return socket.inet_ntoa(struct.pack("=I", ip_int)) + + devtypes = { 1: "Ethernet", 2: "Wi-Fi", @@ -56,19 +62,23 @@ manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager") # Get all devices known to NM and print their properties devices = manager.GetDevices() + for d in devices: dev_proxy = bus.get_object("org.freedesktop.NetworkManager", d) prop_iface = dbus.Interface(dev_proxy, "org.freedesktop.DBus.Properties") props = prop_iface.GetAll("org.freedesktop.NetworkManager.Device") + print("============================") print("Interface: %s" % props["Interface"]) + print("Ip 4 Address: %s" % int_to_ip(props["Ip4Address"])) + print("Ip 4 Config: %s" % props["Ip4Config"]) + try: devtype = devtypes[props["DeviceType"]] except KeyError: devtype = "Unknown" print("Type: %s" % devtype) - print("Driver: %s" % props["Driver"]) try: diff --git a/examples/python/dbus/nm-state.py b/examples/python/dbus/nm-state.py index c380bd093c..4bc9285ef1 100755 --- a/examples/python/dbus/nm-state.py +++ b/examples/python/dbus/nm-state.py @@ -68,7 +68,6 @@ for d in devices: else: print("Device %s is not activated (state=%s)" % (name, device_states[state])) - # Get active connection state manager_prop_iface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") active = manager_prop_iface.Get("org.freedesktop.NetworkManager", "ActiveConnections") @@ -90,4 +89,4 @@ for a in active: con_details = con_iface.GetSettings() con_name = con_details["connection"]["id"] - print("Connection '%s' is %s" % (con_name, connectivity_states[state].lower())) + print("Connection '%s' is %s" % (con_name, connectivity_states[state])) diff --git a/examples/python/dbus/show-bssids.py b/examples/python/dbus/show-bssids.py index 1e7181fece..b56822fbe4 100755 --- a/examples/python/dbus/show-bssids.py +++ b/examples/python/dbus/show-bssids.py @@ -4,7 +4,6 @@ # Copyright (C) 2010 Red Hat, Inc. # - # This example prints out all the AP BSSIDs that all Wi-Fi devices on the # machine can see. Useful for location-based services like Skyhook that # can geolocate you based on the APs you can see. diff --git a/examples/python/dbus/update-secrets.py b/examples/python/dbus/update-secrets.py index d5c20b2d4b..5df25e5c1b 100755 --- a/examples/python/dbus/update-secrets.py +++ b/examples/python/dbus/update-secrets.py @@ -13,8 +13,7 @@ # values, and add them to the settings that we pass to Update(). # -import dbus -import sys +import dbus, sys bus = dbus.SystemBus() diff --git a/examples/python/dbus/vpn.py b/examples/python/dbus/vpn.py index ed38137280..814e4415d1 100755 --- a/examples/python/dbus/vpn.py +++ b/examples/python/dbus/vpn.py @@ -8,17 +8,15 @@ # Run this script without any arguments to list the available connection uuids. # The uuid of the connection to activate -CONNECTION_UUID = "ac6dc9b2-85ef-4311-83d8-add5d7db3f59" +CONNECTION_UUID = "c08142a4-00d9-45bd-a3b1-7610fe146374" # UID to use. Note that NM only allows the owner of the connection to activate it. # UID=1000 UID = 0 -import sys -import os -import dbus +import sys, os, dbus from dbus.mainloop.glib import DBusGMainLoop -import gobject +from gi.repository import GLib DBusGMainLoop(set_as_default=True) @@ -156,5 +154,5 @@ if get_active_connection_path(CONNECTION_UUID): print("Activating connection...") activate_connection(connection_path, device_path) -loop = gobject.MainLoop() +loop = GLib.MainLoop() loop.run() diff --git a/examples/python/dbus/wifi-hotspot.py b/examples/python/dbus/wifi-hotspot.py index a3c573a643..77582b6a3c 100755 --- a/examples/python/dbus/wifi-hotspot.py +++ b/examples/python/dbus/wifi-hotspot.py @@ -50,22 +50,15 @@ def usage(): bus = dbus.SystemBus() -service_name = "org.freedesktop.NetworkManager" -proxy = bus.get_object(service_name, "/org/freedesktop/NetworkManager/Settings") +proxy = bus.get_object( + "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings" +) settings = dbus.Interface(proxy, "org.freedesktop.NetworkManager.Settings") -if len(sys.argv) != 3: - usage() - -iface = sys.argv[1] -proxy = bus.get_object(service_name, "/org/freedesktop/NetworkManager") -nm = dbus.Interface(proxy, "org.freedesktop.NetworkManager") -devpath = nm.GetDeviceByIpIface(iface) - # Find our existing hotspot connection connection_path = None for path in settings.ListConnections(): - proxy = bus.get_object(service_name, path) + proxy = bus.get_object("org.freedesktop.NetworkManager", path) settings_connection = dbus.Interface( proxy, "org.freedesktop.NetworkManager.Settings.Connection" ) @@ -78,13 +71,25 @@ for path in settings.ListConnections(): if not connection_path: connection_path = settings.AddConnection(con) + +if len(sys.argv) != 3: + usage() + +# Get device using iface +iface = sys.argv[1] +proxy = bus.get_object( + "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager" +) +nm = dbus.Interface(proxy, "org.freedesktop.NetworkManager") +devpath = nm.GetDeviceByIpIface(iface) + # Now start or stop the hotspot on the requested device -proxy = bus.get_object(service_name, devpath) +proxy = bus.get_object("org.freedesktop.NetworkManager", devpath) device = dbus.Interface(proxy, "org.freedesktop.NetworkManager.Device") operation = sys.argv[2] if operation == "up": acpath = nm.ActivateConnection(connection_path, devpath, "/") - proxy = bus.get_object(service_name, acpath) + proxy = bus.get_object("org.freedesktop.NetworkManager", acpath) active_props = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") # Wait for the hotspot to start up -- cgit v1.2.1