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

@@ -48,10 +48,10 @@ def create_profile(name):
s_wired = NM.SettingWired.new()
s_ip4 = NM.SettingIP4Config.new()
s_ip4.set_property(NM.SETTING_IP4_CONFIG_METHOD, "auto")
s_ip4.set_property(NM.SETTING_IP_CONFIG_METHOD, "auto")
s_ip6 = NM.SettingIP6Config.new()
s_ip6.set_property(NM.SETTING_IP6_CONFIG_METHOD, "auto")
s_ip6.set_property(NM.SETTING_IP_CONFIG_METHOD, "auto")
profile.add_setting(s_con)
profile.add_setting(s_ip4)
@@ -64,11 +64,12 @@ def create_profile(name):
return profile
# callback function
def added_cb(settings, con, error, data):
if error is (None):
def added_cb(client, result, data):
try:
client.add_connection_finish(result)
print("The connection profile has been succesfully added to NetworkManager.")
else:
print(error)
except Exception, e:
print("Error: %s" % e)
main_loop.quit()
if __name__ == "__main__":
@@ -85,17 +86,14 @@ if __name__ == "__main__":
main_loop = GLib.MainLoop()
# create RemoteSettings object
settings = NM.RemoteSettings.new(None)
# create Client object
client = NM.Client.new(None)
# create a connection profile for NM
con = create_profile(profile_name)
# send the connection to NM
if persistent:
settings.add_connection(con, added_cb, None)
else:
settings.add_connection_unsaved(con, added_cb, None)
client.add_connection_async(con, persistent, None, added_cb, None)
main_loop.run()

View File

@@ -48,8 +48,8 @@ if __name__ == "__main__":
sys.exit('Usage: %s <connection name or UUID> [new zone]' % sys.argv[0])
main_loop = GLib.MainLoop()
settings = NM.RemoteSettings.new(None)
connections = settings.list_connections()
client = NM.Client.new(None)
connections = client.get_connections()
con_name = sys.argv[1]
if len(sys.argv) == 3:

View File

@@ -20,7 +20,7 @@
# Copyright 2014 Red Hat, Inc.
#
import sys, socket, struct
import sys, socket
from gi.repository import GLib, NM
#
@@ -28,11 +28,11 @@ from gi.repository import GLib, NM
# from NMIP4Config and NMIP6Config (got out of NMDevice)
#
def show_addresses(self, family):
def show_addresses(dev, family):
if (family == socket.AF_INET):
ip_cfg = self.get_ip4_config()
ip_cfg = dev.get_ip4_config()
else:
ip_cfg = self.get_ip6_config()
ip_cfg = dev.get_ip6_config()
if ip_cfg is None:
print("None")
@@ -46,24 +46,29 @@ def show_addresses(self, family):
for nm_address in nm_addresses:
addr = nm_address.get_address()
prefix = nm_address.get_prefix()
gateway = nm_address.get_gateway()
if (family == socket.AF_INET):
addr_struct = struct.pack("=I", addr)
gateway_struct = struct.pack("=I", gateway)
else:
addr_struct = addr
gateway_struct = gateway
print("%s/%d %s") % (socket.inet_ntop(family, addr_struct),
prefix,
socket.inet_ntop(family, gateway_struct))
print("%s/%d") % (addr, prefix)
def show_routes(self, family):
def show_gateway(dev, family):
if (family == socket.AF_INET):
ip_cfg = self.get_ip4_config()
ip_cfg = dev.get_ip4_config()
else:
ip_cfg = self.get_ip6_config()
ip_cfg = dev.get_ip6_config()
if ip_cfg is None:
gw = "None"
else:
gw = ip_cfg.get_gateway()
if gw == '':
gw = "None"
print(gw)
def show_routes(dev, family):
if (family == socket.AF_INET):
ip_cfg = dev.get_ip4_config()
else:
ip_cfg = dev.get_ip6_config()
if ip_cfg is None:
print("None")
@@ -80,23 +85,14 @@ def show_routes(self, family):
next_hop = nm_route.get_next_hop()
metric = nm_route.get_metric()
if (family == socket.AF_INET):
dest_struct = struct.pack("=I", dest)
next_hop_struct = struct.pack("=I", next_hop)
else:
dest_struct = dest
next_hop_struct = next_hop
print("%s/%d %s %d") % (socket.inet_ntop(family, dest_struct),
prefix,
socket.inet_ntop(family, next_hop_struct),
metric)
print("%s/%d %s %d") % (dest, prefix, next_hop, metric)
def show_dns(self, family):
def show_dns(dev, family):
if (family == socket.AF_INET):
ip_cfg = self.get_ip4_config()
ip_cfg = dev.get_ip4_config()
else:
ip_cfg = self.get_ip6_config()
ip_cfg = dev.get_ip6_config()
if ip_cfg is None:
print("None")
@@ -126,6 +122,11 @@ if __name__ == "__main__":
show_addresses(dev, socket.AF_INET)
print
print("IPv4 gateway:")
print("-------------")
show_gateway(dev, socket.AF_INET)
print
print("IPv4 routes:")
print("------------")
show_routes(dev, socket.AF_INET)
@@ -136,6 +137,11 @@ if __name__ == "__main__":
show_addresses(dev, socket.AF_INET6)
print
print("IPv6 gateway:")
print("-------------")
show_gateway(dev, socket.AF_INET6)
print
print "IPv6 routes:"
print("------------")
show_routes(dev, socket.AF_INET6)

View File

@@ -23,21 +23,16 @@
from gi.repository import NM
# This example asks settings service for all configured connections.
# Unfortunately, at this time since libnm still makes heavy use of
# GValue and GHashTable (rather than GVariant), libnm isn't fully
# usable from GObject Introspection-ready languages. Most functions will
# work fine, but e.g. nm_connection_to_dbus() causes failures.
def print_values(setting, key, value, flags, data):
print " %s.%s: %s" % (setting.get_name(), key, value)
if __name__ == "__main__":
# create RemoteSettings object
settings = NM.RemoteSettings.new(None)
# create Client object
client = NM.Client.new(None)
# get all connections
connections = settings.list_connections()
connections = client.get_connections()
# print the connections' details
for c in connections:

View File

@@ -20,6 +20,7 @@
# Copyright (C) 2013 Red Hat, Inc.
#
import locale
from gi.repository import NM
#
@@ -31,22 +32,20 @@ from gi.repository import NM
# an error without it: http://www.python.org/dev/peps/pep-0263/
#
signal_bars = {
0 : "____",
1 : "▂___",
2 : "▂▄__",
3 : "▂▄▆_",
4 : "▂▄▆█"
}
def clamp(value, minvalue, maxvalue):
return max(minvalue, min(value, maxvalue))
def ssid_to_utf8(ap):
ssid = ap.get_ssid()
if not ssid:
return ""
return NM.utils_ssid_to_utf8(ap.get_ssid().get_data())
def print_device_info(device):
active_ap = dev.get_active_access_point()
ssid = None
if active_ap is not None:
ssid = active_ap.get_ssid()
ssid = ssid_to_utf8(active_ap)
info = "Device: %s | Driver: %s | Active AP: %s" % (dev.get_iface(), dev.get_driver(), ssid)
print info
print '=' * len(info)
@@ -54,14 +53,19 @@ def print_device_info(device):
def print_ap_info(ap):
strength = ap.get_strength()
frequency = ap.get_frequency()
print "SSID: %s" % (ap.get_ssid())
print "SSID: %s" % (ssid_to_utf8(ap))
print "BSSID: %s" % (ap.get_bssid())
print "Frequency: %s" % (frequency)
print "Channel: %s" % (NM.utils_wifi_freq_to_channel(frequency))
print "Strength: %s %s%%" % (signal_bars[(clamp(strength-5, 0, 99)+24)/25], strength)
print "Strength: %s %s%%" % (NM.utils_wifi_strength_bars(strength), strength)
print
if __name__ == "__main__":
# Python apparently doesn't call setlocale() on its own? We have to call this or else
# NM.utils_wifi_strength_bars() will think the locale is ASCII-only, and return the
# fallback characters rather than the unicode bars
locale.setlocale(locale.LC_ALL, '')
nmc = NM.Client.new(None)
devs = nmc.get_devices()

View File

@@ -24,22 +24,11 @@
# using the libnm GObject-based convenience APIs.
#
# Configuration settings are described at
# https://developer.gnome.org/NetworkManager/0.9/ref-settings.html
# https://developer.gnome.org/NetworkManager/1.0/ref-settings.html
#
from gi.repository import GLib, NM
import sys, struct, socket
def ip_to_int(ip_string):
return struct.unpack("=I", socket.inet_aton(ip_string))[0]
# callback function
def commit_cb(connection, error, data):
if error is (None):
print("The connection profile has been updated.")
else:
print(error)
main_loop.quit()
import sys, socket
if __name__ == "__main__":
# parse and validate arguments
@@ -48,7 +37,7 @@ if __name__ == "__main__":
sys.exit(1)
method = sys.argv[2]
if method == "static" or method == "manual" and len(sys.argv) < 5:
if (method == "static" or method == "manual") and len(sys.argv) < 5:
print "Usage: %s %s static address prefix [gateway]" % (sys.argv[0], sys.argv[1])
sys.exit(1)
@@ -60,10 +49,10 @@ if __name__ == "__main__":
main_loop = GLib.MainLoop()
# create RemoteSettings object
settings = NM.RemoteSettings.new(None)
# create Client object
client = NM.Client.new(None)
all_connections = settings.list_connections()
all_connections = client.get_connections()
for c in all_connections:
if c.get_uuid() != uuid:
continue
@@ -75,21 +64,22 @@ if __name__ == "__main__":
c.add_setting(s_ip4)
# set the method and change properties
s_ip4.set_property(NM.SETTING_IP4_CONFIG_METHOD, method)
s_ip4.set_property(NM.SETTING_IP_CONFIG_METHOD, method)
if method == "auto":
# remove addresses
# remove addresses and gateway
s_ip4.clear_addresses()
s_ip4.props.gateway = None
elif method == "manual":
# Add the static IP address, prefix, and (optional) gateway
addr = NM.IP4Address.new()
addr.set_address(ip_to_int(sys.argv[3]))
addr.set_prefix(int(sys.argv[4]))
if len(sys.argv) == 6:
addr.set_gateway(ip_to_int(sys.argv[5]))
addr = NM.IPAddress.new(socket.AF_INET, sys.argv[3], int(sys.argv[4]))
s_ip4.add_address(addr)
if len(sys.argv) == 6:
s_ip4.props.gateway = sys.argv[5]
c.commit_changes(commit_cb, None)
# run main loop
main_loop.run()
try:
c.commit_changes(True, None)
print("The connection profile has been updated.")
except Exception, e:
print("Error: %s" % e)
break