add bash completion

This commit is contained in:
Christophe Benz 2010-04-22 17:08:37 +02:00
commit a311ba455f
4 changed files with 69 additions and 7 deletions

View file

@ -0,0 +1,33 @@
# weboob completion
# Copyright 2010 Christophe Benz <christophe.benz@gmail.com>
# This script can be distributed under the same license as the
# pastescript or bash packages.
have weboobcfg &&
_weboob()
{
local cur videoob_options commands
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
commands="$(${COMP_WORDS[0]} --commands)"
options="$(${COMP_WORDS[0]} --options)"
case ${COMP_WORDS[1]} in
*)
COMPREPLY=( $( compgen -W "$commands $options" | grep "^$cur" ) )
;;
esac
return 0
}
[ "$have" ] || return
weboob_applications=$(weboobcfg applications)
for application in $weboob_applications
do
complete -F _weboob $application
done
# vim: filetype=sh expandtab softtabstop=4 shiftwidth=4

View file

@ -19,8 +19,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
import ConfigParser
import os
import sys
import weboob
from weboob.tools.application import ConsoleApplication
class WeboobCfg(ConsoleApplication):
@ -57,6 +59,12 @@ class WeboobCfg(ConsoleApplication):
else:
print ' %s' % cap.__name__
@ConsoleApplication.command('List applications')
def command_applications(self, *caps):
applications_path = os.path.abspath(os.path.join(os.path.dirname(weboob.__file__), '..', 'scripts'))
assert os.path.exists(applications_path)
print ' '.join(f for f in os.listdir(applications_path) if not f.startswith('.'))
@ConsoleApplication.command('Display a module')
def command_modinfo(self, name):
try:

View file

@ -59,6 +59,20 @@ class BaseApplication(object):
logging_options.add_option('-q', '--quiet', action='store_true', help='display only error messages')
logging_options.add_option('-v', '--verbose', action='store_true', help='display info messages')
self._parser.add_option_group(logging_options)
self._other_options = OptionGroup(self._parser, 'Other options')
self._other_options.add_option('--options', action='callback', callback=self.print_options,
help='print available options')
self._parser.add_option_group(self._other_options)
def print_options(self, option, opt, value, parser):
result = []
for o in self._parser._get_all_options():
if o._short_opts:
result.append(o._short_opts[0])
if o._long_opts:
result.append(o._long_opts[0])
print ' '.join(result)
sys.exit(0)
def create_weboob(self):
return Weboob(self.APPNAME)

View file

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""
Copyright(C) 2010 Romain Bignon, Julien Hébert
Copyright(C) 2010 Romain Bignon, Julien Hébert, Christophe Benz
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -23,6 +23,7 @@ import sys, tty, termios
import re
from inspect import getargspec
from functools import partial
from optparse import OptionGroup
from weboob.modules import BackendsConfig
@ -107,11 +108,18 @@ class ConsoleApplication(BaseApplication):
if self._parser.description is None:
self._parser.description = ''
self._parser.description += 'Available commands:\n'
for f in self._command_help:
self._parser.description += ' %s\n' % f
for name, arguments, doc_string in self._commands:
command = '%s %s' % (name, arguments)
self._parser.description += ' %-30s %s\n' % (command, doc_string)
self._parser.add_option('-o', '--output-format', choices=formatters.keys(),
help='output format %s (default: table)' % formatters.keys())
self._other_options.add_option('--commands', action='callback', callback=self.print_commands,
help='print available commands')
def print_commands(self, option, opt, value, parser):
print ' '.join(name for name, arguments, doc_string in self._commands)
sys.exit(0)
def ask(self, question, default=None, masked=False, regexp=None):
"""
@ -207,8 +215,8 @@ class ConsoleApplication(BaseApplication):
else:
raise Exception('Should never go here')
_command_help = []
def register_command(f, doc_string, register_to=_command_help):
_commands = []
def register_command(f, doc_string, register_to=_commands):
def get_arguments(func, skip=0):
"""
Get arguments of a function as a string.
@ -228,8 +236,7 @@ class ConsoleApplication(BaseApplication):
return " ".join(args)
command_name = f.func_name.replace('command_', '')
command = '%s %s' % (command_name, get_arguments(f))
register_to.append('%-30s %s' % (command, doc_string))
register_to.append((command_name, get_arguments(f), doc_string))
return f
def command(doc_string, f=register_command):