|
| 1 | +#!/Users/jonathan/flask-app/.env/bin/python2.7 |
| 2 | +# PYTHON_ARGCOMPLETE_OK |
| 3 | + |
| 4 | +# Copyright 2012-2013, Andrey Kislyuk and argcomplete contributors. |
| 5 | +# Licensed under the Apache License. See https://github.com/kislyuk/argcomplete for more info. |
| 6 | + |
| 7 | +''' |
| 8 | +Activate the generic bash-completion script for the argcomplete module. |
| 9 | +''' |
| 10 | + |
| 11 | +import os, sys, argparse, argcomplete, shutil, fileinput |
| 12 | + |
| 13 | +parser = argparse.ArgumentParser(description=__doc__, |
| 14 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 15 | + |
| 16 | +dest_opt = parser.add_argument("--dest", default="/etc/bash_completion.d", |
| 17 | + help="Specify the bash completion modules directory to install into") |
| 18 | +parser.add_argument("--user", help="Install into user directory (~/.bash_completion.d/)", action='store_true') |
| 19 | +parser.add_argument("--no-defaults", dest="use_defaults", action="store_false", default=True, |
| 20 | + help="When no matches are generated, do not fallback to readline\'s default completion") |
| 21 | +parser.add_argument("--complete-arguments", nargs=argparse.REMAINDER, |
| 22 | + help="arguments to call complete with; use of this option discards default options") |
| 23 | +argcomplete.autocomplete(parser) |
| 24 | +args = parser.parse_args() |
| 25 | + |
| 26 | +if args.user: |
| 27 | + args.dest = os.path.expanduser("~/.bash_completion.d/") |
| 28 | + if not os.path.exists(args.dest): |
| 29 | + try: |
| 30 | + os.mkdir(args.dest) |
| 31 | + except Exception as e: |
| 32 | + parser.error("Path {d} does not exist and could not be created: {e}".format(d=args.dest, e=e)) |
| 33 | +elif not os.path.exists(args.dest) and args.dest != '-': |
| 34 | + if sys.platform == 'darwin' and args.dest == dest_opt.default and os.path.exists("/usr/local" + dest_opt.default): |
| 35 | + args.dest = "/usr/local" + dest_opt.default |
| 36 | + else: |
| 37 | + parser.error("Path {d} does not exist".format(d=args.dest)) |
| 38 | + |
| 39 | +activator = os.path.join(os.path.dirname(argcomplete.__file__), 'bash_completion.d', 'python-argcomplete.sh') |
| 40 | + |
| 41 | +if args.complete_arguments is None: |
| 42 | + complete_options = '-o default -o bashdefault' if args.use_defaults else '-o bashdefault' |
| 43 | +else: |
| 44 | + complete_options = " ".join(args.complete_arguments) |
| 45 | +complete_call = "complete{} -D -F _python_argcomplete_global".format(" " + complete_options if complete_options else "") |
| 46 | +def replaceCompleteCall(line): |
| 47 | + if line.startswith("complete") and "_python_argcomplete_global" in line: |
| 48 | + return complete_call + ('\n' if line.endswith('\n') else '') |
| 49 | + else: |
| 50 | + return line |
| 51 | + |
| 52 | +if args.dest == '-': |
| 53 | + for l in open(activator): |
| 54 | + sys.stdout.write(replaceCompleteCall(l)) |
| 55 | +else: |
| 56 | + dest = os.path.join(args.dest, "python-argcomplete.sh") |
| 57 | + |
| 58 | + sys.stdout.write("Installing bash completion script " + dest) |
| 59 | + if not args.use_defaults: |
| 60 | + sys.stdout.write(" without -o default") |
| 61 | + elif args.complete_arguments: |
| 62 | + sys.stdout.write(" with options: " + complete_options) |
| 63 | + sys.stdout.write("\n") |
| 64 | + |
| 65 | + try: |
| 66 | + shutil.copy(activator, dest) |
| 67 | + if not args.complete_arguments is None or not args.use_defaults: |
| 68 | + for l in fileinput.input(dest, inplace=True): |
| 69 | + # fileinput with inplace=True redirects stdout to the edited file |
| 70 | + sys.stdout.write(replaceCompleteCall(l)) |
| 71 | + except Exception as e: |
| 72 | + err = str(e) |
| 73 | + if args.dest == dest_opt.default: |
| 74 | + err += "\nPlease try --user to install into a user directory, or --dest to specify the bash completion modules directory" |
| 75 | + parser.error(err) |
0 commit comments