2

Array in dbus/gsettings has a format [element1, element2, element3, ...].

Gsettings' interface allows only replacing existing array with another, so for instance, adding another item element can be achieved as follows:

#!/bin/bash
gsettings get ${schema} ${key} | grep -q \"${value}\" >/dev/null
if [ $? -ne 0 ]; then
   gsettings set ${schema} ${key} \"`gsettings get ${schema} ${key} | sed s/.$//`, ${value}]\"
fi

Can anyone help me with a script, that checks whether at least one element exists that matches the regex, and if it does, it parses the gsettingsarray string, and removes all matching elements (and puts it back withgsettings set`, but that is trivial).

I tried to find such tool, but I failed to find one. I cannot believe, that nobody yet had a need for such tool...


The question complements How to add element to the gsettings array at the specific location (given by index)?

3

Using bash for this task should be possible but I prefer doing complex array manipulations with a Python3 script:

#!/usr/bin/env python3

import argparse
import subprocess
import sys

parser = argparse.ArgumentParser()
parser.add_argument("schema", help="gsettings shema", metavar="SCHEMA")
parser.add_argument("key", help="gsettings key", metavar="KEY")
parser.add_argument("values",
                    help="gsettings value(s) to remove from the KEY array",
                    metavar="VALUE", nargs='*')
args = parser.parse_args()

array = eval(subprocess.check_output(["gsettings", "get", args.schema, args.key]))
for v in args.values:
    try:
        value = eval(v)
    except:
        value = v
    try:
        array.remove(value)
    except ValueError:
        print("Warning: {} not in {} {}".format(value, args.schema, args.key), file=sys.stderr)
subprocess.call(["gsettings", "set", args.schema, args.key, str(array)])

This script allow multiple values to be removed at the same time from the schema/key passed to the script.

6
  • Works flawlessly if the element is actually present in the list (easy to check with gsettings get ${schema} ${key} | grep -q "${value}" ) – Adam Ryczkowski May 1 '14 at 9:08
  • You're right, I didn't check if targeted values were actually present in the array. I'll update my answer – Sylvain Pineau May 1 '14 at 9:10
  • Done, I now print a warning if one of the values is not in the array – Sylvain Pineau May 1 '14 at 9:16
  • Another problem, this one I cannot fix: ./remove.py org.gnome.desktop.input-sources sources "('xkb', 'us')" doesn't finds the entry. I guess it has something to do with ' quotes or spaces in the argument. – Adam Ryczkowski May 3 '14 at 8:17
  • I've updated my answer to handle this case where basically I first need to "eval" such patterns to find a match in the gsettings array – Sylvain Pineau May 5 '14 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.