def _change_module(self, resources, changes, factory_name, global_, job_set): if global_: replacement = '__rope_factory_%s_' % factory_name else: replacement = self._new_function_name(factory_name, global_) for file_ in resources: job_set.started_job(file_.path) if file_ == self.resource: self._change_resource(changes, factory_name, global_) job_set.finished_job() continue changed_code = self._rename_occurrences(file_, replacement, global_) if changed_code is not None: if global_: new_pymodule = libutils.get_string_module( self.project, changed_code, self.resource) modname = libutils.modname(self.resource) changed_code, imported = importutils.add_import( self.project, new_pymodule, modname, factory_name) changed_code = changed_code.replace(replacement, imported) changes.add_change(ChangeContents(file_, changed_code)) job_set.finished_job()
def _get_module(self, pyfunction): module = pyfunction.get_module() if module is not None: resource = module.get_resource() if resource is not None: return libutils.modname(resource) + '.' return ''
def _handle_moving_in_from_import_stmt(self, dest, import_stmt, module_imports, parent_module): changed = False context = importutils.importinfo.ImportContext(self.project, None) if import_stmt.import_info.get_imported_resource(context) == \ parent_module: imports = import_stmt.import_info.names_and_aliases new_imports = [] for name, alias in imports: # The moving module was imported. if name == self.old_name: changed = True new_import = importutils.FromImport( libutils.modname(dest), 0, [(self.old_name, alias)]) module_imports.add_import(new_import) else: new_imports.append((name, alias)) # Update the imports if the imported names were changed. if new_imports != imports: changed = True if new_imports: import_stmt.import_info = importutils.FromImport( import_stmt.import_info.module_name, import_stmt.import_info.level, new_imports) else: import_stmt.empty_import() return changed
def _change_import_statements(self, dest, new_name, module_imports): moving_module = self.source parent_module = moving_module.parent changed = False for import_stmt in module_imports.imports: if not any(name_and_alias[0] == self.old_name for name_and_alias in import_stmt.import_info.names_and_aliases) and \ not any(name_and_alias[0] == libutils.modname(self.source) for name_and_alias in import_stmt.import_info.names_and_aliases): continue # Case 1: Look for normal imports of the moving module. if isinstance(import_stmt.import_info, importutils.NormalImport): continue # Case 2: The moving module is from-imported. changed = self._handle_moving_in_from_import_stmt( dest, import_stmt, module_imports, parent_module) or changed # Case 3: Names are imported from the moving module. context = importutils.importinfo.ImportContext(self.project, None) if not import_stmt.import_info.is_empty() and \ import_stmt.import_info.get_imported_resource(context) == \ moving_module: import_stmt.import_info = importutils.FromImport( new_name, import_stmt.import_info.level, import_stmt.import_info.names_and_aliases) changed = True return changed
def _change_occurrences_in_module(self, dest, pymodule=None, resource=None): if not self.tools.occurs_in_module(pymodule=pymodule, resource=resource): return if pymodule is None: pymodule = self.project.get_pymodule(resource) new_name = self._new_modname(dest) module_imports = importutils.get_module_imports(self.project, pymodule) changed = False source = None if libutils.modname(dest): changed = self._change_import_statements(dest, new_name, module_imports) if changed: source = module_imports.get_changed_source() source = self.tools.new_source(pymodule, source) pymodule = self.tools.new_pymodule(pymodule, source) new_import = self._new_import(dest) source = self.tools.rename_in_module( new_name, imports=True, pymodule=pymodule, resource=resource if not changed else None ) should_import = self.tools.occurs_in_module(pymodule=pymodule, resource=resource, imports=False) pymodule = self.tools.new_pymodule(pymodule, source) source = self.tools.remove_old_imports(pymodule) if should_import: pymodule = self.tools.new_pymodule(pymodule, source) source = self.tools.add_imports(pymodule, [new_import]) source = self.tools.new_source(pymodule, source) if source is not None and source != pymodule.resource.read(): return source return None
def _change_occurrences_in_module(self, dest, pymodule=None, resource=None): if not self.tools.occurs_in_module(pymodule=pymodule, resource=resource): return if pymodule is None: pymodule = self.project.get_pymodule(resource) new_name = self._new_modname(dest) module_imports = importutils.get_module_imports(self.project, pymodule) changed = False source = None if libutils.modname(dest): changed = self._change_import_statements(dest, new_name, module_imports) if changed: source = module_imports.get_changed_source() source = self.tools.new_source(pymodule, source) pymodule = self.tools.new_pymodule(pymodule, source) new_import = self._new_import(dest) source = self.tools.rename_in_module( new_name, imports=True, pymodule=pymodule, resource=resource if not changed else None) should_import = self.tools.occurs_in_module( pymodule=pymodule, resource=resource, imports=False) pymodule = self.tools.new_pymodule(pymodule, source) source = self.tools.remove_old_imports(pymodule) if should_import: pymodule = self.tools.new_pymodule(pymodule, source) source = self.tools.add_imports(pymodule, [new_import]) source = self.tools.new_source(pymodule, source) if source is not None and source != pymodule.resource.read(): return source return None
def _change_import_statements(self, dest, new_name, module_imports): moving_module = self.source parent_module = moving_module.parent changed = False for import_stmt in module_imports.imports: if not any( name_and_alias[0] == self.old_name for name_and_alias in import_stmt.import_info.names_and_aliases ) and not any( name_and_alias[0] == libutils.modname(self.source) for name_and_alias in import_stmt.import_info.names_and_aliases ): continue # Case 1: Look for normal imports of the moving module. if isinstance(import_stmt.import_info, importutils.NormalImport): continue # Case 2: The moving module is from-imported. changed = ( self._handle_moving_in_from_import_stmt(dest, import_stmt, module_imports, parent_module) or changed ) # Case 3: Names are imported from the moving module. context = importutils.importinfo.ImportContext(self.project, None) if ( not import_stmt.import_info.is_empty() and import_stmt.import_info.get_imported_resource(context) == moving_module ): import_stmt.import_info = importutils.FromImport( new_name, import_stmt.import_info.level, import_stmt.import_info.names_and_aliases ) changed = True return changed
def _handle_moving_in_from_import_stmt(self, dest, import_stmt, module_imports, parent_module): changed = False context = importutils.importinfo.ImportContext(self.project, None) if import_stmt.import_info.get_imported_resource(context) == parent_module: imports = import_stmt.import_info.names_and_aliases new_imports = [] for name, alias in imports: # The moving module was imported. if name == self.old_name: changed = True new_import = importutils.FromImport(libutils.modname(dest), 0, [(self.old_name, alias)]) module_imports.add_import(new_import) else: new_imports.append((name, alias)) # Update the imports if the imported names were changed. if new_imports != imports: changed = True if new_imports: import_stmt.import_info = importutils.FromImport( import_stmt.import_info.module_name, import_stmt.import_info.level, new_imports ) else: import_stmt.empty_import() return changed
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 _add_import_to_module(project, resource, imported): pymodule = project.get_pymodule(resource) import_tools = importutils.ImportTools(project) module_imports = import_tools.module_imports(pymodule) module_name = libutils.modname(imported) new_import = importutils.NormalImport(((module_name, None), )) module_imports.add_import(new_import) return change.ChangeContents(resource, module_imports.get_changed_source())
def get_from_import(self, resource, name): """The from import statement for `name` in `resource`""" module_name = libutils.modname(resource) names = [] if isinstance(name, list): names = [(imported, None) for imported in name] else: names = [(name, None), ] return FromImport(module_name, 0, tuple(names))
def get_from_import(self, resource, name): """The from import statement for `name` in `resource`""" module_name = libutils.modname(resource) names = [] if isinstance(name, list): names = [(imported, None) for imported in name] else: names = [(name, None)] return FromImport(module_name, 0, tuple(names))
def _pydefined_to_str(pydefined): address = [] if isinstance(pydefined, (builtins.BuiltinClass, builtins.BuiltinFunction)): return "__builtins__." + pydefined.get_name() else: while pydefined.parent is not None: address.insert(0, pydefined.get_name()) pydefined = pydefined.parent module_name = libutils.modname(pydefined.resource) return ".".join(module_name.split(".") + address)
def _pydefined_to_str(pydefined): address = [] if isinstance(pydefined, (builtins.BuiltinClass, builtins.BuiltinFunction)): return '__builtins__.' + pydefined.get_name() else: while pydefined.parent is not None: address.insert(0, pydefined.get_name()) pydefined = pydefined.parent module_name = libutils.modname(pydefined.resource) return '.'.join(module_name.split('.') + address)
def _import_filter(self, stmt): module_name = libutils.modname(self.source) if isinstance(stmt.import_info, importutils.NormalImport): return any(module_name == name for name, alias in stmt.import_info.names_and_aliases) elif isinstance(stmt.import_info, importutils.FromImport): if '.' in module_name: package_name = '.'.join(module_name.split('.')[:-1]) if stmt.import_info.module_name == package_name: return True return stmt.import_info.module_name == module_name return False
def _get_relative_to_absolute_list(self, import_info): result = [] for name, alias in import_info.names_and_aliases: if alias is not None: continue resource = self.project.find_module(name, folder=self.folder) if resource is None: continue absolute_name = libutils.modname(resource) if absolute_name != name: result.append((name, absolute_name)) return result
def visitNormalImport(self, import_stmt, import_info): self.to_be_absolute.extend( self._get_relative_to_absolute_list(import_info)) new_pairs = [] for name, alias in import_info.names_and_aliases: resource = self.project.find_module(name, folder=self.folder) if resource is None: new_pairs.append((name, alias)) continue absolute_name = libutils.modname(resource) new_pairs.append((absolute_name, alias)) if not import_info._are_name_and_alias_lists_equal( new_pairs, import_info.names_and_aliases): import_stmt.import_info = importinfo.NormalImport(new_pairs)
def _import_filter(self, stmt): module_name = libutils.modname(self.source) if isinstance(stmt.import_info, importutils.NormalImport): # Affect any statement that imports the source module return any(module_name == name for name, alias in stmt.import_info.names_and_aliases) elif isinstance(stmt.import_info, importutils.FromImport): # Affect statements importing from the source package if "." in module_name: package_name, basename = module_name.rsplit(".", 1) if stmt.import_info.module_name == package_name and any( basename == name for name, alias in stmt.import_info.names_and_aliases ): return True return stmt.import_info.module_name == module_name return False
def _import_filter(self, stmt): module_name = libutils.modname(self.source) if isinstance(stmt.import_info, importutils.NormalImport): # Affect any statement that imports the source module return any(module_name == name for name, alias in stmt.import_info.names_and_aliases) elif isinstance(stmt.import_info, importutils.FromImport): # Affect statements importing from the source package if '.' in module_name: package_name, basename = module_name.rsplit('.', 1) if (stmt.import_info.module_name == package_name and any(basename == name for name, alias in stmt.import_info.names_and_aliases)): return True return stmt.import_info.module_name == module_name return False
def _new_modname(self, dest): return libutils.modname(dest)
def get_import(self, resource): """The import statement for `resource`""" module_name = libutils.modname(resource) return NormalImport(((module_name, None),))
def _new_modname(self, dest): destname = libutils.modname(dest) if destname: return destname + "." + self.old_name return self.old_name
def _module_name(self, resource): return libutils.modname(resource)
def get_import(self, resource): """The import statement for `resource`""" module_name = libutils.modname(resource) return NormalImport(((module_name, None), ))
def _module_name(self): return libutils.modname(self.resource)
def _new_modname(self, dest): destname = libutils.modname(dest) if destname: return destname + '.' + self.old_name return self.old_name