forked from stb-tester/stb-tester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstbt-completion
436 lines (391 loc) · 13.4 KB
/
stbt-completion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# Bash tab-completion for stb-tester. -*- shell-script -*-
# Copyright 2012 David Rothlisberger <[email protected]>
# Copyright 2012-2013 YouView TV Ltd.
# License: LGPL v2.1 or (at your option) any later version (see
# https://github.com/stb-tester/stb-tester/blob/master/LICENSE for details).
complete -o nospace -F _stbt stbt
complete -o nospace -F _stbt_run stbt-run
complete -o nospace -F _stbt_record stbt-record
_stbt() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local IFS=$'\n'
if [ $COMP_CWORD = 1 ]; then
COMPREPLY=($(compgen \
-W "$(_stbt_trailing_space --help --version \
auto-selftest \
batch \
config \
control \
lint \
power \
record \
run \
screenshot \
match \
tv)" \
-- "$cur"))
else
case "${COMP_WORDS[1]}" in
auto-selftest) _stbt_auto_selftest;;
batch) _stbt_batch;;
config) _stbt_config;;
control) _stbt_control;;
lint) _stbt_lint;;
power) _stbt_power;;
record) _stbt_record;;
run) _stbt_run;;
match) _stbt_match;;
*) COMPREPLY=();;
esac
fi
}
_stbt_run() {
_stbt_get_prev
local cur="$_stbt_cur"
local prev="$_stbt_prev"
case "$prev" in
--control=*) COMPREPLY=($(_stbt_control_uri));;
--source-pipeline=*) COMPREPLY=();;
--sink-pipeline=*) COMPREPLY=();;
--save-video=*) COMPREPLY=($(_stbt_filenames "$cur"));;
*) COMPREPLY=(
$(compgen -W "$(_stbt_trailing_space \
--help --verbose --restart-source --save-video \
--control --source-pipeline --sink-pipeline)" \
-- "$cur")
$(_stbt_filename_possibly_with_test_functions));;
esac
}
_stbt_record() {
_stbt_get_prev
local cur="$_stbt_cur"
local prev="$_stbt_prev"
case "$prev" in
--control=*) COMPREPLY=($(_stbt_control_uri));;
--source-pipeline=*) COMPREPLY=();;
--sink-pipeline=*) COMPREPLY=();;
--control-recorder=*) COMPREPLY=($(_stbt_control_recorder_uri));;
-o=*|--output-file=*) COMPREPLY=($(_stbt_filenames "$cur"));;
*) COMPREPLY=($(compgen \
-W "$(_stbt_trailing_space \
--help --verbose --restart-source \
--control --source-pipeline --sink-pipeline \
--control-recorder --output-file)" \
-- "$cur"));;
esac
}
_stbt_auto_selftest() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local subcommand="${COMP_WORDS[2]}"
case "$prev" in
auto-selftest)
COMPREPLY=($(compgen \
-W "$(_stbt_trailing_space --help generate validate)" \
-- "$cur"));
return;;
esac
case "$subcommand" in
--help|validate) COMPREPLY=();;
generate) COMPREPLY=($(_stbt_filenames "$cur"));;
esac
}
_stbt_batch() {
_stbt_get_prev
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local subcommand="${COMP_WORDS[2]}"
case "$COMP_CWORD,$subcommand" in
2,*) COMPREPLY=($(compgen \
-W "$(_stbt_trailing_space --help run report instaweb)" \
-- "$cur"));;
3,run) COMPREPLY=(
$(compgen -W "-h" -- "$cur")
$(_stbt_filename_possibly_with_test_functions));;
*,run) COMPREPLY=($(_stbt_filename_possibly_with_test_functions));;
*,report) COMPREPLY=($(compgen -d -S "/" -- "$cur"));;
3,instaweb) COMPREPLY=($(compgen \
-W "$(_stbt_trailing_space \
--help localhost:5000 0.0.0.0:5000)" \
-- "$cur"));;
esac
}
_stbt_config() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
if [ $COMP_CWORD -eq 2 ]; then
COMPREPLY=($(compgen \
-W "$(_stbt_trailing_space --help $(_stbt_config_keys))" \
-- "$cur"))
fi
}
_stbt_control() {
_stbt_get_prev
local cur="$_stbt_cur"
local prev="$_stbt_prev"
case "$prev" in
--keymap=*) COMPREPLY=($(_stbt_filenames "$cur"));;
--control=*) COMPREPLY=($(_stbt_control_uri));;
*) COMPREPLY=($(compgen -W "$(_stbt_trailing_space \
--help --help-keymap --keymap --control)" \
-- "$cur"));;
esac
}
_stbt_lint() {
declare -F _pylint &>/dev/null || return # Provided by bash-completion
_pylint pylint
}
_stbt_power() {
_stbt_get_prev
local cur="$_stbt_cur"
local prev="$_stbt_prev"
case "$prev" in
--power-outlet=*) COMPREPLY=($(_stbt_power_outlet_uri));;
*) COMPREPLY=($(compgen -W "$(_stbt_trailing_space \
--help --power-outlet on off status)" \
-- "$cur"));;
esac
}
_stbt_match() {
_stbt_get_prev
local cur="$_stbt_cur"
local prev="$_stbt_prev"
local v
[[ $COMP_CWORD -gt 2 && ${COMP_WORDS[2]} == "-v" ]] && v="-v" || v=
[[ ${COMP_WORDS[2]} == "--help" ]] && return
case "$v,$COMP_CWORD" in
,2) COMPREPLY=($(compgen \
-f -W "$(_stbt_trailing_space --help -v)" \
-- "$cur"));;
*,3|-v,4) COMPREPLY=($(_stbt_filenames "$cur"));;
,4|*,*)
case "$prev" in
match_method=) COMPREPLY=($(compgen \
-W "$(_stbt_trailing_space \
sqdiff-normed ccorr-normed ccoeff-normed)" \
-- "$cur"));;
confirm_method=) COMPREPLY=($(compgen \
-W "$(_stbt_trailing_space none absdiff normed-absdiff)" \
-- "$cur"));;
match_threshold=|erode_passes=|confirm_threshold=)
COMPREPLY=();;
*) COMPREPLY=($(compgen \
-W "$(_stbt_no_space \
match_method= match_threshold= \
confirm_method= erode_passes= confirm_threshold=)" \
-- "$cur"));;
esac
esac
}
## arguments to command-line options
############################################################################
_stbt_control_uri() {
local cur="$_stbt_cur"
local prev="$_stbt_prev"
case "$prev" in
--control=irnetbox:*:*:) _stbt_irnetbox_config;;
--control=irnetbox:*:) _stbt_irnetbox_output;;
--control=irnetbox:) _stbt_hostname;;
--control=lirc:*:*:) _stbt_lirc_name;;
--control=lirc:*:) _stbt_lirc_port_or_name;;
--control=lirc:) _stbt_lirc_socket_or_port_or_hostname;;
--control=rfb:*:) _stbt_rfb_port;;
--control=rfb:) _stbt_hostname;;
--control=samsung:*) _stbt_hostname;;
*)
compgen -W "$( \
_stbt_no_space irnetbox: lirc: rfb: samsung: x11:
_stbt_trailing_space none test)" \
-- "$cur";;
esac
}
_stbt_control_recorder_uri() {
local cur="$_stbt_cur"
local prev="$_stbt_prev"
case "$prev" in
--control-recorder=lirc:*:*:) _stbt_lirc_name;;
--control-recorder=lirc:*:) _stbt_lirc_port_or_name;;
--control-recorder=lirc:) _stbt_lirc_socket_or_port_or_hostname;;
--control-recorder=file:) _stbt_control_recorder_file;;
--control-recorder=stbt-control:) _stbt_filenames "$cur";;
*) compgen -W "$( \
_stbt_no_space lirc: file://
_stbt_trailing_space stbt-control)" \
-- "$cur";;
esac
}
_stbt_irnetbox_output() {
local cur="$_stbt_cur"
compgen -S ":" -W "$(
_stbt_no_space 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16)" \
-- "$cur"
}
_stbt_irnetbox_config() {
local cur="$_stbt_cur"
_stbt_filenames "$cur"
}
_stbt_lirc_socket_or_port_or_hostname() {
local cur="$_stbt_cur"
compgen -W "$(_stbt_no_space $(_lirc_tcp_ports))" -- "$cur"
compgen -f -S ":" -- "$cur"
compgen -A hostname -S ":" -- "$cur"
}
_stbt_lirc_port_or_name() {
local cur="$_stbt_cur"
compgen -W "$(_stbt_no_space $(_lirc_tcp_ports))" -- "$cur"
compgen -W "$(_stbt_trailing_space $(_lirc_remote_names))" -- "$cur"
}
_stbt_lirc_name() {
local cur="$_stbt_cur"
compgen -W "$(_stbt_trailing_space $(_lirc_remote_names))" -- "$cur"
}
_stbt_hostname() {
local cur="$_stbt_cur"
compgen -A hostname -S ":" -- "$cur"
}
_stbt_rfb_port() {
local cur="$_stbt_cur"
compgen -W "$(_stbt_trailing_space 5900)" -- "$cur"
}
_stbt_control_recorder_file() {
local cur="$_stbt_cur"
compgen -f -P "//" -- "${cur#//}"
}
_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_power_outlet_uri() {
local cur="$_stbt_cur"
local prev="$_stbt_prev"
case "$prev" in
--power-outlet=*:*:) _stbt_power_outlet_outlet;;
--power-outlet=*:) _stbt_hostname;;
*) compgen -W "$(_stbt_no_space ipp: pdu:)" -- "$cur";;
esac
}
_stbt_power_outlet_outlet() {
local prev="$_stbt_prev"
case "$prev" in
--power-outlet=pdu:*)
compgen -W "$(_stbt_trailing_space \
1-A1 1-A2 1-A3 1-A4 1-A5 1-A6 1-A7 1-A8)" \
-- "$cur";;
esac
}
## Helper functions
############################################################################
# When you type a python filename that doesn't have any functions named "test_",
# completes the filename followed by a space;
# otherwise completes the filename followed by "::" followed by the function
# name followed by a space.
#
# Before you've typed ":":
# * cur == the filename you've typed so far (e.g. test_something.p)
#
# After you've typed ":" or "::":
# * prev = filename name + ":" or "::" (e.g. test_something.py::)
# * cur = the function name you've typed so far
#
# See test cases in _stbt_test_filename_possibly_with_test_functions, below.
# Note that COMP_WORDBREAKS by default includes "=" and ":".
_stbt_filename_possibly_with_test_functions() {
local cur="$_stbt_cur"
local prev="$_stbt_prev"
local test_re='^def test_.*( *) *:'
local filename
[[ "$prev" =~ \.py::?$ ]] && filename="$prev" || filename="$cur"
filename=${filename%:}; filename=${filename%:} # abc.py:: => abc.py
{
# Files with test functions:
_stbt_trailing_space $(
compgen -f -X '!*.py' -- "$filename" |
xargs --no-run-if-empty grep -H "$test_re" |
sed -e 's/:def */::/' -e 's/ *( *) *:.*//')
# Files without test functions:
_stbt_trailing_space $(
compgen -f -X '!*.py' -- "$filename" |
xargs --no-run-if-empty grep -L "$test_re")
# Directories:
compgen -d -S/ -- "$filename"
} |
if [[ "$prev" =~ \.py::?$ ]]; then
sed -n "s,^$prev$cur,$cur,p"
else
cat
fi
}
# Returns filenames and directories, appending a slash to directory names.
_stbt_filenames() {
# Files, excluding directories:
grep -v -F -f <(compgen -d -P ^ -S '$' -- "$1") \
<(compgen -f -P ^ -S '$' -- "$1") |
sed -e 's/^\^//' -e 's/\$$/ /'
# Directories:
compgen -d -S / -- "$1"
}
# Walks backward from the current word, collecting the entire preceding
# flag and its argument:
# "stbt run --control lirc::name..." => _stbt_prev="--control=lirc::"
# See test cases in _stbt_test_get_prev, below.
# Note that COMP_WORDBREAKS by default includes "=" and ":".
_stbt_get_prev() {
local c p pp i
c="${COMP_WORDS[COMP_CWORD]}"
p="$c"
i=$((COMP_CWORD-1))
while [ $i -gt 1 ]; do
pp="${COMP_WORDS[i]}" # the word previous to $p
case "$pp,$p" in
[=:],*|::,*|*,[=:]*) p="$pp$p";;
*) _stbt_takes_arg "$pp" || break; p="$pp=$p";;
esac
i=$((i-1))
done
[[ "$c" == ":" || "$c" == "::" || "$c" == "=" ]] && c=""
p="${p%$c}"
_stbt_cur="$c"
_stbt_prev="$p"
}
_stbt_takes_arg() {
case "$1" in
--control|--source-pipeline|--sink-pipeline) true;;
--save-video) true;;
--control-recorder|-o|--output-file) true;;
--keymap) true;;
--power-outlet) true;;
*) false;;
esac
}
_stbt_trailing_space() {
local c
for c in $*; do printf -- "$c \n"; done
}
_stbt_no_space() {
local c
for c in $*; do printf -- "$c\n"; done
}
_lirc_tcp_ports() {
ps ax | grep 'lircd' | \
perl -nle 'print $1 if (/--listen=(\d+)/ || /-l +(\d+)/)' | sort
}
_lirc_remote_names() {
cat /etc/lirc/lircd.conf 2>/dev/null |
awk '/^begin remote/ { inremote = 1 }
inremote && /^ *name / { print $2; inremote = 0 }'
}