Fix iniset and his friends

* In python the white spaces are part of the section name
* Handle options with empty value
* Support paths with white spaces

Change-Id: I69a584608853cfdb8b7dce1e24d929216ef2fc41
This commit is contained in:
Attila Fazekas 2012-12-20 10:57:16 +01:00
parent 701718a844
commit 588eb4129d
2 changed files with 61 additions and 10 deletions

View File

@ -460,7 +460,7 @@ function inicomment() {
local file=$1
local section=$2
local option=$3
sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
}
# Uncomment an option in an INI file
@ -469,7 +469,7 @@ function iniuncomment() {
local file=$1
local section=$2
local option=$3
sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
}
@ -480,10 +480,20 @@ function iniget() {
local section=$2
local option=$3
local line
line=$(sed -ne "/^\[ *$section *\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
echo ${line#*=}
}
# Determinate is the given option present in the INI file
# ini_has_option config-file section option
function ini_has_option() {
local file=$1
local section=$2
local option=$3
local line
line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
[ -n "$line" ]
}
# Set an option in an INI file
# iniset config-file section option value
@ -492,18 +502,18 @@ function iniset() {
local section=$2
local option=$3
local value=$4
if ! grep -q "^\[ *$section *\]" $file; then
if ! grep -q "^\[$section\]" "$file"; then
# Add section at the end
echo -e "\n[$section]" >>$file
echo -e "\n[$section]" >>"$file"
fi
if [[ -z "$(iniget $file $section $option)" ]]; then
if ! ini_has_option "$file" "$section" "$option"; then
# Add it
sed -i -e "/^\[ *$section *\]/ a\\
sed -i -e "/^\[$section\]/ a\\
$option = $value
" $file
" "$file"
else
# Replace it
sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" "$file"
fi
}

View File

@ -57,6 +57,9 @@ handlers=ee,ff
[ ccc ]
spaces = yes
[ddd]
empty =
EOF
# Test with spaces
@ -79,13 +82,22 @@ fi
# Test with spaces in section header
VAL=$(iniget test.ini ccc spaces)
VAL=$(iniget test.ini " ccc " spaces)
if [[ "$VAL" == "yes" ]]; then
echo "OK: $VAL"
else
echo "iniget failed: $VAL"
fi
iniset test.ini "b b" opt_ion 42
VAL=$(iniget test.ini "b b" opt_ion)
if [[ "$VAL" == "42" ]]; then
echo "OK: $VAL"
else
echo "iniget failed: $VAL"
fi
# Test without spaces, end of file
VAL=$(iniget test.ini bbb handlers)
@ -104,6 +116,29 @@ else
echo "iniget failed: $VAL"
fi
# test empty option
if ini_has_option test.ini ddd empty; then
echo "OK: ddd.empty present"
else
echo "ini_has_option failed: ddd.empty not found"
fi
# test non-empty option
if ini_has_option test.ini bbb handlers; then
echo "OK: bbb.handlers present"
else
echo "ini_has_option failed: bbb.handlers not found"
fi
# test changing empty option
iniset test.ini ddd empty "42"
VAL=$(iniget test.ini ddd empty)
if [[ "$VAL" == "42" ]]; then
echo "OK: $VAL"
else
echo "iniget failed: $VAL"
fi
# Test section not exist
@ -132,6 +167,12 @@ else
echo "iniget failed: $VAL"
fi
if ! ini_has_option test.ini aaa debug; then
echo "OK aaa.debug not present"
else
echo "ini_has_option failed: aaa.debug"
fi
iniset test.ini aaa debug "999"
VAL=$(iniget test.ini aaa debug)