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