example script for GObject
I tried using Javascript (gjs) with GObject-Introspection and did a port of the create_disk example which is attached below. Quite a bit of time was spent looking up stuff in the GLib reference and guessing how certain idioms translate to gjs. I used the traditional add_drive_opts method since I haven't been able to find how to specify the optional arguments to add_drive_opts. How does that work? Cheers, -Hilko
On Wed, Jan 25, 2012 at 12:02:41AM +0100, Hilko Bengen wrote:
I tried using Javascript (gjs) with GObject-Introspection and did a port of the create_disk example which is attached below.
Quite a bit of time was spent looking up stuff in the GLib reference and guessing how certain idioms translate to gjs.
I used the traditional add_drive_opts method since I haven't been able to find how to specify the optional arguments to add_drive_opts. How does that work?
var optargs = new Guestfs.AddDriveOpts ({readonly: true}); g.add_drive_opts (filename, optargs); This awkward style is required because GObject Introspection doesn't support optional arguments. Here is my program, which uses add_drive_opts and performs inspection, similar to the 'inspect_vm' example: https://rwmj.wordpress.com/2012/01/25/calling-libguestfs-from-javascript/ [...]
g.add_drive (file.get_path (), Guestfs.AddDriveOpts.OPTS_FORMAT, "raw");
I think Javascript is simply ignoring the second and third arguments. I'm a bit surprised there is no error ... Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://et.redhat.com/~rjones/virt-df/
* Richard W.M. Jones:
var optargs = new Guestfs.AddDriveOpts ({readonly: true}); g.add_drive_opts (filename, optargs);
This awkward style is required because GObject Introspection doesn't support optional arguments.
This is really, really ugly. Couldn't one use some sort of list instead? Something that could be translated to Javascript as g.add_drive_opts (filename, [ Guestfs.AddDriveOpts.FORMAT, "raw" ] ) or even g.add_drive_opts (filename, Guestfs.AddDriveOpts.FORMAT, "raw" ) would be much nicer.
g.add_drive (file.get_path (), Guestfs.AddDriveOpts.OPTS_FORMAT, "raw"); I think Javascript is simply ignoring the second and third arguments. I'm a bit surprised there is no error ...
Oh, thanks for noticing. -Hilko
On 01/26/2012 11:54 AM, Hilko Bengen wrote:
* Richard W.M. Jones:
var optargs = new Guestfs.AddDriveOpts ({readonly: true}); g.add_drive_opts (filename, optargs);
This awkward style is required because GObject Introspection doesn't support optional arguments.
This is really, really ugly. Couldn't one use some sort of list instead?
Something that could be translated to Javascript as
g.add_drive_opts (filename, [ Guestfs.AddDriveOpts.FORMAT, "raw" ] )
or even
g.add_drive_opts (filename, Guestfs.AddDriveOpts.FORMAT, "raw" )
would be much nicer.
We went round the houses on this one. The main advantage of the optargs object is that it's typesafe. It's also quite a lot more concise than the [ GUESTFS_VERYVERYVERYLONG_OPTIONNAME, "value" ] format as soon as you've got 2 optional arguments. Note that you can shorten the syntax further by declaring the optargs object inline. Compare: g.add_drive_opts (filename, [Guestfs.AddDriveOpts.FORMAT, "raw"]) with: g.add_drive_opts (filename, new Guestfs.AddDriveOpts({format: "raw})) There's barely any difference, and for 2 options the latter would be much shorter. Yes, it would be nicer to have proper optional arguments, but GObject Introspection is constrained somewhat by being a meta-binding. Matt -- Matthew Booth, RHCA, RHCSS Red Hat Engineering, Virtualisation Team GPG ID: D33C3490 GPG FPR: 3733 612D 2D05 5458 8A8A 1600 3441 EA19 D33C 3490
On Thu, Jan 26, 2012 at 12:54:55PM +0100, Hilko Bengen wrote:
* Richard W.M. Jones:
var optargs = new Guestfs.AddDriveOpts ({readonly: true}); g.add_drive_opts (filename, optargs);
This awkward style is required because GObject Introspection doesn't support optional arguments.
This is really, really ugly. Couldn't one use some sort of list instead?
That's somewhat of an overly dramatic statement really. g.add_drive_opts (filename, Guestfs.AddDriveOpts({format: "raw"})) is really not all that different & more complex from what you show below
Something that could be translated to Javascript as
g.add_drive_opts (filename, [ Guestfs.AddDriveOpts.FORMAT, "raw" ] )
In GObject list elements cannot be of variant types as you would require with this example, so this would only work if every single variadic parameter to the function was of the same data type, ie all strings, or all ints, etc.
or even
g.add_drive_opts (filename, Guestfs.AddDriveOpts.FORMAT, "raw" )
And doing variadic functions is even less possible, hence why Matt did things the way they are now. Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
participants (4)
-
Daniel P. Berrange -
Hilko Bengen -
Matthew Booth -
Richard W.M. Jones