0.27.16
by Martin von Gagern
Added distutils setup script and plugin meta data. |
1 |
#!/usr/bin/env python
|
0.27.2
by Martin von Gagern
First programmatic generation of completions. |
2 |
|
5147.5.4
by Martin von Gagern
Assign copyright to Canonical Ltd. |
3 |
# Copyright (C) 2009, 2010 Canonical Ltd
|
0.27.14
by Martin von Gagern
Added GPL 2 license document and copyright notice. |
4 |
#
|
5147.5.5
by Martin von Gagern
Adjust copyright notice comments for bash_completion. |
5 |
# This program is free software; you can redistribute it and/or modify
|
6 |
# it under the terms of the GNU General Public License as published by
|
|
7 |
# the Free Software Foundation; either version 2 of the License, or
|
|
8 |
# (at your option) any later version.
|
|
9 |
#
|
|
10 |
# This program is distributed in the hope that it will be useful,
|
|
11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
# GNU General Public License for more details.
|
|
0.27.14
by Martin von Gagern
Added GPL 2 license document and copyright notice. |
14 |
#
|
15 |
# You should have received a copy of the GNU General Public License
|
|
5147.5.5
by Martin von Gagern
Adjust copyright notice comments for bash_completion. |
16 |
# along with this program; if not, write to the Free Software
|
17 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
0.27.14
by Martin von Gagern
Added GPL 2 license document and copyright notice. |
18 |
|
6379.6.3
by Jelmer Vernooij
Use absolute_import. |
19 |
from __future__ import absolute_import |
20 |
||
0.28.2
by Martin von Gagern
merge from trunk |
21 |
from bzrlib import ( |
5245.1.1
by Andrew Bennetts
Fix deprecation warning in bash_completion plugin. |
22 |
cmdline, |
0.28.2
by Martin von Gagern
merge from trunk |
23 |
commands, |
24 |
config, |
|
25 |
help_topics, |
|
26 |
option, |
|
27 |
plugin, |
|
28 |
)
|
|
0.27.36
by Martin von Gagern
List versions of bzr and plugins. |
29 |
import bzrlib |
0.27.18
by Martin von Gagern
Add global options in all cases |
30 |
import re |
6386.1.2
by Jelmer Vernooij
Move sys import. |
31 |
import sys |
0.27.2
by Martin von Gagern
First programmatic generation of completions. |
32 |
|
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
33 |
|
34 |
class BashCodeGen(object): |
|
35 |
"""Generate a bash script for given completion data."""
|
|
36 |
||
37 |
def __init__(self, data, function_name='_bzr', debug=False): |
|
38 |
self.data = data |
|
39 |
self.function_name = function_name |
|
40 |
self.debug = debug |
|
41 |
||
42 |
def script(self): |
|
43 |
return ("""\ |
|
0.30.3
by Martin von Gagern
Lazy initialization |
44 |
# Programmable completion for the Bazaar-NG bzr command under bash.
|
45 |
# Known to work with bash 2.05a as well as bash 4.1.2, and probably
|
|
46 |
# all versions in between as well.
|
|
0.27.1
by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr |
47 |
|
48 |
# Based originally on the svn bash completition script.
|
|
49 |
# Customized by Sven Wilhelm/Icecrash.com
|
|
0.27.2
by Martin von Gagern
First programmatic generation of completions. |
50 |
# Adjusted for automatic generation by Martin von Gagern
|
0.27.1
by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr |
51 |
|
5147.5.12
by Martin von Gagern
Use version of bzrlib. Drop meta.py. |
52 |
# Generated using the bash_completion plugin.
|
0.27.17
by Martin von Gagern
Mention version and homepage in generated shell code comment. |
53 |
# See https://launchpad.net/bzr-bash-completion for details.
|
54 |
||
0.27.36
by Martin von Gagern
List versions of bzr and plugins. |
55 |
# Commands and options of bzr %(bzr_version)s |
56 |
||
0.27.20
by Martin von Gagern
More flexible handling of fixed word lists. |
57 |
shopt -s progcomp
|
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
58 |
%(function)s
|
59 |
complete -F %(function_name)s -o default bzr |
|
60 |
""" % { |
|
61 |
"function_name": self.function_name, |
|
62 |
"function": self.function(), |
|
63 |
"bzr_version": self.bzr_version(), |
|
64 |
})
|
|
65 |
||
66 |
def function(self): |
|
67 |
return ("""\ |
|
0.27.4
by Martin von Gagern
Introduce --function-name switch. (LP #439829) |
68 |
%(function_name)s () |
0.27.1
by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr |
69 |
{
|
0.27.20
by Martin von Gagern
More flexible handling of fixed word lists. |
70 |
local cur cmds cmdIdx cmd cmdOpts fixedWords i globalOpts
|
0.27.30
by Martin von Gagern
Complete values for enum_switch of RegistryOptions. |
71 |
local curOpt optEnums
|
0.32.16
by Martin von Gagern
Ensure proper quoting of spaces in tag names. |
72 |
local IFS=$' \\n' |
0.27.1
by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr |
73 |
|
74 |
COMPREPLY=()
|
|
75 |
cur=${COMP_WORDS[COMP_CWORD]} |
|
76 |
||
0.27.2
by Martin von Gagern
First programmatic generation of completions. |
77 |
cmds='%(cmds)s' |
0.32.15
by Martin von Gagern
Allow spaces in tag names. Use bash arrays more extensively. |
78 |
globalOpts=( %(global_options)s ) |
0.27.19
by Martin von Gagern
Take global options preceding the command into account. |
79 |
|
80 |
# do ordinary expansion if we are anywhere after a -- argument
|
|
81 |
for ((i = 1; i < COMP_CWORD; ++i)); do
|
|
82 |
[[ ${COMP_WORDS[i]} == "--" ]] && return 0 |
|
83 |
done
|
|
84 |
||
85 |
# find the command; it's the first word not starting in -
|
|
86 |
cmd=
|
|
87 |
for ((cmdIdx = 1; cmdIdx < ${#COMP_WORDS[@]}; ++cmdIdx)); do
|
|
88 |
if [[ ${COMP_WORDS[cmdIdx]} != -* ]]; then |
|
89 |
cmd=${COMP_WORDS[cmdIdx]} |
|
90 |
break
|
|
91 |
fi
|
|
92 |
done
|
|
93 |
||
94 |
# complete command name if we are not already past the command
|
|
0.27.30
by Martin von Gagern
Complete values for enum_switch of RegistryOptions. |
95 |
if [[ $COMP_CWORD -le cmdIdx ]]; then
|
0.32.15
by Martin von Gagern
Allow spaces in tag names. Use bash arrays more extensively. |
96 |
COMPREPLY=( $( compgen -W "$cmds ${globalOpts[*]}" -- $cur ) ) |
0.27.1
by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr |
97 |
return 0
|
98 |
fi
|
|
99 |
||
0.27.30
by Martin von Gagern
Complete values for enum_switch of RegistryOptions. |
100 |
# find the option for which we want to complete a value
|
101 |
curOpt=
|
|
102 |
if [[ $cur != -* ]] && [[ $COMP_CWORD -gt 1 ]]; then
|
|
103 |
curOpt=${COMP_WORDS[COMP_CWORD - 1]} |
|
104 |
if [[ $curOpt == = ]]; then
|
|
105 |
curOpt=${COMP_WORDS[COMP_CWORD - 2]} |
|
0.32.1
by Martin von Gagern
Complete tag names. |
106 |
elif [[ $cur == : ]]; then
|
107 |
cur=
|
|
108 |
curOpt="$curOpt:"
|
|
109 |
elif [[ $curOpt == : ]]; then
|
|
110 |
curOpt=${COMP_WORDS[COMP_CWORD - 2]}: |
|
0.27.30
by Martin von Gagern
Complete values for enum_switch of RegistryOptions. |
111 |
fi
|
112 |
fi
|
|
0.27.31
by Martin von Gagern
Introduce --debug switch to enable some debugging code. |
113 |
%(debug)s
|
0.32.15
by Martin von Gagern
Allow spaces in tag names. Use bash arrays more extensively. |
114 |
cmdOpts=( )
|
115 |
optEnums=( )
|
|
116 |
fixedWords=( )
|
|
0.27.19
by Martin von Gagern
Take global options preceding the command into account. |
117 |
case $cmd in
|
0.27.2
by Martin von Gagern
First programmatic generation of completions. |
118 |
%(cases)s\ |
0.27.1
by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr |
119 |
*)
|
0.32.15
by Martin von Gagern
Allow spaces in tag names. Use bash arrays more extensively. |
120 |
cmdOpts=(--help -h)
|
0.27.1
by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr |
121 |
;;
|
122 |
esac
|
|
123 |
||
0.32.16
by Martin von Gagern
Ensure proper quoting of spaces in tag names. |
124 |
IFS=$'\\n' |
0.32.15
by Martin von Gagern
Allow spaces in tag names. Use bash arrays more extensively. |
125 |
if [[ ${#fixedWords[@]} -eq 0 ]] && [[ ${#optEnums[@]} -eq 0 ]] && [[ $cur != -* ]]; then
|
0.32.1
by Martin von Gagern
Complete tag names. |
126 |
case $curOpt in
|
0.32.20
by Martin von Gagern
Allow completion of tags at end of revision range. |
127 |
tag:|*..tag:)
|
0.32.16
by Martin von Gagern
Ensure proper quoting of spaces in tag names. |
128 |
fixedWords=( $(bzr tags 2>/dev/null | sed 's/ *[^ ]*$//; s/ /\\\\\\\\ /g;') ) |
129 |
;;
|
|
130 |
esac
|
|
131 |
case $cur in
|
|
132 |
[\\"\\']tag:*) |
|
133 |
fixedWords=( $(bzr tags 2>/dev/null | sed 's/ *[^ ]*$//; s/^/tag:/') )
|
|
0.32.1
by Martin von Gagern
Complete tag names. |
134 |
;;
|
0.32.20
by Martin von Gagern
Allow completion of tags at end of revision range. |
135 |
[\\"\\']*..tag:*) |
136 |
fixedWords=( $(bzr tags 2>/dev/null | sed 's/ *[^ ]*$//') )
|
|
0.36.1
by Martin von Gagern
Fix test_revspec_tag_endrange on Mac OS X 10.5.8. |
137 |
fixedWords=( $(for i in "${fixedWords[@]}"; do echo "${cur%%..tag:*}..tag:${i}"; done) ) |
0.32.20
by Martin von Gagern
Allow completion of tags at end of revision range. |
138 |
;;
|
0.32.1
by Martin von Gagern
Complete tag names. |
139 |
esac
|
0.32.15
by Martin von Gagern
Allow spaces in tag names. Use bash arrays more extensively. |
140 |
elif [[ $cur == = ]] && [[ ${#optEnums[@]} -gt 0 ]]; then
|
0.27.30
by Martin von Gagern
Complete values for enum_switch of RegistryOptions. |
141 |
# complete directly after "--option=", list all enum values
|
0.32.15
by Martin von Gagern
Allow spaces in tag names. Use bash arrays more extensively. |
142 |
COMPREPLY=( "${optEnums[@]}" ) |
0.32.3
by Martin von Gagern
Restore completion of registry options immediately after =. |
143 |
return 0
|
0.27.30
by Martin von Gagern
Complete values for enum_switch of RegistryOptions. |
144 |
else
|
0.32.15
by Martin von Gagern
Allow spaces in tag names. Use bash arrays more extensively. |
145 |
fixedWords=( "${cmdOpts[@]}" |
146 |
"${globalOpts[@]}" |
|
147 |
"${optEnums[@]}" |
|
148 |
"${fixedWords[@]}" ) |
|
0.32.1
by Martin von Gagern
Complete tag names. |
149 |
fi
|
150 |
||
0.32.15
by Martin von Gagern
Allow spaces in tag names. Use bash arrays more extensively. |
151 |
if [[ ${#fixedWords[@]} -gt 0 ]]; then
|
152 |
COMPREPLY=( $( compgen -W "${fixedWords[*]}" -- $cur ) ) |
|
0.27.30
by Martin von Gagern
Complete values for enum_switch of RegistryOptions. |
153 |
fi
|
0.27.1
by Martin von Gagern
Original script from bzr.dev/contrib/bash/bzr |
154 |
|
155 |
return 0
|
|
156 |
}
|
|
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
157 |
""" % { |
158 |
"cmds": self.command_names(), |
|
159 |
"function_name": self.function_name, |
|
160 |
"cases": self.command_cases(), |
|
161 |
"global_options": self.global_options(), |
|
162 |
"debug": self.debug_output(), |
|
163 |
})
|
|
0.32.20
by Martin von Gagern
Allow completion of tags at end of revision range. |
164 |
# Help Emacs terminate strings: "
|
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
165 |
|
166 |
def command_names(self): |
|
167 |
return " ".join(self.data.all_command_aliases()) |
|
168 |
||
169 |
def debug_output(self): |
|
170 |
if not self.debug: |
|
171 |
return '' |
|
172 |
else: |
|
173 |
return (r""" |
|
0.27.31
by Martin von Gagern
Introduce --debug switch to enable some debugging code. |
174 |
# Debugging code enabled using the --debug command line switch.
|
175 |
# Will dump some variables to the top portion of the terminal.
|
|
176 |
echo -ne '\e[s\e[H'
|
|
177 |
for (( i=0; i < ${#COMP_WORDS[@]}; ++i)); do
|
|
178 |
echo "\$COMP_WORDS[$i]='${COMP_WORDS[i]}'"$'\e[K' |
|
179 |
done
|
|
180 |
for i in COMP_CWORD COMP_LINE COMP_POINT COMP_TYPE COMP_KEY cur curOpt; do
|
|
181 |
echo "\$${i}=\"${!i}\""$'\e[K' |
|
182 |
done
|
|
183 |
echo -ne '---\e[K\e[u'
|
|
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
184 |
""") |
185 |
||
186 |
def bzr_version(self): |
|
187 |
bzr_version = bzrlib.version_string |
|
188 |
if not self.data.plugins: |
|
189 |
bzr_version += "." |
|
190 |
else: |
|
191 |
bzr_version += " and the following plugins:" |
|
192 |
for name, plugin in sorted(self.data.plugins.iteritems()): |
|
193 |
bzr_version += "\n# %s" % plugin |
|
0.32.9
by Martin von Gagern
Added test cases for DataCollector and BashCodeGen. |
194 |
return bzr_version |
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
195 |
|
196 |
def global_options(self): |
|
197 |
return " ".join(sorted(self.data.global_options)) |
|
198 |
||
199 |
def command_cases(self): |
|
200 |
cases = "" |
|
201 |
for command in self.data.commands: |
|
202 |
cases += self.command_case(command) |
|
203 |
return cases |
|
204 |
||
205 |
def command_case(self, command): |
|
206 |
case = "\t%s)\n" % "|".join(command.aliases) |
|
207 |
if command.plugin: |
|
208 |
case += "\t\t# plugin \"%s\"\n" % command.plugin |
|
209 |
options = [] |
|
210 |
enums = [] |
|
211 |
for option in command.options: |
|
212 |
for message in option.error_messages: |
|
213 |
case += "\t\t# %s\n" % message |
|
214 |
if option.registry_keys: |
|
215 |
for key in option.registry_keys: |
|
216 |
options.append("%s=%s" % (option, key)) |
|
0.32.15
by Martin von Gagern
Allow spaces in tag names. Use bash arrays more extensively. |
217 |
enums.append("%s) optEnums=( %s ) ;;" % |
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
218 |
(option, ' '.join(option.registry_keys))) |
219 |
else: |
|
220 |
options.append(str(option)) |
|
0.32.15
by Martin von Gagern
Allow spaces in tag names. Use bash arrays more extensively. |
221 |
case += "\t\tcmdOpts=( %s )\n" % " ".join(options) |
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
222 |
if command.fixed_words: |
223 |
fixed_words = command.fixed_words |
|
224 |
if isinstance(fixed_words, list): |
|
0.32.15
by Martin von Gagern
Allow spaces in tag names. Use bash arrays more extensively. |
225 |
fixed_words = "( %s )" + ' '.join(fixed_words) |
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
226 |
case += "\t\tfixedWords=%s\n" % fixed_words |
227 |
if enums: |
|
228 |
case += "\t\tcase $curOpt in\n\t\t\t" |
|
229 |
case += "\n\t\t\t".join(enums) |
|
230 |
case += "\n\t\tesac\n" |
|
231 |
case += "\t\t;;\n" |
|
232 |
return case |
|
233 |
||
234 |
||
235 |
class CompletionData(object): |
|
236 |
||
237 |
def __init__(self): |
|
238 |
self.plugins = {} |
|
239 |
self.global_options = set() |
|
240 |
self.commands = [] |
|
241 |
||
242 |
def all_command_aliases(self): |
|
243 |
for c in self.commands: |
|
244 |
for a in c.aliases: |
|
245 |
yield a |
|
246 |
||
247 |
||
248 |
class CommandData(object): |
|
249 |
||
250 |
def __init__(self, name): |
|
251 |
self.name = name |
|
252 |
self.aliases = [name] |
|
253 |
self.plugin = None |
|
254 |
self.options = [] |
|
255 |
self.fixed_words = None |
|
256 |
||
257 |
||
258 |
class PluginData(object): |
|
259 |
||
260 |
def __init__(self, name, version=None): |
|
261 |
if version is None: |
|
0.32.20
by Martin von Gagern
Allow completion of tags at end of revision range. |
262 |
try: |
263 |
version = bzrlib.plugin.plugins()[name].__version__ |
|
264 |
except: |
|
265 |
version = 'unknown' |
|
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
266 |
self.name = name |
267 |
self.version = version |
|
268 |
||
269 |
def __str__(self): |
|
270 |
if self.version == 'unknown': |
|
271 |
return self.name |
|
272 |
return '%s %s' % (self.name, self.version) |
|
273 |
||
274 |
||
275 |
class OptionData(object): |
|
276 |
||
277 |
def __init__(self, name): |
|
278 |
self.name = name |
|
279 |
self.registry_keys = None |
|
280 |
self.error_messages = [] |
|
281 |
||
282 |
def __str__(self): |
|
283 |
return self.name |
|
284 |
||
285 |
def __cmp__(self, other): |
|
286 |
return cmp(self.name, other.name) |
|
287 |
||
288 |
||
289 |
class DataCollector(object): |
|
290 |
||
291 |
def __init__(self, no_plugins=False, selected_plugins=None): |
|
292 |
self.data = CompletionData() |
|
293 |
self.user_aliases = {} |
|
294 |
if no_plugins: |
|
295 |
self.selected_plugins = set() |
|
296 |
elif selected_plugins is None: |
|
297 |
self.selected_plugins = None |
|
298 |
else: |
|
299 |
self.selected_plugins = set([x.replace('-', '_') |
|
300 |
for x in selected_plugins]) |
|
301 |
||
302 |
def collect(self): |
|
303 |
self.global_options() |
|
304 |
self.aliases() |
|
305 |
self.commands() |
|
306 |
return self.data |
|
307 |
||
308 |
def global_options(self): |
|
309 |
re_switch = re.compile(r'\n(--[A-Za-z0-9-_]+)(?:, (-\S))?\s') |
|
310 |
help_text = help_topics.topic_registry.get_detail('global-options') |
|
311 |
for long, short in re_switch.findall(help_text): |
|
312 |
self.data.global_options.add(long) |
|
313 |
if short: |
|
314 |
self.data.global_options.add(short) |
|
315 |
||
316 |
def aliases(self): |
|
317 |
for alias, expansion in config.GlobalConfig().get_aliases().iteritems(): |
|
5245.1.1
by Andrew Bennetts
Fix deprecation warning in bash_completion plugin. |
318 |
for token in cmdline.split(expansion): |
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
319 |
if not token.startswith("-"): |
320 |
self.user_aliases.setdefault(token, set()).add(alias) |
|
321 |
break
|
|
322 |
||
323 |
def commands(self): |
|
324 |
for name in sorted(commands.all_command_names()): |
|
325 |
self.command(name) |
|
326 |
||
327 |
def command(self, name): |
|
328 |
cmd = commands.get_cmd_object(name) |
|
329 |
cmd_data = CommandData(name) |
|
330 |
||
331 |
plugin_name = cmd.plugin_name() |
|
332 |
if plugin_name is not None: |
|
333 |
if (self.selected_plugins is not None and |
|
334 |
plugin not in self.selected_plugins): |
|
0.32.9
by Martin von Gagern
Added test cases for DataCollector and BashCodeGen. |
335 |
return None |
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
336 |
plugin_data = self.data.plugins.get(plugin_name) |
337 |
if plugin_data is None: |
|
338 |
plugin_data = PluginData(plugin_name) |
|
339 |
self.data.plugins[plugin_name] = plugin_data |
|
340 |
cmd_data.plugin = plugin_data |
|
341 |
self.data.commands.append(cmd_data) |
|
0.27.12
by Martin von Gagern
Take user-configured aliases into account. |
342 |
|
343 |
# Find all aliases to the command; both cmd-defined and user-defined.
|
|
344 |
# We assume a user won't override one command with a different one,
|
|
345 |
# but will choose completely new names or add options to existing
|
|
346 |
# ones while maintaining the actual command name unchanged.
|
|
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
347 |
cmd_data.aliases.extend(cmd.aliases) |
348 |
cmd_data.aliases.extend(sorted([useralias |
|
349 |
for cmdalias in cmd_data.aliases |
|
350 |
if cmdalias in self.user_aliases |
|
351 |
for useralias in self.user_aliases[cmdalias] |
|
352 |
if useralias not in cmd_data.aliases])) |
|
353 |
||
0.27.2
by Martin von Gagern
First programmatic generation of completions. |
354 |
opts = cmd.options() |
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
355 |
for optname, opt in sorted(opts.iteritems()): |
356 |
cmd_data.options.extend(self.option(opt)) |
|
357 |
||
358 |
if 'help' == name or 'help' in cmd.aliases: |
|
0.32.15
by Martin von Gagern
Allow spaces in tag names. Use bash arrays more extensively. |
359 |
cmd_data.fixed_words = ('($cmds %s)' % |
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
360 |
" ".join(sorted(help_topics.topic_registry.keys()))) |
361 |
||
0.32.9
by Martin von Gagern
Added test cases for DataCollector and BashCodeGen. |
362 |
return cmd_data |
363 |
||
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
364 |
def option(self, opt): |
365 |
optswitches = {} |
|
366 |
parser = option.get_optparser({opt.name: opt}) |
|
367 |
parser = self.wrap_parser(optswitches, parser) |
|
368 |
optswitches.clear() |
|
369 |
opt.add_option(parser, opt.short_name()) |
|
370 |
if isinstance(opt, option.RegistryOption) and opt.enum_switch: |
|
371 |
enum_switch = '--%s' % opt.name |
|
372 |
enum_data = optswitches.get(enum_switch) |
|
373 |
if enum_data: |
|
0.27.41
by Martin von Gagern
Recover from ImportError when loading a registry. |
374 |
try: |
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
375 |
enum_data.registry_keys = opt.registry.keys() |
0.27.41
by Martin von Gagern
Recover from ImportError when loading a registry. |
376 |
except ImportError, e: |
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
377 |
enum_data.error_messages.append( |
378 |
"ERROR getting registry keys for '--%s': %s" |
|
379 |
% (opt.name, str(e).split('\n')[0])) |
|
380 |
return sorted(optswitches.values()) |
|
381 |
||
382 |
def wrap_container(self, optswitches, parser): |
|
383 |
def tweaked_add_option(*opts, **attrs): |
|
384 |
for name in opts: |
|
385 |
optswitches[name] = OptionData(name) |
|
386 |
parser.add_option = tweaked_add_option |
|
387 |
return parser |
|
388 |
||
389 |
def wrap_parser(self, optswitches, parser): |
|
390 |
orig_add_option_group = parser.add_option_group |
|
391 |
def tweaked_add_option_group(*opts, **attrs): |
|
392 |
return self.wrap_container(optswitches, |
|
393 |
orig_add_option_group(*opts, **attrs)) |
|
394 |
parser.add_option_group = tweaked_add_option_group |
|
395 |
return self.wrap_container(optswitches, parser) |
|
396 |
||
397 |
||
398 |
def bash_completion_function(out, function_name="_bzr", function_only=False, |
|
399 |
debug=False, |
|
400 |
no_plugins=False, selected_plugins=None): |
|
401 |
dc = DataCollector(no_plugins=no_plugins, selected_plugins=selected_plugins) |
|
402 |
data = dc.collect() |
|
403 |
cg = BashCodeGen(data, function_name=function_name, debug=debug) |
|
0.27.7
by Martin von Gagern
Introduce --function-only option |
404 |
if function_only: |
0.32.8
by Martin von Gagern
Refactor to increase modularity. |
405 |
res = cg.function() |
406 |
else: |
|
407 |
res = cg.script() |
|
408 |
out.write(res) |
|
409 |
||
0.27.2
by Martin von Gagern
First programmatic generation of completions. |
410 |
|
5147.5.11
by Martin von Gagern
Register command lazily. |
411 |
class cmd_bash_completion(commands.Command): |
5147.5.14
by Martin von Gagern
Assign user-visible docstrings to __doc__. |
412 |
__doc__ = """Generate a shell function for bash command line completion. |
5147.5.11
by Martin von Gagern
Register command lazily. |
413 |
|
414 |
This command generates a shell function which can be used by bash to
|
|
415 |
automatically complete the currently typed command when the user presses
|
|
416 |
the completion key (usually tab).
|
|
417 |
|
|
418 |
Commonly used like this:
|
|
419 |
eval "`bzr bash-completion`"
|
|
420 |
"""
|
|
421 |
||
422 |
takes_options = [ |
|
423 |
option.Option("function-name", short_name="f", type=str, argname="name", |
|
424 |
help="Name of the generated function (default: _bzr)"), |
|
425 |
option.Option("function-only", short_name="o", type=None, |
|
426 |
help="Generate only the shell function, don't enable it"), |
|
427 |
option.Option("debug", type=None, hidden=True, |
|
428 |
help="Enable shell code useful for debugging"), |
|
429 |
option.ListOption("plugin", type=str, argname="name", |
|
430 |
# param_name="selected_plugins", # doesn't work, bug #387117
|
|
431 |
help="Enable completions for the selected plugin" |
|
432 |
+ " (default: all plugins)"), |
|
433 |
]
|
|
434 |
||
435 |
def run(self, **kwargs): |
|
436 |
if 'plugin' in kwargs: |
|
437 |
# work around bug #387117 which prevents us from using param_name
|
|
5147.5.18
by Martin von Gagern
Include all plugins if --plugins isn't specified (fixes regression). |
438 |
if len(kwargs['plugin']) > 0: |
439 |
kwargs['selected_plugins'] = kwargs['plugin'] |
|
5147.5.11
by Martin von Gagern
Register command lazily. |
440 |
del kwargs['plugin'] |
441 |
bash_completion_function(sys.stdout, **kwargs) |
|
442 |
||
443 |
||
0.27.2
by Martin von Gagern
First programmatic generation of completions. |
444 |
if __name__ == '__main__': |
445 |
||
446 |
import locale |
|
0.27.32
by Martin von Gagern
Added OptionParser for script usage. |
447 |
import optparse |
448 |
||
0.27.38
by Martin von Gagern
Add --plugin switch to selectively include plugins. |
449 |
def plugin_callback(option, opt, value, parser): |
450 |
values = parser.values.selected_plugins |
|
451 |
if value == '-': |
|
452 |
del values[:] |
|
453 |
else: |
|
454 |
values.append(value) |
|
455 |
||
5147.5.12
by Martin von Gagern
Use version of bzrlib. Drop meta.py. |
456 |
parser = optparse.OptionParser(usage="%prog [-f NAME] [-o]") |
0.27.32
by Martin von Gagern
Added OptionParser for script usage. |
457 |
parser.add_option("--function-name", "-f", metavar="NAME", |
458 |
help="Name of the generated function (default: _bzr)") |
|
459 |
parser.add_option("--function-only", "-o", action="store_true", |
|
460 |
help="Generate only the shell function, don't enable it") |
|
461 |
parser.add_option("--debug", action="store_true", |
|
462 |
help=optparse.SUPPRESS_HELP) |
|
0.27.37
by Martin von Gagern
Introduce --no-plugins option to script execution mode. |
463 |
parser.add_option("--no-plugins", action="store_true", |
464 |
help="Don't load any bzr plugins") |
|
0.27.38
by Martin von Gagern
Add --plugin switch to selectively include plugins. |
465 |
parser.add_option("--plugin", metavar="NAME", type="string", |
466 |
dest="selected_plugins", default=[], |
|
467 |
action="callback", callback=plugin_callback, |
|
468 |
help="Enable completions for the selected plugin" |
|
469 |
+ " (default: all plugins)") |
|
0.27.32
by Martin von Gagern
Added OptionParser for script usage. |
470 |
(opts, args) = parser.parse_args() |
471 |
if args: |
|
472 |
parser.error("script does not take positional arguments") |
|
473 |
kwargs = dict() |
|
474 |
for name, value in opts.__dict__.iteritems(): |
|
475 |
if value is not None: |
|
476 |
kwargs[name] = value |
|
0.27.2
by Martin von Gagern
First programmatic generation of completions. |
477 |
|
478 |
locale.setlocale(locale.LC_ALL, '') |
|
0.27.37
by Martin von Gagern
Introduce --no-plugins option to script execution mode. |
479 |
if not kwargs.get('no_plugins', False): |
480 |
plugin.load_plugins() |
|
0.27.2
by Martin von Gagern
First programmatic generation of completions. |
481 |
commands.install_bzr_command_hooks() |
0.27.32
by Martin von Gagern
Added OptionParser for script usage. |
482 |
bash_completion_function(sys.stdout, **kwargs) |