commit 366195ca5d7000aa7776be9171c3b6b376da455c Author: Damian Johnson atagar@torproject.org Date: Sat Apr 19 19:03:18 2014 -0700
Revise _get_commands() helper
Little refactoring for our _get_commands() helper to reduce some duplicate bits and minor improvements for readability. The only functional change is that we no longer blindly add /help commands for everything. Rather, we add help for commands with usage information. --- stem/interpretor/autocomplete.py | 76 ++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 44 deletions(-)
diff --git a/stem/interpretor/autocomplete.py b/stem/interpretor/autocomplete.py index ff61941..a94c09d 100644 --- a/stem/interpretor/autocomplete.py +++ b/stem/interpretor/autocomplete.py @@ -19,67 +19,55 @@ def _get_commands(config, controller):
commands = config.get('autocomplete', [])
- # GETINFO commands + # GETINFO commands. Lines are of the form '[option] -- [description]'. This + # strips '*' from options that accept values.
- getinfo_options = controller.get_info('info/names', None) + results = controller.get_info('info/names', None)
- if getinfo_options: - # Lines are of the form '[option] -- [description]'. This strips '*' from - # options that accept values. - - options = [line.split(' ', 1)[0].rstrip('*') for line in getinfo_options.splitlines()] - - commands += ['GETINFO %s' % opt for opt in options] + if results: + for line in results.splitlines(): + option = line.split(' ', 1)[0].rstrip('*') + commands.append('GETINFO %s' % option) else: commands.append('GETINFO ')
- # GETCONF, SETCONF, and RESETCONF commands - - config_options = controller.get_info('config/names', None) + # GETCONF, SETCONF, and RESETCONF commands. Lines are of the form + # '[option] [type]'.
- if config_options: - # individual options are '[option] [type]' pairs + results = controller.get_info('config/names', None)
- entries = [opt.split(' ', 1)[0] for opt in config_options.splitlines()] + if results: + for line in results.splitlines(): + option = line.split(' ', 1)[0]
- commands += ['GETCONF %s' % opt for opt in entries] - commands += ['SETCONF %s ' % opt for opt in entries] - commands += ['RESETCONF %s' % opt for opt in entries] + commands.append('GETCONF %s' % option) + commands.append('SETCONF %s' % option) + commands.append('RESETCONF %s' % option) else: commands += ['GETCONF ', 'SETCONF ', 'RESETCONF ']
- # SETEVENT commands - - events = controller.get_info('events/names', None) - - if events: - commands += ['SETEVENTS %s' % event for event in events.split(' ')] - else: - commands.append('SETEVENTS ') + # SETEVENT, USEFEATURE, and SIGNAL commands. For each of these the GETINFO + # results are simply a space separated lists of the values they can have.
- # USEFEATURE commands + options = ( + ('SETEVENTS ', 'events/names'), + ('USEFEATURE ', 'features/names'), + ('SIGNAL ', 'signal/names'), + )
- features = controller.get_info('features/names', None) + for prefix, getinfo_cmd in options: + results = controller.get_info(getinfo_cmd, None)
- if features: - commands += ['USEFEATURE %s' % feature for feature in features.split(' ')] - else: - commands.append('USEFEATURE ') - - # SIGNAL commands - - signals = controller.get_info('signal/names', None) - - if signals: - commands += ['SIGNAL %s' % signal for signal in signals.split(' ')] - else: - commands.append('SIGNAL ') + if results: + commands += [prefix + value for value in results.split()] + else: + commands.append(prefix)
- # adds help options for the previous commands + # Adds /help commands.
- base_cmd = set([cmd.split(' ')[0].replace('+', '').replace('/', '') for cmd in commands]) + usage_info = config.get('help.usage', {})
- for cmd in base_cmd: + for cmd in usage_info.keys(): commands.append('/help ' + cmd)
return commands
tor-commits@lists.torproject.org