Debian Codegraph @

pyqi/commands/make_bash_completion.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 #!/usr/bin/python3
2
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2013, The BiPy Development Team.
5 #
6 # Distributed under the terms of the Modified BSD License.
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
10
•11
12
13 __credits__ = ["Daniel McDonald", "Jai Ram Rideout", "Doug Wendel",
14 "Greg Caporaso"]
15
16 import importlib
17 from pyqi.core.command import (Command, CommandIn, CommandOut,
18 ParameterCollection)
19 from pyqi.core.interface import get_command_names, get_command_config
20
21 def _get_cfg_module(desc):
22 """Load a module"""
23 mod = importlib.import_module(desc)
24 return mod
25
26 # Based on http://stackoverflow.com/questions/5302650/multi-level-bash-completion
27 script_fmt = """_%(driver)s_complete()
28 {
29 local cur prev
30
31 COMPREPLY=()
32 cur=${COMP_WORDS[COMP_CWORD]}
33 prev=${COMP_WORDS[COMP_CWORD-1]}
34
35 if [ $COMP_CWORD -gt 1 ]; then
36 prev=${COMP_WORDS[1]}
37 fi
38
39 if [ $COMP_CWORD -eq 1 ]; then
40 COMPREPLY=( $(compgen -W "%(command_list)s" -- $cur) )
41 elif [ $COMP_CWORD -gt 1 ]; then
42 case "$prev" in
43 %(commands)s
44 *)
45 ;;
46 esac
47 fi
48
49 return 0
50 } &&
51 complete -F _%(driver)s_complete -f %(driver)s
52 """
53
54 command_fmt = """ "%(command)s")
55 COMPREPLY=( $(compgen -W "%(options)s" -- $cur) )
56 ;;
57 """
58
59 class BashCompletion(Command):
60 BriefDescription = "Construct a bash completion script"
61 LongDescription = ("Construct a bash tab completion script that will search"
62 " through available commands and options")
63
64 CommandIns = ParameterCollection([
65 CommandIn(Name='command_config_module', DataType=str,
66 Description="CLI command configuration module",
67 Required=True),
68 CommandIn(Name='driver_name', DataType=str,
69 Description="name of the driver script", Required=True)
70 ])
71
72 CommandOuts = ParameterCollection([
73 CommandOut(Name='result', DataType=str,
74 Description="result bash completion script")
75 ])
76
77 def run(self, **kwargs):
78 driver = kwargs['driver_name']
79 cfg_mod_path = kwargs['command_config_module']
80 cfg_mod = _get_cfg_module(cfg_mod_path)
81 command_names = get_command_names(cfg_mod_path)
82 command_list = ' '.join(command_names)
83
84 commands = []
85 for cmd in command_names:
86 cmd_cfg, _ = get_command_config(cfg_mod_path, cmd,
87 exit_on_failure=False)
88
89 if cmd_cfg is not None:
90 command_options = []
91 command_options.extend(
92 sorted(['--%s' % p.Name for p in cmd_cfg.inputs]))
93 opts = ' '.join(command_options)
94
95 commands.append(command_fmt % {'command':cmd, 'options':opts})
96
97 all_commands = ''.join(commands)
98 return {'result':script_fmt % {'driver':driver,
99 'commands':all_commands,
100 'command_list':command_list}}
101
102 CommandConstructor = BashCompletion