0

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?

Robottinosino
  • 10,384
  • 17
  • 59
  • 97

1 Answers1

2

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).

James Henstridge
  • 42,244
  • 6
  • 132
  • 114
  • This sounds like a better, saner, more robust/sensible/clean approach for my Python scripting of gconf.. I'll report back if it fixes my issue with lists of ints, lists of strings.. – Robottinosino Apr 23 '13 at 05:25
  • Lists of strings give me this: and I am not sure how to discern list of ints, etc. Example: (as you use gnome-terminal..) global/active_encodings – Robottinosino Apr 23 '13 at 05:30
  • the enumeration values are provided as globals in the `gconf` module. So you could do `if value.type == gconf.VALUE_STRING`: ...` – James Henstridge Apr 23 '13 at 07:28
  • Oops. I realised you were asking about lists in particular. `value.get_list_type()` should tell you the type of the list values. The reference documentation (C based) may help too: https://developer.gnome.org/gconf/stable/ – James Henstridge Apr 23 '13 at 07:31