Define a GLib.Variant in Python

2.5k views Asked by At

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}?

2

There are 2 answers

0
Philip Withnall On BEST ANSWER

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)
})
7
ptomato On

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),
})