1
0
mirror of https://gitlab.com/libvirt/libvirt-python.git synced 2024-10-26 16:25:10 +03:00

fix examples

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.

It's mainly to make examples compatible with python3.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
This commit is contained in:
Pavel Hrdina 2014-11-26 18:47:17 +01:00
parent acc47bcb71
commit 39f19f0a2d
2 changed files with 22 additions and 12 deletions

View File

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

View File

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