Question

My desktop is GNOME and I am programmatically changing its settings via Python.

The database has simple value types, e.g. strings, ints, lists of strings, list of ints, ...

A simple CLI tool to manipulate the data is gconftool-2, which returns values for keys using the --get option.

I don't know to to infer the type from these values considering that I need to know the value when setting it back to something. Notice, in my schema, "8" is a string and 8 is an int, but they are both output as just 8 by gconftool-2.

How would you go about doing this?

Was it helpful?

Solution

Rather than invoking the command line tool, try using the gconf module included in the GNOME Python bindings:

>>> import gconf
>>> client = gconf.Client()
>>> # Get a value and introspect its type:
>>> value = client.get('/apps/gnome-terminal/profiles/Default/background_color')
>>> value.type
<enum GCONF_VALUE_STRING of type GConfValueType>
>>> value.get_string()
'#FFFFFFFFDDDD'

For lists, you can introspect the list value type:

>>> value = client.get('/apps/compiz-1/general/screen0/options/active_plugins')
>>> value.type
<enum GCONF_VALUE_LIST of type GConfValueType>
>>> value.get_list_type()
<enum GCONF_VALUE_STRING of type GConfValueType>
>>> value.get_list()
(<GConfValue at 0x159aa80>, <GConfValue at 0x159aaa0>, ...)

In general though, you should know the types of the keys you're manipulating and use the appropriate type specific access methods directly (e.g. Client.get_string and Client.set_string).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top