pixelb / crudini

A utility for manipulating ini files

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

edit multiple values at once and file locking

fekir opened this issue · comments

While checking the root cause for #85, I realized that crudini tries to lock a file before editing it.

I presume the reason is to avoid getting an inconsistent file (in case someone else edits it in the meantime too), but I believe that with the current user API, the locking mechanism has little benefit.

In particular, most of the time I want to set multiple parameters.

AFAIK it can only be done by invoking crudini multiple times:

python crudini --set file.ini section parameter1 value1
python crudini --set file.ini section parameter2 value2
python crudini --set file.ini section parameter3 value3

I would love to be able to set/delete multiple parameters at one, something like

python crudini file.ini --set section parameter1 value1 --set section parameter2 value2 --set section parameter3 value3 --del section2 parameter4 ...

For reference, consider sed and grep.
Thanks to -e it is possible with both to avoid creating multiple processes, and yet use a simple syntax:

echo 'ae' | sed -e 's/e/f/g' -e 's/a/b/g'

vs

echo 'ae' | sed 's/e/f/g' | sed 's/a/b/g'

or grep:

echo 'ae' | grep -e a -e e

vs

echo 'ae' | grep -e a | grep -e e

In the case of crudini,

  • the overhead of a new process is generally bigger
  • locking a file also has a non-trivial overhead
  • needs to parse the same file multiple times

But the price of the first both actions could be payed only once, instead of once per every edit.

If crudini would be able to edit multiple parameters at once, file locking would also generally be more useful, as one can avoid intermediary and possibly invalid states (a configuration might make sense only if a set of values is set, and invalid if only a subset is present).

If the solution is to temporarily create a new file, then file locking could be avoided altogether (and would immediately solve some cross-platform issues like #85).

NOTE: Even if the lock mechanism gets removed (I see little value in it, but happy to be proved wrong) I would still claim it would be much more user-friendly to be able to set/remove multiple parameters at once.

The only drawback is that I see no way to expand the current CLI for editing multiple values at once.

The --merge functionality may be used to update multiple items at once.
That doesn't cater for delete though

Mhm, I thought --merge needed a second file, but I see it's not the case, but

  • as you wrote, it is not possible to remove values
  • it also seems that it is possible to add values only under a given section (granted, in the example I gave I also added values only under a given section)

a workaround 🤡

#bash4
declare -A General=([Experimental]=true [KernelExperimental]=true [ControllerMode]=bredr)

for c in "${!General[@]}"; do
    printf  -- "--set  /etc/bluetooth/main.conf  General %s  %s\n" "$c" "${General[$c]}" |xargs -t crudini
done
  # crudini --set /etc/bluetooth/main.conf General KernelExperimental true
  # crudini --set /etc/bluetooth/main.conf General Experimental true
  # crudini --set /etc/bluetooth/main.conf General ControllerMode bredr

(taken from my memory aid ;)

workaround is concise, but still inefficient, non atomic.
Still intend to implement support for this

@fekir one can now do:

crudini --set config_file section parameter1 value \
        --set config_file section parameter2 value \
        --del config_file section parameter3