[libvirt] [libvirt-python PATCH] fix examples

Pavel Hrdina phrdina at redhat.com
Fri Nov 28 08:57:28 UTC 2014


The dhcpleases example had an old usage of print function. The formating
of leases record was also wrong.

The event-test example had an old usage of exceptions.

Signed-off-by: Pavel Hrdina <phrdina at redhat.com>
---
 examples/dhcpleases.py | 30 ++++++++++++++++++++----------
 examples/event-test.py |  4 ++--
 2 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/examples/dhcpleases.py b/examples/dhcpleases.py
index c172dc2..da51f52 100755
--- a/examples/dhcpleases.py
+++ b/examples/dhcpleases.py
@@ -6,8 +6,8 @@ import sys
 import time
 
 def usage():
-    print "Usage: %s [URI] NETWORK" % sys.argv[0]
-    print "        Print leases info for a given virtual network"
+    print("Usage: %s [URI] NETWORK" % sys.argv[0])
+    print("        Print leases info for a given virtual network")
 
 uri = None
 network = None
@@ -24,18 +24,18 @@ else:
 
 conn = libvirt.open(uri)
 if conn == None:
-    print "Unable to open connection to libvirt"
+    print("Unable to open connection to libvirt")
     sys.exit(1)
 
 try:
     net = conn.networkLookupByName(network)
 except libvirt.libvirtError:
-    print "Network %s not found" % network
+    print("Network %s not found" % network)
     sys.exit(0)
 
 leases = net.DHCPLeases();
 if (leases == None):
-    print "Failed to get leases for %s" % net.name()
+    print("Failed to get leases for %s" % net.name())
     sys.exit(0)
 
 def toIPAddrType(addrType):
@@ -44,10 +44,20 @@ def toIPAddrType(addrType):
     elif addrType == libvirt.VIR_IP_ADDR_TYPE_IPV6:
         return "ipv6"
 
-print " {0:20} {1:18} {2:9} {3:25} {4:15} {5}".format("Expiry Time", "MAC address", "Protocol", "IP address", "Hostname", "Client ID or DUID")
-print "-"*115
+print(" {0:20} {1:18} {2:9} {3:25} {4:15} {5}".format("Expiry Time",
+                                                      "MAC address",
+                                                      "Protocol",
+                                                      "IP address",
+                                                      "Hostname",
+                                                      "Client ID or DUID"))
+print("-"*115)
 
 for lease in leases:
-    print " {0:20}".format(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(lease['expirytime']))),
-    print "{0:18} {1:9}".format(lease['mac'], toIPAddrType(lease['type'])),
-    print "{0:<25} {1:15} {2}".format("{}/{}".format(lease['ipaddr'], lease['prefix']), lease['hostname'], lease['clientid'])
+    print(" {0:20} {1:18} {2:9} {3:25} {4:15} {5}".format(
+        time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(lease['expirytime'])),
+        lease['mac'],
+        toIPAddrType(lease['type']),
+        "{}/{}".format(lease['ipaddr'], lease['prefix']),
+        lease['hostname'],
+        lease['clientid']
+    ))
diff --git a/examples/event-test.py b/examples/event-test.py
index 6cc33ce..452f659 100755
--- a/examples/event-test.py
+++ b/examples/event-test.py
@@ -220,7 +220,7 @@ class virEventLoopPure:
                     t.set_last_fired(now)
                     t.dispatch()
 
-        except (os.error, select.error), e:
+        except (os.error, select.error) as e:
             if e.args[0] != errno.EINTR:
                 raise
         finally:
@@ -576,7 +576,7 @@ def usage():
 def main():
     try:
         opts, args = getopt.getopt(sys.argv[1:], "hdl", ["help", "debug", "loop"])
-    except getopt.GetoptError, err:
+    except getopt.GetoptError as err:
         # print help information and exit:
         print(str(err)) # will print something like "option -a not recognized"
         usage()
-- 
2.0.4




More information about the libvir-list mailing list