def _from_matches(self, module, text): """from_matches(module:str, text:str) : list Create a list of completions for 'text' in the namespace of 'module'. If 'module' is imported: complete on attributes and possible sub-modules; else complete only sub-modules. """ fullname = ".".join([module, text]) completions = [] subpkgs = modulescanner.subpackages(fullname) if not self.use_main_ns: temp_ns = self.namespace self.namespace = sys.modules try: subobjs = self.attr_matches(fullname) except NameError: subobjs = None if not self.use_main_ns: self.namespace = temp_ns if subpkgs: completions.extend([pkg.split('.')[-1] for pkg in subpkgs]) if subobjs: completions.extend([obj.split('.')[-1] for obj in subobjs]) return completions
def _import_matches(self, line, text): """import_matches(line:str, text:str) : list Returns a list of possible completions for an import statement and a partially spelled-out package/module name. Raises StopMatching when we know we're in the correct matcher, but there are no matches. """ basic_match = re.compile(r"\s*(from|import)\b") import_stmt = re.compile(r"\s*(from|import)\s+([\w_\.]+)?$") from_stmt = re.compile(r"\s*from [\w_\.]+ ([\w_\.]*)$") if not basic_match.match(line): return [] import_match = import_stmt.search(line) if import_match: match = re.search(r"from ([\w_.]+) import", line) if match: if '.' in text: raise StopMatching() matches = self._from_matches(match.group(1), text) if not matches: raise StopMatching() else: return matches if '.' in text: matches = modulescanner.subpackages(text) if not matches: raise StopMatching() else: return matches matches = modulescanner.get_completions(text) if not matches: raise StopMatching() else: return matches from_match = from_stmt.match(line) if from_match: if text != "" and not "import".startswith(text): raise StopMatching() else: return ["import"] raise StopMatching()