From c51e541ae2f02b99aeb3b64152f42a883d89fadf Mon Sep 17 00:00:00 2001 From: Thomas Scholtes Date: Sun, 2 Mar 2014 19:59:35 +0100 Subject: [PATCH] Move completion_script into commands module --- beets/ui/commands.py | 71 ++++++++++++++++++++++++++++++++++++++++++ beets/ui/completion.py | 67 --------------------------------------- 2 files changed, 71 insertions(+), 67 deletions(-) delete mode 100644 beets/ui/completion.py diff --git a/beets/ui/commands.py b/beets/ui/commands.py index a1bdf7b5b..1bfc7d0d9 100644 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -23,6 +23,7 @@ import time import itertools import codecs from datetime import datetime +from pkg_resources import resource_string import beets from beets import ui @@ -1300,6 +1301,76 @@ def print_completion(*args): log.warn(u'Warning: Unable to find the bash-completion package. ' u'Command line completion might not work.') +def completion_script(commands): + """Yield the full completion shell script as strings. + + ``commands`` is alist of ``ui.Subcommand`` instances to generate + completion data for. + """ + yield resource_string(__name__, 'completion_base.sh') + + options = {} + aliases = {} + command_names = [] + + # Collect subcommands + for cmd in commands: + name = cmd.name + command_names.append(name) + + for alias in cmd.aliases: + aliases[alias] = name + + options[name] = {'flags': [], 'opts': []} + for opts in cmd.parser._get_all_options()[1:]: + if opts.action in ('store_true', 'store_false'): + option_type = 'flags' + else: + option_type = 'opts' + + options[name][option_type].extend(opts._short_opts + opts._long_opts) + + # Add global options + options['_global'] = { + 'flags': ['-v', '--verbose'], + 'opts': '-l --library -c --config -d --directory -h --help'.split(' ') + } + + # Help subcommand + command_names.append('help') + + # Add flags common to all commands + options['_common'] = { + 'flags': ['-h', '--help'] + } + + # Start generating the script + yield "_beet() {\n" + + # Command names + yield " local commands='%s'\n" % ' '.join(command_names) + yield "\n" + + # Command aliases + yield " local aliases='%s'\n" % ' '.join(aliases.keys()) + for alias, cmd in aliases.items(): + yield " local alias__%s=%s\n" % (alias, cmd) + yield '\n' + + # Fields + yield " fields='%s'\n" % ' '.join( + set(library.ITEM_KEYS + library.ALBUM_KEYS)) + + # Command options + for cmd, opts in options.items(): + for option_type, option_list in opts.items(): + if option_list: + option_list = ' '.join(option_list) + yield " local %s__%s='%s'\n" % (option_type, cmd, option_list) + + yield ' _beet_dispatch\n' + yield '}\n' + completion_cmd.func = print_completion default_commands.append(completion_cmd) diff --git a/beets/ui/completion.py b/beets/ui/completion.py deleted file mode 100644 index 9f4eb1bd6..000000000 --- a/beets/ui/completion.py +++ /dev/null @@ -1,67 +0,0 @@ -from pkg_resources import resource_string -from beets import library - -def completion_script(commands): - yield resource_string(__name__, 'completion_base.sh') - - options = {} - aliases = {} - command_names = [] - - # Collect subcommands - for cmd in commands: - name = cmd.name - command_names.append(name) - - for alias in cmd.aliases: - aliases[alias] = name - - options[name] = {'flags': [], 'opts': []} - for opts in cmd.parser._get_all_options()[1:]: - if opts.action in ('store_true', 'store_false'): - option_type = 'flags' - else: - option_type = 'opts' - - options[name][option_type].extend(opts._short_opts + opts._long_opts) - - # Add global options - options['_global'] = { - 'flags': ['-v', '--verbose'], - 'opts': '-l --library -c --config -d --directory -h --help'.split(' ') - } - - # Help subcommand - command_names.append('help') - - # Add flags common to all commands - options['_common'] = { - 'flags': ['-h', '--help'] - } - - # Start generating the script - yield "_beet() {\n" - - # Command names - yield " local commands='%s'\n" % ' '.join(command_names) - yield "\n" - - # Command aliases - yield " local aliases='%s'\n" % ' '.join(aliases.keys()) - for alias, cmd in aliases.items(): - yield " local alias__%s=%s\n" % (alias, cmd) - yield '\n' - - # Fields - yield " fields='%s'\n" % ' '.join( - set(library.ITEM_KEYS + library.ALBUM_KEYS)) - - # Command options - for cmd, opts in options.items(): - for option_type, option_list in opts.items(): - if option_list: - option_list = ' '.join(option_list) - yield " local %s__%s='%s'\n" % (option_type, cmd, option_list) - - yield ' _beet_dispatch\n' - yield '}\n'