Debian Codegraph @

_pytest/_argcomplete.py

raw symbols SCIP
1
2 """allow bash-completion for argparse with argcomplete if installed
3 needs argcomplete>=0.5.6 for python 3.2/3.3 (older versions fail
4 to find the magic string, so _ARGCOMPLETE env. var is never set, and
5 this does not need special code.
6
7 argcomplete does not support python 2.5 (although the changes for that
8 are minor).
9
10 Function try_argcomplete(parser) should be called directly before
11 the call to ArgumentParser.parse_args().
12
13 The filescompleter is what you normally would use on the positional
14 arguments specification, in order to get "dirname/" after "dirn<TAB>"
15 instead of the default "dirname ":
16
17 optparser.add_argument(Config._file_or_dir, nargs='*'
18 ).completer=filescompleter
19
20 Other, application specific, completers should go in the file
21 doing the add_argument calls as they need to be specified as .completer
22 attributes as well. (If argcomplete is not installed, the function the
23 attribute points to will not be used).
24
25 SPEEDUP
26 =======
27 The generic argcomplete script for bash-completion
28 (/etc/bash_completion.d/python-argcomplete.sh )
29 uses a python program to determine startup script generated by pip.
30 You can speed up completion somewhat by changing this script to include
31 # PYTHON_ARGCOMPLETE_OK
32 so the the python-argcomplete-check-easy-install-script does not
33 need to be called to find the entry point of the code and see if that is
34 marked with PYTHON_ARGCOMPLETE_OK
35
36 INSTALL/DEBUGGING
37 =================
38 To include this support in another application that has setup.py generated
39 scripts:
40 - add the line:
41 # PYTHON_ARGCOMPLETE_OK
42 near the top of the main python entry point
43 - include in the file calling parse_args():
44 from _argcomplete import try_argcomplete, filescompleter
45 , call try_argcomplete just before parse_args(), and optionally add
46 filescompleter to the positional arguments' add_argument()
47 If things do not work right away:
48 - switch on argcomplete debugging with (also helpful when doing custom
49 completers):
50 export _ARC_DEBUG=1
51 - run:
52 python-argcomplete-check-easy-install-script $(which appname)
53 echo $?
54 will echo 0 if the magic line has been found, 1 if not
55 - sometimes it helps to find early on errors using:
56 _ARGCOMPLETE=1 _ARC_DEBUG=1 appname
57 which should throw a KeyError: 'COMPLINE' (which is properly set by the
58 global argcomplete script).
59 """
60
61 import sys
62 import os
63 from glob import glob
64
65 class FastFilesCompleter:
66 'Fast file completer class'
67 def __init__(self, directories=True):
68 self.directories = directories
69
70 def __call__(self, prefix, **kwargs):
71 """only called on non option completions"""
72 if os.path.sep in prefix[1:]: #
73 prefix_dir = len(os.path.dirname(prefix) + os.path.sep)
74 else:
75 prefix_dir = 0
76 completion = []
77 globbed = []
78 if '*' not in prefix and '?' not in prefix:
79 if prefix[-1] == os.path.sep: # we are on unix, otherwise no bash
80 globbed.extend(glob(prefix + '.*'))
81 prefix += '*'
82 globbed.extend(glob(prefix))
83 for x in sorted(globbed):
84 if os.path.isdir(x):
85 x += '/'
86 # append stripping the prefix (like bash, not like compgen)
87 completion.append(x[prefix_dir:])
88 return completion
89
90 if os.environ.get('_ARGCOMPLETE'):
91 try:
92 import argcomplete.completers
93 except ImportError:
94 sys.exit(-1)
95 filescompleter = FastFilesCompleter()
96
97 def try_argcomplete(parser):
98 argcomplete.autocomplete(parser)
99 else:
100 def try_argcomplete(parser): pass
101 filescompleter = None