examples: update python examples

Update the raw D-Bus python examples to use newer APIs where
appropriate (and split the add-connection example into 1.0-only and
0.9-compatible versions). Update the gi-based python examples for the
various API changes since they were last updated.

Also add a comment to the ruby add-connection example pointing out
that it's still using the old settings APIs.
This commit is contained in:
Dan Winship
2014-11-13 14:27:26 -05:00
parent 94157ce900
commit 66936decfa
12 changed files with 227 additions and 137 deletions

View File

@@ -28,6 +28,8 @@ 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
@@ -42,34 +44,54 @@ proxy = bus.get_object(NM_SERVICE_NAME, "/org/freedesktop/NetworkManager")
manager = dbus.Interface(proxy, NM_MANAGER_IFACE)
props = dbus.Interface(proxy, DBUS_PROPS_IFACE)
default_is_wwan = False
def found_connection_type(ctype):
if ctype == '':
print "No active connection"
elif ctype in ["gsm", "cdma", "bluetooth"]:
print "WWAN is default"
else:
print "WWAN is not default"
sys.exit(0)
# Look through all active network connections for the default one
# 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("org.freedesktop.NetworkManager.Connection.Active")
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
# Shortcut: check for Type property (only present in NM 0.9.9+)
try:
ctype = all_props["Type"]
if ctype in ["gsm", "cdma", "bluetooth"]:
default_is_wwan = True
break
except KeyError:
# Fall back to checking the type of the network connection's device
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
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"