MCPcopy Create free account
hub / github.com/pytest-dev/pytest / FilesCompleter

Class FilesCompleter

testing/test_argcomplete.py:35–65  ·  view source on GitHub ↗

File completer class, optionally takes a list of allowed extensions.

Source from the content-addressed store, hash-verified

33
34
35class FilesCompleter:
36 """File completer class, optionally takes a list of allowed extensions."""
37
38 def __init__(self, allowednames=(), directories=True):
39 # Fix if someone passes in a string instead of a list
40 if type(allowednames) is str:
41 allowednames = [allowednames]
42
43 self.allowednames = [x.lstrip("*").lstrip(".") for x in allowednames]
44 self.directories = directories
45
46 def __call__(self, prefix, **kwargs):
47 completion = []
48 if self.allowednames:
49 if self.directories:
50 files = _wrapcall(["bash", "-c", f"compgen -A directory -- '{prefix}'"])
51 completion += [f + "/" for f in files]
52 for x in self.allowednames:
53 completion += _wrapcall(
54 ["bash", "-c", f"compgen -A file -X '!*.{x}' -- '{prefix}'"]
55 )
56 else:
57 completion += _wrapcall(["bash", "-c", f"compgen -A file -- '{prefix}'"])
58
59 anticomp = _wrapcall(["bash", "-c", f"compgen -A directory -- '{prefix}'"])
60
61 completion = list(set(completion) - set(anticomp))
62
63 if self.directories:
64 completion += [f + "/" for f in anticomp]
65 return completion
66
67
68class TestArgComplete:

Callers 2

Calls

no outgoing calls

Tested by 2