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

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