For Python, use Gio
module. Specifically, here's an example of two functions I use in my own code (feel free to see how they're used within my Launcher List Indicator):
from gi.repository import Gio
# Define your own class here
# Note that these use `self` for use with classes
# this could be removed otherwise
def gsettings_get(self, schema, path, key):
"""Get value of gsettings schema"""
if path is None:
gsettings = Gio.Settings.new(schema)
else:
gsettings = Gio.Settings.new_with_path(schema, path)
return gsettings.get_value(key)
def gsettings_set(self, schema, path, key, value):
"""Set value of gsettings schema"""
if path is None:
gsettings = Gio.Settings.new(schema)
else:
gsettings = Gio.Settings.new_with_path(schema, path)
if isinstance(value, list):
return gsettings.set_strv(key, value)
if isinstance(value, int):
return gsettings.set_int(key, value)
These functions make getting and setting values easy and similar to the command-line utilities; schema
, path
, and key
values are all strings. For instance, to set Unity launcher position to "Bottom" you would do:
self.gsettings_set( 'com.canonical.Unity.Launcher', 'launcher-position', 'Bottom')
For shell scripts, there are dconf
and gsettings
command-line tools, the later being a front-end to dconf
. The gsettings
command is preferred because it performs safety checks on the input values. Example of usage:
gsettings set com.canonical.Unity.Launcher launcher-position 'Bottom'
You can call these two from python using subprocess.call()
or subprocess.check_output()
, but this has the overhead of spawning extra process, which is unnecessary (And if you're going to do it Pythonic way, do it right via the API).
See also
from gi.repository import Gio
:) – Rinzwind Aug 04 '18 at 06:42gsettings
, there is adconf
command-line tool that you can use directly from the shell - see What is dconf, what is its function, and how do I use it? – steeldriver Aug 04 '18 at 08:22