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
This commit is contained in:
Sayed Shah 2020-06-30 16:21:00 -04:00 committed by Thomas Haller
parent 4d878d7012
commit 7baf615eaa
No known key found for this signature in database
GPG Key ID: 29C2366E4DFC5728
15 changed files with 70 additions and 120 deletions

View File

@ -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()

View File

@ -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"

View File

@ -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"
)

View File

@ -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"
)

View File

@ -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 <ROLLBACK-INTERVAL> [INTERFACE]..." % sys.argv[0]
print("Usage: %s <ROLLBACK-INTERVAL> [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)

View File

@ -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):

View File

@ -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:

View File

@ -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")

View File

@ -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]:

View File

@ -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:

View File

@ -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]))

View File

@ -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.

View File

@ -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()

View File

@ -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()

View File

@ -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