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