Skip to content

Commit

Permalink
Refactor: Implement bash completion for stbt config in Python
Browse files Browse the repository at this point in the history
The current bash implementation calls `stbt config` so we may as well do
all the work there.  This removes the need for `__system_config` to be in
`stbt.conf`.
  • Loading branch information
wmanley authored and drothlis committed Apr 25, 2018
1 parent c057a97 commit 1be8c07
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 36 deletions.
20 changes: 1 addition & 19 deletions stbt-completion
Expand Up @@ -279,25 +279,7 @@ _stbt_control_recorder_file() {
}

_stbt_config_keys() {
_stbt_config_file_contents |
grep -v ^__system_config |
perl -lne '
$section = $1 if /^\s*\[([a-zA-Z0-9_]+)\]\s*$/;
print "$section.$1" if /^\s*([a-zA-Z0-9_]+)\s*=/;'
}

_stbt_config_file_contents() {
[ -n "$in_unit_test" ] && { cat; return; }

local defaults=$(
awk '/stbt-"\$@"/ { print $NF }' "$(which stbt)" |
xargs dirname
)/stbt.conf
local system_config=$(stbt config global.__system_config)
local user_config="${XDG_CONFIG_HOME:-$HOME/.config}/stbt/stbt.conf"
for f in "$defaults" "$system_config" "$user_config" "$STBT_CONFIG_FILE"; do
[ -r "$f" ] && cat "$f"
done
stbt config --bash-completion ""
}

_stbt_power_outlet_uri() {
Expand Down
11 changes: 10 additions & 1 deletion stbt-config
Expand Up @@ -9,7 +9,7 @@ https://github.com/stb-tester/stb-tester/blob/master/LICENSE for details).
import argparse
import sys

from _stbt.config import ConfigurationError, get_config
from _stbt.config import _config_init, ConfigurationError, get_config


def error(s):
Expand All @@ -23,11 +23,20 @@ parser.description = """Prints the value of the specified key from the stbt
configuration file. See 'configuration' in the stbt(1) man page."""
parser.epilog = \
"Returns non-zero exit status if the specified key or section isn't found."
parser.add_argument(
"--bash-completion", action="store_true", help=argparse.SUPPRESS)
parser.add_argument(
"name", metavar="section.key",
help="e.g. 'global.source_pipeline' or 'record.control_recorder'")
args = parser.parse_args(sys.argv[1:])

if args.bash_completion:
cfg = _config_init()
for section in cfg.sections():
for option in cfg.options(section):
print "%s.%s" % (section, option)
sys.exit(0)

if args.name.rfind(".") == -1:
error(
"'name' parameter must contain the section and key separated by a dot")
Expand Down
8 changes: 8 additions & 0 deletions tests/completion-test.conf
@@ -0,0 +1,8 @@
[global]
key1 = hi there
key2=hi there
key3 = hi there
key4 = hi there

[run]
key5 = hi there
20 changes: 4 additions & 16 deletions tests/test-stbt-completion.sh
Expand Up @@ -128,23 +128,11 @@ test_completion_filenames() {
fail "unexpected completions for files + directories"
}

completion_config_for_tests() {
cat <<-EOF
[global]
key1 = hi there
key2=hi there
key3 = hi there
key4 = hi there
[run]
key5 = hi there
EOF
}

test_completion_config_keys() {
cd "$srcdir" && . stbt-completion
diff \
<(completion_config_for_tests | in_unit_test=yes _stbt_config_keys) \
<(printf '%s\n' \
global.key1 global.key2 global.key3 global.key4 run.key5) ||
diff -u \
<(printf '%s\n' global.key1 global.key2 global.key3 global.key4 run.key5) \
<(STBT_CONFIG_FILE="tests/completion-test.conf" _stbt_config_keys | grep 'key[0-9]') \
||
fail "_stbt_config_keys unexpected output"
}

0 comments on commit 1be8c07

Please sign in to comment.