2

I am trying to define a GLib.Variant data type in Python to use it with the pydbus library. This is my attempt to do so:

#!/usr/bin/python

from gi.repository import GLib
from pydbus import SessionBus

var1 = GLib.Variant.new_variant('draw-cursor', False)
var2 = GLib.Variant.new_variant('framerate', 30)

bus = SessionBus()
calling = bus.get('org.gnome.Shell.Screencast', '/org/gnome/Shell/Screencast')

calling.Screencast('out.webm', {var1, var2})

However it says TypeError: GLib.Variant.new_variant() takes exactly 1 argument (2 given). And I can see that clear. But then how can I assign the values for what I will define? Shouldn't it be a dictionary like {'framerate': 30}?

Philip Withnall
  • 5,293
  • 14
  • 28
Madno
  • 910
  • 2
  • 12
  • 27

2 Answers2

2

The options argument has type a{sv}, so you probably need to provide the types explicitly:

options = GLib.Variant('a{sv}', {
    'draw-cursor': GLib.Variant('b', False),
    'framerate': GLib.Variant('i', 30),
})
ptomato
  • 56,175
  • 13
  • 112
  • 165
  • Now it says: `AttributeError: 'Variant' object has no attribute 'items'`. I don't know if this was a bug in GLib itself. – Madno Feb 06 '17 at 07:25
  • It works for me; this is the latest GLib and Python 3 by the way. – ptomato Feb 07 '17 at 04:30
  • Well I have the same too. Yet it yields the error. The complete code is: https://pastebin.mozilla.org/8976332 Right? – Madno Feb 07 '17 at 12:39
  • You didn't say the error was from the `Screencast` call now. Are you sure you need to pass a variant there in the first place? – ptomato Feb 08 '17 at 05:57
  • Yes definitely. It was mentioned in some documentation beside a lot of other places. Also this is an example: http://askubuntu.com/questions/359587/how-to-pass-asv-arguments-to-gdbus – Madno Feb 08 '17 at 06:31
  • That example is for gdbus command line, not Python. Are you really sure you need to pass a variant object to that method *in Python*? The error you quoted seems to indicate it's looking for a dictionary. – ptomato Feb 08 '17 at 16:37
  • Actually you seem to be right. I check the gobject properties and found this: http://i.imgur.com/9Dk6qyJ.png But if it was a dictionary. How can I pass it like this type then? – Madno Feb 09 '17 at 06:51
2

The second failure (AttributeError: 'Variant' object has no attribute 'items') seems to be because pydbus expects you to pass in a dict, rather than a GLib.Variant, and it unconditionally wraps whatever you pass it in a GLib.Variant. This means it tries to get the items from the options variant, which fails because GLib.Variant doesn’t support that.

This code works with pydbus:

calling.Screencast('out.webm', {
    'draw-cursor': GLib.Variant('b', False),
    'framerate': GLib.Variant('i', 30)
})
Philip Withnall
  • 5,293
  • 14
  • 28