python: Let RHashtable be returned as a Python dict.

The initial proposal was suggested by Matt Booth and discussed on the
mailing list here:

https://www.redhat.com/archives/libguestfs/2013-April/msg00007.html
This commit is contained in:
Richard W.M. Jones
2013-04-03 18:58:43 +01:00
parent 3dcb572bda
commit ba0199c487
10 changed files with 103 additions and 26 deletions

View File

@@ -5,7 +5,11 @@ import guestfs
output = "disk.img"
g = guestfs.GuestFS ()
# All new Python code should pass python_return_dict=True
# to the constructor. It indicates that your program wants
# to receive Python dicts for methods in the API that return
# hashtables.
g = guestfs.GuestFS (python_return_dict=True)
# Create a raw-format sparse disk image, 512 MB in size.
f = open (output, "w")

View File

@@ -7,7 +7,7 @@ guestfs-python - How to use libguestfs from Python
=head1 SYNOPSIS
import guestfs
g = guestfs.GuestFS ()
g = guestfs.GuestFS (python_return_dict=True)
g.add_drive_opts ("disk.img", format="raw", readonly=1)
g.launch ()
@@ -18,6 +18,17 @@ programming language. This page just documents the differences from
the C API and gives some examples. If you are not familiar with using
libguestfs, you also need to read L<guestfs(3)>.
=head2 python_return_dict=True
All new code should construct the handle using:
g = guestfs.GuestFS (python_return_dict=True)
This indicates that your program wants to receive Python dicts for
methods in the API that return hashtables.
In a future version of libguestfs, this will become the default.
=head2 EXCEPTIONS
Errors from libguestfs functions are mapped into C<RuntimeException>

View File

@@ -6,7 +6,11 @@ import guestfs
assert (len (sys.argv) == 2)
disk = sys.argv[1]
g = guestfs.GuestFS ()
# All new Python code should pass python_return_dict=True
# to the constructor. It indicates that your program wants
# to receive Python dicts for methods in the API that return
# hashtables.
g = guestfs.GuestFS (python_return_dict=True)
# Attach the disk image read-only to libguestfs.
g.add_drive_opts (disk, readonly=1)
@@ -35,17 +39,10 @@ for root in roots:
# Sort keys by length, shortest first, so that we end up
# mounting the filesystems in the correct order.
mps = g.inspect_get_mountpoints (root)
def compare (a, b):
if len(a[0]) > len(b[0]):
return 1
elif len(a[0]) == len(b[0]):
return 0
else:
return -1
mps.sort (compare)
for mp_dev in mps:
def compare (a, b): return len(a) - len(b)
for device in sorted (mps.keys(), compare):
try:
g.mount_ro (mp_dev[1], mp_dev[0])
g.mount_ro (mps[device], device)
except RuntimeError as msg:
print "%s (ignored)" % msg