Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/_pytest/_argcomplete.py : 3%

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