| 41 | __all__ = ["Completer"] |
| 42 | |
| 43 | class Completer: |
| 44 | def __init__(self, namespace = None): |
| 45 | """Create a new completer for the command line. |
| 46 | |
| 47 | Completer([namespace]) -> completer instance. |
| 48 | |
| 49 | If unspecified, the default namespace where completions are performed |
| 50 | is __main__ (technically, __main__.__dict__). Namespaces should be |
| 51 | given as dictionaries. |
| 52 | |
| 53 | Completer instances should be used as the completion mechanism of |
| 54 | readline via the set_completer() call: |
| 55 | |
| 56 | readline.set_completer(Completer(my_namespace).complete) |
| 57 | """ |
| 58 | |
| 59 | if namespace and not isinstance(namespace, dict): |
| 60 | raise TypeError('namespace must be a dictionary') |
| 61 | |
| 62 | # Don't bind to namespace quite yet, but flag whether the user wants a |
| 63 | # specific namespace or to use __main__.__dict__. This will allow us |
| 64 | # to bind to __main__.__dict__ at completion time, not now. |
| 65 | if namespace is None: |
| 66 | self.use_main_ns = 1 |
| 67 | else: |
| 68 | self.use_main_ns = 0 |
| 69 | self.namespace = namespace |
| 70 | |
| 71 | def complete(self, text, state): |
| 72 | """Return the next possible completion for 'text'. |
| 73 | |
| 74 | This is called successively with state == 0, 1, 2, ... until it |
| 75 | returns None. The completion should begin with 'text'. |
| 76 | |
| 77 | """ |
| 78 | if self.use_main_ns: |
| 79 | self.namespace = __main__.__dict__ |
| 80 | |
| 81 | if not text.strip(): |
| 82 | if state == 0: |
| 83 | if _readline_available: |
| 84 | readline.insert_text('\t') |
| 85 | readline.redisplay() |
| 86 | return '' |
| 87 | else: |
| 88 | return '\t' |
| 89 | else: |
| 90 | return None |
| 91 | |
| 92 | if state == 0: |
| 93 | with warnings.catch_warnings(action="ignore"): |
| 94 | if "." in text: |
| 95 | self.matches = self.attr_matches(text) |
| 96 | else: |
| 97 | self.matches = self.global_matches(text) |
| 98 | try: |
| 99 | return self.matches[state] |
| 100 | except IndexError: |
no outgoing calls
no test coverage detected
searching dependent graphs…