def visitFromImport(self, import_stmt, import_info): resource = import_info.get_imported_resource(self.context) if resource is None: return if resource == self.resource: self._importing_names_from_self(import_info, import_stmt) return pymodule = self.project.get_pymodule(resource) new_pairs = [] for name, alias in import_info.names_and_aliases: try: result = pymodule[name].get_object() if isinstance(result, pyobjects.PyModule) and \ result.get_resource() == self.resource: imported = name if alias is not None: imported = alias self.to_be_fixed.add(imported) else: new_pairs.append((name, alias)) except exceptions.AttributeNotFoundError: new_pairs.append((name, alias)) if not import_info._are_name_and_alias_lists_equal( new_pairs, import_info.names_and_aliases): import_stmt.import_info = importinfo.FromImport( import_info.module_name, import_info.level, new_pairs)
def visitFromImport(self, import_stmt, import_info): resource = import_info.get_imported_resource(self.context) if resource is None: return None absolute_name = libutils.modname(resource) if import_info.module_name != absolute_name: import_stmt.import_info = importinfo.FromImport( absolute_name, 0, import_info.names_and_aliases)
def visitNormalImport(self, import_stmt, import_info): for name, alias in import_info.names_and_aliases: if alias is None and self._is_long(name): self.to_be_renamed.add(name) last_dot = name.rindex('.') from_ = name[:last_dot] imported = name[last_dot + 1:] self.new_imports.append( importinfo.FromImport(from_, 0, ((imported, None), )))
def visitFromImport(self, import_stmt, import_info): if import_info.is_star_import(): new_pairs = [] for name in import_info.get_imported_names(self.context): new_pairs.append((name, None)) new_import = importinfo.FromImport(import_info.module_name, import_info.level, new_pairs) import_stmt.import_info = \ self.filtering.visitFromImport(None, new_import) else: self.filtering.dispatch(import_stmt)
def visitFromImport(self, import_stmt, import_info): new_pairs = [] if not import_info.is_star_import(): for name, alias in import_info.names_and_aliases: try: pyname = self.pymodule[alias or name] if occurrences.same_pyname(self.pyname, pyname): continue except exceptions.AttributeNotFoundError: pass new_pairs.append((name, alias)) return importinfo.FromImport(import_info.module_name, import_info.level, new_pairs)
def visit_from(self, node, end_line): level = 0 if node.level: level = node.level import_info = importinfo.FromImport( node.module or '', # see comment at rope.base.ast.walk level, self._get_names(node.names)) start_line = node.lineno self.imports.append(importinfo.ImportStatement( import_info, node.lineno, end_line, self._get_text(start_line, end_line), blank_lines= self._count_empty_lines_before(start_line)))
def visitFromImport(self, import_stmt, import_info): if _is_future(import_info): return import_info new_pairs = [] if import_info.is_star_import(): for name in import_info.get_imported_names(self.context): if self.can_select(name, None): new_pairs.append(import_info.names_and_aliases[0]) break else: for name, alias in import_info.names_and_aliases: if self.can_select(name, alias): new_pairs.append((name, alias)) return importinfo.FromImport(import_info.module_name, import_info.level, new_pairs)
def force_single_imports(self): """force a single import per statement""" for import_stmt in self.imports[:]: import_info = import_stmt.import_info if import_info.is_empty() or import_stmt.readonly: continue if len(import_info.names_and_aliases) > 1: for name_and_alias in import_info.names_and_aliases: if hasattr(import_info, "module_name"): new_import = importinfo.FromImport( import_info.module_name, import_info.level, [name_and_alias]) else: new_import = importinfo.NormalImport([name_and_alias]) self.add_import(new_import) import_stmt.empty_import()
def visitFromImport(self, import_stmt, import_info): if isinstance(self.import_info, import_info.__class__) and \ import_info.module_name == self.import_info.module_name and \ import_info.level == self.import_info.level: if import_info.is_star_import(): return True if self.import_info.is_star_import(): import_stmt.import_info = self.import_info return True new_pairs = list(import_info.names_and_aliases) for pair in self.import_info.names_and_aliases: if pair not in new_pairs: new_pairs.append(pair) import_stmt.import_info = importinfo.FromImport( import_info.module_name, import_info.level, new_pairs) return True