comparison env/lib/python3.9/site-packages/argcomplete/completers.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 # Copyright 2012-2019, Andrey Kislyuk and argcomplete contributors.
2 # Licensed under the Apache License. See https://github.com/kislyuk/argcomplete for more info.
3
4 from __future__ import absolute_import, division, print_function, unicode_literals
5
6 import os
7 import subprocess
8 from .compat import str, sys_encoding
9
10 def _call(*args, **kwargs):
11 try:
12 return subprocess.check_output(*args, **kwargs).decode(sys_encoding).splitlines()
13 except subprocess.CalledProcessError:
14 return []
15
16 class ChoicesCompleter(object):
17 def __init__(self, choices):
18 self.choices = choices
19
20 def _convert(self, choice):
21 if isinstance(choice, bytes):
22 choice = choice.decode(sys_encoding)
23 if not isinstance(choice, str):
24 choice = str(choice)
25 return choice
26
27 def __call__(self, **kwargs):
28 return (self._convert(c) for c in self.choices)
29
30 EnvironCompleter = ChoicesCompleter(os.environ)
31
32 class FilesCompleter(object):
33 """
34 File completer class, optionally takes a list of allowed extensions
35 """
36
37 def __init__(self, allowednames=(), directories=True):
38 # Fix if someone passes in a string instead of a list
39 if isinstance(allowednames, (str, bytes)):
40 allowednames = [allowednames]
41
42 self.allowednames = [x.lstrip("*").lstrip(".") for x in allowednames]
43 self.directories = directories
44
45 def __call__(self, prefix, **kwargs):
46 completion = []
47 if self.allowednames:
48 if self.directories:
49 files = _call(["bash", "-c", "compgen -A directory -- '{p}'".format(p=prefix)])
50 completion += [f + "/" for f in files]
51 for x in self.allowednames:
52 completion += _call(["bash", "-c", "compgen -A file -X '!*.{0}' -- '{p}'".format(x, p=prefix)])
53 else:
54 completion += _call(["bash", "-c", "compgen -A file -- '{p}'".format(p=prefix)])
55 anticomp = _call(["bash", "-c", "compgen -A directory -- '{p}'".format(p=prefix)])
56 completion = list(set(completion) - set(anticomp))
57
58 if self.directories:
59 completion += [f + "/" for f in anticomp]
60 return completion
61
62 class _FilteredFilesCompleter(object):
63 def __init__(self, predicate):
64 """
65 Create the completer
66
67 A predicate accepts as its only argument a candidate path and either
68 accepts it or rejects it.
69 """
70 assert predicate, "Expected a callable predicate"
71 self.predicate = predicate
72
73 def __call__(self, prefix, **kwargs):
74 """
75 Provide completions on prefix
76 """
77 target_dir = os.path.dirname(prefix)
78 try:
79 names = os.listdir(target_dir or ".")
80 except:
81 return # empty iterator
82 incomplete_part = os.path.basename(prefix)
83 # Iterate on target_dir entries and filter on given predicate
84 for name in names:
85 if not name.startswith(incomplete_part):
86 continue
87 candidate = os.path.join(target_dir, name)
88 if not self.predicate(candidate):
89 continue
90 yield candidate + "/" if os.path.isdir(candidate) else candidate
91
92 class DirectoriesCompleter(_FilteredFilesCompleter):
93 def __init__(self):
94 _FilteredFilesCompleter.__init__(self, predicate=os.path.isdir)
95
96 class SuppressCompleter(object):
97 """
98 A completer used to suppress the completion of specific arguments
99 """
100
101 def __init__(self):
102 pass
103
104 def suppress(self):
105 """
106 Decide if the completion should be suppressed
107 """
108 return True