Commit b45e5e40 authored by Laurent Bachelier's avatar Laurent Bachelier 🐧 Committed by Romain Bignon

Make the bash completion into a static file

Since we already generate the man pages automatically,
we can generate the bash completions too.
It is much faster and does not risk making any network requests.

fixes #194
parent 779ed946
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright(C) 2010-2014 Laurent Bachelier
# Copyright(C) 2010-2018 Laurent Bachelier
#
# This file is part of weboob.
#
......@@ -28,13 +28,14 @@ import re
import sys
import tempfile
import time
from datetime import datetime
from weboob.tools.application.base import Application
from textwrap import dedent
from weboob.tools.application.base import Application
BASE_PATH = os.path.join(os.path.dirname(__file__), os.pardir)
DEST_DIR = 'man'
COMP_PATH = 'tools/weboob_bash_completion'
class ManpageHelpFormatter(optparse.HelpFormatter):
......@@ -107,6 +108,7 @@ class ManpageHelpFormatter(optparse.HelpFormatter):
def main():
scripts_path = os.path.join(BASE_PATH, "scripts")
files = os.listdir(scripts_path)
completions = dict()
# Create a fake "scripts" modules to import the scripts into
sys.modules["scripts"] = imp.new_module("scripts")
......@@ -132,11 +134,12 @@ def main():
# Find the applications we can handle
for klass in script.__dict__.values():
if inspect.isclass(klass) and issubclass(klass, Application) and klass.VERSION:
analyze_application(klass, fname)
completions[fname] = analyze_application(klass, fname)
finally:
# Cleanup compiled files if needed
if (os.path.isfile(tmpfile + "c")):
os.unlink(tmpfile + "c")
write_completions(completions)
def format_title(title):
......@@ -241,5 +244,37 @@ For full copyright information see the COPYING file in the weboob package.
manfile.write('%s\n' % line.lstrip().encode('utf-8'))
print("wrote %s/%s.1" % (DEST_DIR, script_name))
return application._shell_completion_items()
def write_completions(completions):
compscript = dedent('''
# Weboob completion for Bash (automatically generated by tools/make_man.sh)
#
# vim: filetype=sh expandtab softtabstop=4 shiftwidth=4
#
# This file is part of weboob.
#
# This script can be distributed under the same license as the
# weboob or bash packages.
''')
for name, items in completions.items():
compscript += dedent('''
_weboob_{1}()
{{
local cur args
COMPREPLY=()
cur=${{COMP_WORDS[COMP_CWORD]}}
args="{2}"
COMPREPLY=( $(compgen -o default -W "${{args}}" -- "$cur" ) )
}}
complete -F _weboob_{1} {0}
''').format(name, name.replace('-', '_'), ' '.join(items))
with open(os.path.join(BASE_PATH, COMP_PATH), 'w') as f:
f.write(compscript)
if __name__ == '__main__':
sys.exit(main())
This diff is collapsed.
......@@ -354,15 +354,19 @@ class Application(object):
if ask_debug_mode:
print(debugmsg, file=self.stderr)
def _shell_completion_items(self):
items = set()
for option in self._parser.option_list:
if option.help is not optparse.SUPPRESS_HELP:
items.update(str(option).split('/'))
items.update(self._get_completions())
return items
def parse_args(self, args):
self.options, args = self._parser.parse_args(args)
if self.options.shell_completion:
items = set()
for option in self._parser.option_list:
if option.help is not optparse.SUPPRESS_HELP:
items.update(str(option).split('/'))
items.update(self._get_completions())
items = self._shell_completion_items()
print(' '.join(items))
sys.exit(0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment