Commits

Anonymous committed 6058a29

Handling pydocs when inlining functions
Addin remove/rename current module

  • Participants
  • Parent commits 4cb368e

Comments (0)

Files changed (23)

docs/dev/done.txt

 ===========
 
 
+- Moving/renaming current module/package : December 25, 2006
+
+
 - Removing imports from the same module : December 22, 2006
 
 

docs/dev/issues.txt

 * Updating global `PyCore` after performing changes
 
 
-Gradual Implementation
-----------------------
-
-Since this would be a big refactoring we need to divide this
-refactoring into a few smaller tasks.
-
-
 Simple Implementation
 ---------------------
 

docs/dev/stories.txt

 * Sorting imports; standard, third party, project
 
 
-* Moving/renaming current module/package
-
-
 * Commanding buffer
 
 

docs/dev/workingon.txt

-Removing Imports From The Same Module
-=====================================
+Small Stories
+=============
 
-- Adding `rope.base.fscommands.FileAccess` class
-- Refactoring `importutils` module
-- Moving operations from `ImportInfo`\s to visitors
-- Adding `rope.refactor.importutils` package; needs changing ``setup.py``
-- Not using `ImportInfoVisitor.dispatch` in favor of `ImportStatement.accept`
-- Changing organize imports to use `ImportTools.organize_imports`
-- Organizing imports after move refactorings
-- What if there is no dot after ``mod``
-
-* Importing star
-* Definition location for variables inside loops
-* Choosing better names for import related modules
-  visitors -> actions
-* Adding tools to ease working on long files
-* Supporting ``from w.x.y import z`` for organizing long imports;
-  ``Use long import list format`` refactoring
+- Method inlining and pydocs
+- Move current module
+- Moving ``__init__.py``
+- Rename current module
+- Renaming ``__init__.py``
 
 
 Remaining Stories
 =================
 
 * Allowing running code to call rope functions while rope is running?
+* Importing star and removing self imports; stack overflow
+* Definition location for variables inside loops
+* Supporting ``from w.x.y import z`` for organizing long imports;
+  ``Use long import list format`` refactoring
+* Change relative imports to ``2.5`` style
 * Extract constant
-* Method inlining and pydocs
 * What to do if a file is removed while editing
 * Allowing non-existent resources?
 * Problems for inside list comprehension assists
 * Better `ropetest` package structure
 * Decide when to use `difflib` in `Editor.set_text`
 * Handling `AssList` for inline variable and encapsulate field
-* Changing `Rename` to take an `OccurrenceFinder`
 * ``break`` and ``continue`` in extract method
 
 * Considering logical lines in `rope.codeanalyze`

rope/refactor/encapsulate_field.py

 import rope.base.codeanalyze
-import rope.refactor.occurrences
+from rope.refactor import occurrences
 
 from rope.refactor import sourceutils
 from rope.refactor.change import ChangeSet, ChangeContents

rope/refactor/importutils/__init__.py

 
 
 import rope.base.pynames
-import rope.refactor.rename
-
+from rope.refactor import rename
+from rope.refactor import occurrences
 from rope.refactor.importutils.importinfo import \
      (NormalImport, FromImport, get_module_name)
 from rope.refactor.importutils import module_imports
             imported = name
             if alias is not None:
                 imported = alias
-            rename_in_module = rope.refactor.rename.RenameInModule(
-                self.pycore, [imported_pymodule.get_attribute(name)], imported,
-                module_name + '.' + name, replace_primary=True, imports=False)
-            source = rename_in_module.get_changed_module(pymodule=pymodule)
+            occurrence_finder = occurrences.FilteredOccurrenceFinder(
+                self.pycore, imported, [imported_pymodule.get_attribute(name)],
+                imports=False)
+            source = rename.rename_in_module(occurrence_finder, module_name + '.' + name,
+                                             pymodule=pymodule, replace_primary=True)
             if source is not None:
                 pymodule = self.pycore.get_string_module(source, resource)
         return pymodule

rope/refactor/importutils/actions.py

+from rope.base import pyobjects
+from rope.base import exceptions
+from rope.refactor.importutils import importinfo
+
+
+class ImportInfoVisitor(object):
+    
+    def dispatch(self, import_):
+        method = getattr(self, 'visit' + import_.import_info.__class__.__name__)
+        return method(import_, import_.import_info)
+
+    def visitEmptyImport(self, import_stmt, import_info):
+        pass
+    
+    def visitNormalImport(self, import_stmt, import_info):
+        pass
+    
+    def visitFromImport(self, import_stmt, import_info):
+        pass
+    
+
+class RelativeToAbsoluteVisitor(ImportInfoVisitor):
+    
+    def __init__(self, pycore, current_folder):
+        self.to_be_absolute = []
+        self.pycore = pycore
+        self.current_folder = current_folder
+    
+    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.pycore.find_module(name, current_folder=self.current_folder)
+            if resource is None:
+                new_pairs.append((name, alias))
+                continue
+            absolute_name = importinfo.get_module_name(self.pycore, 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 _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.pycore.find_module(name, current_folder=self.current_folder)
+            if resource is None:
+                continue
+            absolute_name = importinfo.get_module_name(self.pycore, resource)
+            if absolute_name != name:
+                result.append((name, absolute_name))
+        return result
+    
+    def visitFromImport(self, import_stmt, import_info):
+        if import_info.level == 0:
+            resource = self.pycore.find_module(import_info.module_name,
+                                               current_folder=self.current_folder)
+        else:
+            resource = self.pycore.find_relative_module(
+                import_info.module_name, self.current_folder, import_info.level)
+        if resource is None:
+            return None
+        absolute_name = importinfo.get_module_name(self.pycore, resource)
+        if import_info.module_name != absolute_name:
+            import_stmt.import_info = importinfo.FromImport(
+                absolute_name, 0, import_info.names_and_aliases,
+                self.current_folder, self.pycore)
+    
+
+class FilteringVisitor(ImportInfoVisitor):
+    
+    def __init__(self, pycore, can_select):
+        self.to_be_absolute = []
+        self.pycore = pycore
+        self.can_select = self._transform_can_select(can_select)
+    
+    def _transform_can_select(self, can_select):
+        def can_select_name_and_alias(name, alias):
+            imported = name
+            if alias is not None:
+                imported = alias
+            return can_select(imported)
+        return can_select_name_and_alias
+    
+    def visitNormalImport(self, import_stmt, import_info):
+        new_pairs = []
+        for name, alias in import_info.names_and_aliases:
+            if self.can_select(name, alias):
+                new_pairs.append((name, alias))
+        return importinfo.NormalImport(new_pairs)
+
+    def visitFromImport(self, import_stmt, import_info):
+        new_pairs = []
+        if import_info.is_star_import():
+            for name in import_info.get_imported_names():
+                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,
+            import_info.current_folder, self.pycore)
+
+
+class RemovingVisitor(ImportInfoVisitor):
+    
+    def __init__(self, pycore, can_select):
+        self.to_be_absolute = []
+        self.pycore = pycore
+        self.filtering = FilteringVisitor(pycore, can_select)
+    
+    def dispatch(self, import_):
+        result = self.filtering.dispatch(import_)
+        if result is not None:
+            import_.import_info = result
+
+
+class AddingVisitor(ImportInfoVisitor):
+    
+    def __init__(self, pycore, import_info):
+        self.pycore = pycore
+        self.import_info = import_info
+    
+    def visitNormalImport(self, import_stmt, import_info):
+        if not isinstance(self.import_info, import_info.__class__):
+            return False
+        # Adding ``import x`` and ``import x.y`` that results ``import x.y``
+        if len(import_info.names_and_aliases) == len(self.import_info.names_and_aliases) == 1:
+            imported1 = import_info.names_and_aliases[0]
+            imported2 = self.import_info.names_and_aliases[0]
+            if imported1[1] == imported2[1] == None:
+                if imported1[0].startswith(imported2[0] + '.'):
+                    return True
+                if imported2[0].startswith(imported1[0] + '.'):
+                    import_stmt.import_info = self.import_info
+                    return True
+        # Multiple imports using a single import statement is discouraged
+        # so we won't bother adding them.
+        if self.import_info._are_name_and_alias_lists_equal(
+            import_info.names_and_aliases, self.import_info.names_and_aliases):
+            return True
+    
+    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,
+                import_info.current_folder, import_info.pycore)
+            return True
+
+
+class ExpandStarsVisitor(ImportInfoVisitor):
+    
+    def __init__(self, pycore, can_select):
+        self.pycore = pycore
+        self.filtering = FilteringVisitor(pycore, can_select)
+
+    def visitNormalImport(self, import_stmt, import_info):
+        self.filtering.dispatch(import_stmt)
+
+    def visitFromImport(self, import_stmt, import_info):
+        if import_info.is_star_import():
+            new_pairs = []
+            for name in import_info.get_imported_names():
+                new_pairs.append((name, None))
+            new_import = importinfo.FromImport(
+                import_info.module_name, import_info.level, new_pairs,
+                import_info.current_folder, self.pycore)
+            import_stmt.import_info = self.filtering.visitFromImport(None, new_import)
+        else:
+            self.filtering.dispatch(import_stmt)
+
+
+class SelfImportVisitor(ImportInfoVisitor):
+    
+    def __init__(self, pycore, current_folder, resource):
+        self.pycore = pycore
+        self.current_folder = current_folder
+        self.resource = resource
+        self.to_be_fixed = set()
+        self.to_be_renamed = set()
+    
+    def visitNormalImport(self, import_stmt, import_info):
+        new_pairs = []
+        for name, alias in import_info.names_and_aliases:
+            resource = self.pycore.find_module(name, current_folder=self.current_folder)
+            if resource is not None and resource == self.resource:
+                imported = name
+                if alias is not None:
+                    imported = alias
+                self.to_be_fixed.add(imported)
+            else:
+                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.NormalImport(new_pairs)
+    
+    def visitFromImport(self, import_stmt, import_info):
+        if import_info.level == 0:
+            resource = self.pycore.find_module(import_info.module_name,
+                                               current_folder=self.current_folder)
+        else:
+            resource = self.pycore.find_relative_module(
+                import_info.module_name, self.current_folder, import_info.level)
+        if resource is None:
+            return
+        if resource == self.resource:
+            self._importing_names_from_self(import_info, import_stmt)
+            return
+        pymodule = self.pycore.resource_to_pyobject(resource)
+        new_pairs = []
+        for name, alias in import_info.names_and_aliases:
+            try:
+                result = pymodule.get_attribute(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.AttributeNotFoundException:
+                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,
+                import_info.current_folder, self.pycore)
+
+    def _importing_names_from_self(self, import_info, import_stmt):
+        if not import_info.is_star_import():
+            for name, alias in import_info.names_and_aliases:
+                if alias is not None:
+                    self.to_be_renamed.add((alias, name))
+        import_stmt.empty_import()

rope/refactor/importutils/module_imports.py

 
 import rope.base.pynames
 from rope.refactor.importutils import importinfo
-from rope.refactor.importutils import visitors
+from rope.refactor.importutils import actions
 
 
 class ModuleImports(object):
     
     def remove_unused_imports(self):
         can_select = _OneTimeSelector(self._get_unbound_names(self.pymodule))
-        visitor = visitors.RemovingVisitor(self.pycore, can_select)
+        visitor = actions.RemovingVisitor(self.pycore, can_select)
         for import_statement in self.get_import_statements():
             import_statement.accept(visitor)
     
         all_import_statements = self.get_import_statements()
         result = []
         can_select = _OneTimeSelector(self._get_unbound_names(defined_pyobject))
-        visitor = visitors.FilteringVisitor(self.pycore, can_select)
+        visitor = actions.FilteringVisitor(self.pycore, can_select)
         for import_statement in all_import_statements:
             new_import = import_statement.accept(visitor)
             if new_import is not None and not new_import.is_empty():
         return ''.join(result)
     
     def add_import(self, import_info):
-        visitor = visitors.AddingVisitor(self.pycore, import_info)
+        visitor = actions.AddingVisitor(self.pycore, import_info)
         for import_statement in self.get_import_statements():
             if import_statement.accept(visitor):
                 break
             all_imports.append(importinfo.ImportStatement(import_info, last_line, last_line))
     
     def filter_names(self, can_select):
-        visitor = visitors.RemovingVisitor(self.pycore, can_select)
+        visitor = actions.RemovingVisitor(self.pycore, can_select)
         for import_statement in self.get_import_statements():
             import_statement.accept(visitor)
     
     def expand_stars(self):
         can_select = _OneTimeSelector(self._get_unbound_names(self.pymodule))
-        visitor = visitors.ExpandStarsVisitor(self.pycore, can_select)
+        visitor = actions.ExpandStarsVisitor(self.pycore, can_select)
         for import_statement in self.get_import_statements():
             import_statement.accept(visitor)
     
         imports = self.get_import_statements()
         added_imports = []
         for import_stmt in imports:
-            visitor = visitors.AddingVisitor(self.pycore, import_stmt.import_info)
+            visitor = actions.AddingVisitor(self.pycore, import_stmt.import_info)
             for added_import in added_imports:
                 if added_import.accept(visitor):
                     import_stmt.empty_import()
                 added_imports.append(import_stmt)
     
     def get_relative_to_absolute_list(self):
-        visitor = rope.refactor.importutils.visitors.RelativeToAbsoluteVisitor(
+        visitor = rope.refactor.importutils.actions.RelativeToAbsoluteVisitor(
             self.pycore, self.pymodule.get_resource().get_parent())
         for import_stmt in self.get_import_statements():
             import_stmt.accept(visitor)
         return visitor.to_be_absolute
 
     def get_self_import_fix_and_rename_list(self):
-        visitor = rope.refactor.importutils.visitors.SelfImportVisitor(
+        visitor = rope.refactor.importutils.actions.SelfImportVisitor(
             self.pycore, self.pymodule.get_resource().get_parent(),
             self.pymodule.get_resource())
         for import_stmt in self.get_import_statements():

rope/refactor/importutils/visitors.py

-from rope.base import pyobjects
-from rope.base import exceptions
-from rope.refactor.importutils import importinfo
-
-
-class ImportInfoVisitor(object):
-    
-    def dispatch(self, import_):
-        method = getattr(self, 'visit' + import_.import_info.__class__.__name__)
-        return method(import_, import_.import_info)
-
-    def visitEmptyImport(self, import_stmt, import_info):
-        pass
-    
-    def visitNormalImport(self, import_stmt, import_info):
-        pass
-    
-    def visitFromImport(self, import_stmt, import_info):
-        pass
-    
-
-class RelativeToAbsoluteVisitor(ImportInfoVisitor):
-    
-    def __init__(self, pycore, current_folder):
-        self.to_be_absolute = []
-        self.pycore = pycore
-        self.current_folder = current_folder
-    
-    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.pycore.find_module(name, current_folder=self.current_folder)
-            if resource is None:
-                new_pairs.append((name, alias))
-                continue
-            absolute_name = importinfo.get_module_name(self.pycore, 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 _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.pycore.find_module(name, current_folder=self.current_folder)
-            if resource is None:
-                continue
-            absolute_name = importinfo.get_module_name(self.pycore, resource)
-            if absolute_name != name:
-                result.append((name, absolute_name))
-        return result
-    
-    def visitFromImport(self, import_stmt, import_info):
-        if import_info.level == 0:
-            resource = self.pycore.find_module(import_info.module_name,
-                                               current_folder=self.current_folder)
-        else:
-            resource = self.pycore.find_relative_module(
-                import_info.module_name, self.current_folder, import_info.level)
-        if resource is None:
-            return None
-        absolute_name = importinfo.get_module_name(self.pycore, resource)
-        if import_info.module_name != absolute_name:
-            import_stmt.import_info = importinfo.FromImport(
-                absolute_name, 0, import_info.names_and_aliases,
-                self.current_folder, self.pycore)
-    
-
-class FilteringVisitor(ImportInfoVisitor):
-    
-    def __init__(self, pycore, can_select):
-        self.to_be_absolute = []
-        self.pycore = pycore
-        self.can_select = self._transform_can_select(can_select)
-    
-    def _transform_can_select(self, can_select):
-        def can_select_name_and_alias(name, alias):
-            imported = name
-            if alias is not None:
-                imported = alias
-            return can_select(imported)
-        return can_select_name_and_alias
-    
-    def visitNormalImport(self, import_stmt, import_info):
-        new_pairs = []
-        for name, alias in import_info.names_and_aliases:
-            if self.can_select(name, alias):
-                new_pairs.append((name, alias))
-        return importinfo.NormalImport(new_pairs)
-
-    def visitFromImport(self, import_stmt, import_info):
-        new_pairs = []
-        if import_info.is_star_import():
-            for name in import_info.get_imported_names():
-                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,
-            import_info.current_folder, self.pycore)
-
-
-class RemovingVisitor(ImportInfoVisitor):
-    
-    def __init__(self, pycore, can_select):
-        self.to_be_absolute = []
-        self.pycore = pycore
-        self.filtering = FilteringVisitor(pycore, can_select)
-    
-    def dispatch(self, import_):
-        result = self.filtering.dispatch(import_)
-        if result is not None:
-            import_.import_info = result
-
-
-class AddingVisitor(ImportInfoVisitor):
-    
-    def __init__(self, pycore, import_info):
-        self.pycore = pycore
-        self.import_info = import_info
-    
-    def visitNormalImport(self, import_stmt, import_info):
-        if not isinstance(self.import_info, import_info.__class__):
-            return False
-        # Adding ``import x`` and ``import x.y`` that results ``import x.y``
-        if len(import_info.names_and_aliases) == len(self.import_info.names_and_aliases) == 1:
-            imported1 = import_info.names_and_aliases[0]
-            imported2 = self.import_info.names_and_aliases[0]
-            if imported1[1] == imported2[1] == None:
-                if imported1[0].startswith(imported2[0] + '.'):
-                    return True
-                if imported2[0].startswith(imported1[0] + '.'):
-                    import_stmt.import_info = self.import_info
-                    return True
-        # Multiple imports using a single import statement is discouraged
-        # so we won't bother adding them.
-        if self.import_info._are_name_and_alias_lists_equal(
-            import_info.names_and_aliases, self.import_info.names_and_aliases):
-            return True
-    
-    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,
-                import_info.current_folder, import_info.pycore)
-            return True
-
-
-class ExpandStarsVisitor(ImportInfoVisitor):
-    
-    def __init__(self, pycore, can_select):
-        self.pycore = pycore
-        self.filtering = FilteringVisitor(pycore, can_select)
-
-    def visitNormalImport(self, import_stmt, import_info):
-        self.filtering.dispatch(import_stmt)
-
-    def visitFromImport(self, import_stmt, import_info):
-        if import_info.is_star_import():
-            new_pairs = []
-            for name in import_info.get_imported_names():
-                new_pairs.append((name, None))
-            new_import = importinfo.FromImport(
-                import_info.module_name, import_info.level, new_pairs,
-                import_info.current_folder, self.pycore)
-            import_stmt.import_info = self.filtering.visitFromImport(None, new_import)
-        else:
-            self.filtering.dispatch(import_stmt)
-
-
-class SelfImportVisitor(ImportInfoVisitor):
-    
-    def __init__(self, pycore, current_folder, resource):
-        self.pycore = pycore
-        self.current_folder = current_folder
-        self.resource = resource
-        self.to_be_fixed = set()
-        self.to_be_renamed = set()
-    
-    def visitNormalImport(self, import_stmt, import_info):
-        new_pairs = []
-        for name, alias in import_info.names_and_aliases:
-            resource = self.pycore.find_module(name, current_folder=self.current_folder)
-            if resource is not None and resource == self.resource:
-                imported = name
-                if alias is not None:
-                    imported = alias
-                self.to_be_fixed.add(imported)
-            else:
-                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.NormalImport(new_pairs)
-    
-    def visitFromImport(self, import_stmt, import_info):
-        if import_info.level == 0:
-            resource = self.pycore.find_module(import_info.module_name,
-                                               current_folder=self.current_folder)
-        else:
-            resource = self.pycore.find_relative_module(
-                import_info.module_name, self.current_folder, import_info.level)
-        if resource is None:
-            return
-        if resource == self.resource:
-            self._importing_names_from_self(import_info, import_stmt)
-            return
-        pymodule = self.pycore.resource_to_pyobject(resource)
-        new_pairs = []
-        for name, alias in import_info.names_and_aliases:
-            try:
-                result = pymodule.get_attribute(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.AttributeNotFoundException:
-                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,
-                import_info.current_folder, self.pycore)
-
-    def _importing_names_from_self(self, import_info, import_stmt):
-        if not import_info.is_star_import():
-            for name, alias in import_info.names_and_aliases:
-                if alias is not None:
-                    self.to_be_renamed.add((alias, name))
-        import_stmt.empty_import()

rope/refactor/inline.py

 import rope.base.exceptions
 import rope.base.pynames
 import rope.base.pyobjects
+import rope.refactor.functionutils
 from rope.base import codeanalyze
 from rope.refactor import sourceutils
+from rope.refactor import rename
+from rope.refactor import occurrences
 from rope.refactor.change import ChangeSet, ChangeContents
-import rope.refactor.functionutils
 
 
 class InlineRefactoring(object):
         definition = definition_with_assignment[definition_with_assignment.\
                                                 index('=') + 1:].strip()
 
-        changed_source = rope.refactor.rename.RenameInModule(
-            self.pycore, [self.pyname], self.name, definition,
-            replace_primary=True).get_changed_module(pymodule=self.pymodule)
+        occurrence_finder = occurrences.FilteredOccurrenceFinder(
+            self.pycore, self.name, [self.pyname])
+        changed_source = rename.rename_in_module(
+            occurrence_finder, definition, pymodule=self.pymodule, replace_primary=True)
         if changed_source is None:
             changed_source = self.pymodule.source_code
         lines = codeanalyze.SourceLinesAdapter(changed_source)
         scope = self.pyfunction.get_scope()
         source = self.pymodule.source_code
         lines = self.pymodule.lines
-        start_line = codeanalyze.LogicalLineFinder(lines).\
-                     get_logical_line_in(scope.get_start())[1] + 1
+        line_finder = codeanalyze.LogicalLineFinder(lines)
+        start_line = line_finder.get_logical_line_in(scope.get_start())[1] + 1
+        if self.pyfunction._get_ast().doc is not None:
+            start_line = line_finder.get_logical_line_in(start_line)[1] + 1
         start_offset = lines.get_line_start(start_line)
         end_offset = min(lines.get_line_end(scope.get_end()) + 1, len(source))
         body = source[start_offset:end_offset]

rope/refactor/introduce_factory.py

 import rope.base.codeanalyze
 import rope.base.exceptions
 import rope.base.pyobjects
-import rope.refactor.rename
 import rope.refactor.importutils
+from rope.refactor import rename
+from rope.refactor import occurrences
+from rope.refactor import sourceutils
 
 from rope.refactor.change import (ChangeSet, ChangeContents)
-from rope.refactor import sourceutils
 
 
 class IntroduceFactoryRefactoring(object):
 
     def _change_resource(self, changes, factory_name, global_factory):
         class_scope = self.old_pyname.get_object().get_scope()
-        rename_in_module = rope.refactor.rename.RenameInModule(
-            self.pycore, [self.old_pyname], self.old_name,
-            self._get_new_function_name(factory_name, global_factory), True)
-        source_code = rename_in_module.get_changed_module(pymodule=self.pymodule)
+        occurrence_finder = occurrences.FilteredOccurrenceFinder(
+            self.pycore, self.old_name, [self.old_pyname], only_calls=True)
+        source_code = rename.rename_in_module(
+            occurrence_finder, self._get_new_function_name(factory_name, global_factory),
+            pymodule=self.pymodule)
         if source_code is None:
             source_code = self.pymodule.source_code
         lines = self.pymodule.lines
         for file_ in self.pycore.get_python_files():
             if file_ == self.resource:
                 continue
-            rename_in_module = rope.refactor.rename.RenameInModule(
-                self.pycore, [self.old_pyname], self.old_name, changed_name,
-                only_calls=True, replace_primary=global_factory)
-            changed_code = rename_in_module.get_changed_module(resource=file_)
+            occurrence_finder = occurrences.FilteredOccurrenceFinder(
+                self.pycore, self.old_name, [self.old_pyname], only_calls=True)
+            changed_code = rename.rename_in_module(occurrence_finder, changed_name, resource=file_,
+                                                   replace_primary=global_factory)
             if changed_code is not None:
                 if global_factory:
                     new_pymodule = self.pycore.get_string_module(changed_code, self.resource)

rope/refactor/move.py

 import rope.base.pyobjects
 import rope.refactor.importutils
 import rope.base.exceptions
+from rope.refactor import rename
+from rope.refactor import occurrences
 from rope.refactor.change import (ChangeSet, ChangeContents,
                                   MoveResource, CreateFolder)
 
 
 class MoveRefactoring(object):
+    """A class for moving modules, packages, global functions and classes."""
     
-    def __init__(self, pycore, resource, offset):
+    def __init__(self, pycore, resource, offset=None):
         self.pycore = pycore
-        self.pyname = rope.base.codeanalyze.get_pyname_at(self.pycore, resource, offset)
-        if self.pyname is None:
-            raise rope.base.exceptions.RefactoringException(
-                'Move works on classes,functions or modules.')
+        if offset is not None:
+            self.pyname = rope.base.codeanalyze.get_pyname_at(self.pycore, resource, offset)
+            if self.pyname is None:
+                raise rope.base.exceptions.RefactoringException(
+                    'Move works on classes,functions or modules.')
+        else:
+            if not resource.is_folder() and resource.get_name() == '__init__.py':
+                resource = resource.get_parent()
+            dummy_pymodule = self.pycore.get_string_module('')
+            self.pyname = rope.base.pynames.ImportedModule(dummy_pymodule, resource=resource)
     
     def get_changes(self, dest_resource):
         moving_object = self.pyname.get_object()
         return pymodule, False
 
     def _rename_in_module(self, pymodule, new_name, imports=False):
-        rename_in_module = rope.refactor.rename.RenameInModule(
-            self.pycore, [self.old_pyname], self.old_name,
-            new_name, replace_primary=True, imports=imports)
-        source = rename_in_module.get_changed_module(pymodule=pymodule)
+        occurrence_finder = occurrences.FilteredOccurrenceFinder(
+            self.pycore, self.old_name, [self.old_pyname], imports=imports)
+        source = rename.rename_in_module(occurrence_finder, new_name,
+                                         pymodule=pymodule, replace_primary=True)
         if source is None:
             return pymodule, False
         else:

rope/refactor/rename.py

 import rope.base.pynames
 import rope.base.pyobjects
 import rope.base.exceptions
-import rope.refactor.occurrences
+from rope.refactor import occurrences
 from rope.refactor import sourceutils
 from rope.refactor.change import ChangeSet, ChangeContents, MoveResource
 
 
 class RenameRefactoring(object):
     
-    def __init__(self, pycore, resource, offset):
+    def __init__(self, pycore, resource, offset=None):
         self.pycore = pycore
         self.resource = resource
-        self.offset = offset
-        self.old_name = rope.base.codeanalyze.get_name_at(self.resource, self.offset)
-        self.old_pyname = rope.base.codeanalyze.get_pyname_at(self.pycore, resource,
-                                                         offset)
-        if self.old_pyname is None:
-            raise rope.base.exceptions.RefactoringException(
-                'Rename refactoring should be performed on python identifiers.')
+        if offset is not None:
+            self.old_name = rope.base.codeanalyze.get_name_at(self.resource, offset)
+            self.old_pyname = rope.base.codeanalyze.get_pyname_at(self.pycore, resource,
+                                                                  offset)
+            if self.old_pyname is None:
+                raise rope.base.exceptions.RefactoringException(
+                    'Rename refactoring should be performed on python identifiers.')
+        else:
+            if not resource.is_folder() and resource.get_name() == '__init__.py':
+                resource = resource.get_parent()
+            dummy_pymodule = self.pycore.get_string_module('')
+            self.old_pyname = rope.base.pynames.ImportedModule(dummy_pymodule, resource=resource)
+            if resource.is_folder():
+                self.old_name = resource.get_name()
+            else:
+                self.old_name = resource.get_name()[:-3]
     
     def get_old_name(self):
         return self.old_name
         files = self._get_interesting_files(in_file)
         changes = ChangeSet()
         for file_ in files:
-            new_content = RenameInModule(self.pycore, old_pynames, self.old_name, new_name).\
-                          get_changed_module(file_)
+            occurance_finder = occurrences.FilteredOccurrenceFinder(self.pycore, self.old_name, old_pynames)
+            new_content = rename_in_module(occurance_finder, new_name, resource=file_)
             if new_content is not None:
                 changes.add_change(ChangeContents(file_, new_content))
         
         return MoveResource(resource, new_location)
 
 
-class RenameInModule(object):
-    
-    def __init__(self, pycore, old_pynames, old_name, new_name,
-                 only_calls=False, replace_primary=False,
-                 imports=True):
-        self.occurrences_finder = rope.refactor.occurrences.FilteredOccurrenceFinder(
-            pycore, old_name, old_pynames, only_calls, imports)
-        self.whole_primary = replace_primary
-        self.new_name = new_name
-    
-    def get_changed_module(self, resource=None, pymodule=None):
-        source_code = self._get_source(resource, pymodule)
-        change_collector = sourceutils.ChangeCollector(source_code)
-        for occurrence in self.occurrences_finder.find_occurrences(resource, pymodule):
-            if self.whole_primary and occurrence.is_a_fixed_primary():
-                continue
-            if self.whole_primary:
-                start, end = occurrence.get_primary_range()
-            else:
-                start, end = occurrence.get_word_range()
-            change_collector.add_change(start, end, self.new_name)
-        return change_collector.get_changed()
-    
-    def _get_source(self, resource, pymodule):
-        if resource is not None:
-            return resource.read()
+def rename_in_module(occurrences_finder, new_name, resource=None,
+                     pymodule=None, replace_primary=False):
+    if resource is not None:
+        source_code = resource.read()
+    else:
+        source_code = pymodule.source_code
+    change_collector = sourceutils.ChangeCollector(source_code)
+    for occurrence in occurrences_finder.find_occurrences(resource, pymodule):
+        if replace_primary and occurrence.is_a_fixed_primary():
+            continue
+        if replace_primary:
+            start, end = occurrence.get_primary_range()
         else:
-            return pymodule.source_code
-
+            start, end = occurrence.get_word_range()
+        change_collector.add_change(start, end, new_name)
+    return change_collector.get_changed()

rope/ui/codeassist.py

                             ['python']))
 actions.append(SimpleAction('Run Module', do_run_module, 'M-X p',
                             MenuAddress(['Code', 'Run Module'], 'm', 2), ['python']))
-actions.append(SimpleAction('Run Test', run_tests, 'M-X t',
-                            MenuAddress(['Code', 'Run Test'], 't', 2), ['python']))
+actions.append(SimpleAction('Run Unit Tests', run_tests, 'M-X t',
+                            MenuAddress(['Code', 'Run Unit Tests'], 't', 2), ['python']))
 
 for action in actions:
     core.register_action(action)

rope/ui/editactions.py

     
 
 core = rope.ui.core.Core.get_core()
-core._add_menu_cascade(MenuAddress(['Edit'], 'e'), ['all'])
+core._add_menu_cascade(MenuAddress(['Edit'], 'e'), ['all', 'none'])
 actions = []
 
 actions.append(SimpleAction('Emacs Set Mark', set_mark, 'C-space',
 actions.append(SimpleAction('Goto Line', goto_line, None,
                             MenuAddress(['Edit', 'Goto Line'], 'g'), ['all']))
 actions.append(SimpleAction('Goto Last Edit Location', goto_last_edit_location, 'C-q',
-                            MenuAddress(['Edit', 'Goto Last Edit Location'], 'e'), ['all']))
+                            MenuAddress(['Edit', 'Goto Last Edit Location'], 'e'), ['all', 'none']))
 actions.append(SimpleAction('Undo', undo, 'C-x u',
                             MenuAddress(['Edit', 'Undo'], 'u', 1), ['all']))
 actions.append(SimpleAction('Redo', redo, 'C-x r',

rope/ui/refactor.py

 
 class RenameDialog(RefactoringDialog):
     
-    def __init__(self, context, title, is_local=False):
+    def __init__(self, context, title, is_local=False, current_module=False):
         resource = context.get_active_editor().get_file()
         editor = context.get_active_editor().get_editor()
         super(RenameDialog, self).__init__(_get_refactoring(context), title)
         self.is_local = is_local
+        offset = editor.get_current_offset()
+        if current_module:
+            offset = None
         self.renamer = rope.refactor.rename.RenameRefactoring(
             context.get_core().get_open_project().get_pycore(), 
-            resource, editor.get_current_offset())
+            resource, offset)
     
     def _get_changes(self):
         new_name = self.new_name_entry.get()
 def rename(context):
     RenameDialog(context, 'Rename Refactoring').show()
 
+def rename_module(context):
+    RenameDialog(context, 'Rename Current Module Refactoring', current_module=True).show()
+
 def local_rename(context):
     RenameDialog(context, 'Rename Refactoring', True).show()
 
 
 class MoveDialog(RefactoringDialog):
     
-    def __init__(self, context):
+    def __init__(self, context, current_module=False):
         resource = context.get_active_editor().get_file()
         editor = context.get_active_editor().get_editor()
         self.project = context.get_core().get_open_project()
         super(MoveDialog, self).__init__(_get_refactoring(context),
                                          'Move Refactoring')
+        offset = editor.get_current_offset()
+        if current_module:
+            offset = None
         self.mover = rope.refactor.move.MoveRefactoring(
             context.get_core().get_open_project().get_pycore(), 
-            resource, editor.get_current_offset())
+            resource, offset)
     
     def _get_changes(self):
         destination = self.project.get_pycore().find_module(self.new_name_entry.get())
 def move(context):
     MoveDialog(context).show()
 
+def move_module(context):
+    MoveDialog(context, current_module=True).show()
+
 class _Parameter(object):
     
     def __init__(self, name):
                             ConfirmAllEditorsAreSaved(transform_module_to_package), None,
                             MenuAddress(['Refactor', 'Transform Module to Package'], 't', 1), 
                             ['python']))
+actions.append(SimpleAction('Rename Current Module', 
+                            ConfirmAllEditorsAreSaved(rename_module), None,
+                            MenuAddress(['Refactor', 'Rename Current Module'], None, 1), 
+                            ['python']))
+actions.append(SimpleAction('Move Current Module', 
+                            ConfirmAllEditorsAreSaved(move_module), None,
+                            MenuAddress(['Refactor', 'Move Current Module'], None, 1), 
+                            ['python']))
 actions.append(SimpleAction('Organize Imports', 
                             ConfirmAllEditorsAreSaved(organize_imports), 'C-O',
                             MenuAddress(['Refactor', 'Organize Imports'], 'o', 2), ['python']))

ropetest/__init__.py

 import ropetest.formattertest
 import ropetest.objectinfertest
 import ropetest.runmodtest
-import ropetest.importutilstest
 
 
 def suite():
     result.addTests(unittest.makeSuite(ropetest.formattertest.FormatterTest))
     result.addTests(ropetest.objectinfertest.suite())
     result.addTests(ropetest.runmodtest.suite())
-    result.addTests(unittest.makeSuite(ropetest.importutilstest.ImportUtilsTest))
     return result
 
 

ropetest/importutilstest.py

-import unittest
-
-from rope.base.project import Project
-from ropetest import testutils
-from rope.refactor.importutils import ImportTools
-
-
-class ImportUtilsTest(unittest.TestCase):
-
-    def setUp(self):
-        super(ImportUtilsTest, self).setUp()
-        self.project_root = 'sample_project'
-        testutils.remove_recursively(self.project_root)
-        self.project = Project(self.project_root)
-        self.pycore = self.project.get_pycore()
-        self.import_tools = ImportTools(self.pycore)
-        
-        self.mod = self.pycore.create_module(self.project.get_root_folder(), 'mod')
-        self.pkg1 = self.pycore.create_package(self.project.get_root_folder(), 'pkg1')
-        self.mod1 = self.pycore.create_module(self.pkg1, 'mod1')
-        self.pkg2 = self.pycore.create_package(self.project.get_root_folder(), 'pkg2')
-        self.mod2 = self.pycore.create_module(self.pkg2, 'mod2')
-        self.mod3 = self.pycore.create_module(self.pkg2, 'mod3')
-
-    def tearDown(self):
-        testutils.remove_recursively(self.project_root)
-        super(ImportUtilsTest, self).tearDown()
-
-    def test_get_import_for_module(self):
-        pymod = self.pycore.get_module('mod')
-        import_statement = self.import_tools.get_import_for_module(pymod)
-        self.assertEquals('import mod', import_statement.get_import_statement())
-
-    def test_get_import_for_module_in_nested_modules(self):
-        pymod = self.pycore.get_module('pkg1.mod1')
-        import_statement = self.import_tools.get_import_for_module(pymod)
-        self.assertEquals('import pkg1.mod1', import_statement.get_import_statement())
-
-    def test_get_import_for_module_in_init_dot_py(self):
-        init_dot_py = self.pkg1.get_child('__init__.py')
-        pymod = self.pycore.resource_to_pyobject(init_dot_py)
-        import_statement = self.import_tools.get_import_for_module(pymod)
-        self.assertEquals('import pkg1', import_statement.get_import_statement())
-    
-    
-    def test_get_from_import_for_module(self):
-        pymod = self.pycore.get_module('mod')
-        import_statement = self.import_tools.get_from_import_for_module(pymod, 'a_func')
-        self.assertEquals('from mod import a_func', 
-                          import_statement.get_import_statement())
-
-    def test_get_from_import_for_module_in_nested_modules(self):
-        pymod = self.pycore.get_module('pkg1.mod1')
-        import_statement = self.import_tools.get_from_import_for_module(pymod, 'a_func')
-        self.assertEquals('from pkg1.mod1 import a_func',
-                          import_statement.get_import_statement())
-
-    def test_get_from_import_for_module_in_init_dot_py(self):
-        init_dot_py = self.pkg1.get_child('__init__.py')
-        pymod = self.pycore.resource_to_pyobject(init_dot_py)
-        import_statement = self.import_tools.get_from_import_for_module(pymod, 'a_func')
-        self.assertEquals('from pkg1 import a_func',
-                          import_statement.get_import_statement())
-    
-    
-    def test_get_import_statements(self):
-        self.mod.write('import pkg1\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        imports = module_with_imports.get_import_statements()
-        self.assertEquals('import pkg1',
-                          imports[0].import_info.get_import_statement())
-
-    def test_get_import_statements_with_alias(self):
-        self.mod.write('import pkg1.mod1 as mod1\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        imports = module_with_imports.get_import_statements()
-        self.assertEquals('import pkg1.mod1 as mod1',
-                          imports[0].import_info.get_import_statement())
-
-    def test_get_import_statements_for_froms(self):
-        self.mod.write('from pkg1 import mod1\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        imports = module_with_imports.get_import_statements()
-        self.assertEquals('from pkg1 import mod1',
-                          imports[0].import_info.get_import_statement())
-
-    def test_get_multi_line_import_statements_for_froms(self):
-        self.mod.write('from pkg1 \\\n    import mod1\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        imports = module_with_imports.get_import_statements()
-        self.assertEquals('from pkg1 import mod1',
-                          imports[0].import_info.get_import_statement())
-
-    def test_get_import_statements_for_from_star(self):
-        self.mod.write('from pkg1 import *\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        imports = module_with_imports.get_import_statements()
-        self.assertEquals('from pkg1 import *',
-                          imports[0].import_info.get_import_statement())
-
-    @testutils.run_only_for_25
-    def test_get_import_statements_for_new_relatives(self):
-        self.mod2.write('from .mod3 import *\n')
-        pymod = self.pycore.get_module('pkg2.mod2')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        imports = module_with_imports.get_import_statements()
-        self.assertEquals('from .mod3 import *',
-                          imports[0].import_info.get_import_statement())
-
-    def test_ignoring_indented_imports(self):
-        self.mod.write('if True:\n    import pkg1\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        imports = module_with_imports.get_import_statements()
-        self.assertEquals(0, len(imports))
-
-    def test_import_get_names(self):
-        self.mod.write('import pkg1 as pkg\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        imports = module_with_imports.get_import_statements()
-        self.assertEquals(['pkg'], imports[0].import_info.get_imported_names())
-
-    def test_import_get_names_with_alias(self):
-        self.mod.write('import pkg1.mod1\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        imports = module_with_imports.get_import_statements()
-        self.assertEquals(['pkg1'], imports[0].import_info.get_imported_names())
-
-    def test_import_get_names_with_alias(self):
-        self.mod1.write('def a_func():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import *\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        imports = module_with_imports.get_import_statements()
-        self.assertEquals(['a_func'], imports[0].import_info.get_imported_names())
-    
-    def test_empty_getting_used_imports(self):
-        self.mod.write('')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        imports = module_with_imports.get_used_imports(pymod)
-        self.assertEquals(0, len(imports))
-    
-    def test_empty_getting_used_imports2(self):
-        self.mod.write('import pkg\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        imports = module_with_imports.get_used_imports(pymod)
-        self.assertEquals(0, len(imports))
-    
-    def test_simple_getting_used_imports(self):
-        self.mod.write('import pkg\nprint pkg\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        imports = module_with_imports.get_used_imports(pymod)
-        self.assertEquals(1, len(imports))
-        self.assertEquals('import pkg', imports[0].get_import_statement())
-
-    def test_simple_getting_used_imports2(self):
-        self.mod.write('import pkg\ndef a_func():\n    print pkg\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        imports = module_with_imports.get_used_imports(pymod)
-        self.assertEquals(1, len(imports))
-        self.assertEquals('import pkg', imports[0].get_import_statement())
-
-    def test_getting_used_imports_for_nested_scopes(self):
-        self.mod.write('import pkg1\nprint pkg1\ndef a_func():\n    pass\nprint pkg1\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        imports = module_with_imports.get_used_imports(
-            pymod.get_attribute('a_func').get_object())
-        self.assertEquals(0, len(imports))
-
-    def test_getting_used_imports_for_nested_scopes2(self):
-        self.mod.write('from pkg1 import mod1\ndef a_func():\n    print mod1\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        imports = module_with_imports.get_used_imports(
-            pymod.get_attribute('a_func').get_object())
-        self.assertEquals(1, len(imports))
-        self.assertEquals('from pkg1 import mod1', imports[0].get_import_statement())
-
-    def test_empty_removing_unused_imports(self):
-        self.mod.write('import pkg1\nprint pkg1\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_unused_imports()
-        self.assertEquals('import pkg1\nprint pkg1\n',
-                          module_with_imports.get_changed_source())
-        
-    def test_simple_removing_unused_imports(self):
-        self.mod.write('import pkg1\n\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_unused_imports()
-        self.assertEquals('\n', module_with_imports.get_changed_source())
-        
-    def test_simple_removing_unused_imports_for_froms(self):
-        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import a_func, another_func\n\na_func()\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_unused_imports()
-        self.assertEquals('from pkg1.mod1 import a_func\n\na_func()\n',
-                          module_with_imports.get_changed_source())
-        
-    def test_simple_removing_unused_imports_for_from_stars(self):
-        self.mod.write('from pkg1.mod1 import *\n\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_unused_imports()
-        self.assertEquals('\n', module_with_imports.get_changed_source())
-    
-    def test_simple_removing_unused_imports_for_nested_modules(self):
-        self.mod1.write('def a_func():\n    pass\n')
-        self.mod.write('import pkg1.mod1\npkg1.mod1.a_func()')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_unused_imports()
-        self.assertEquals('import pkg1.mod1\npkg1.mod1.a_func()',
-                          module_with_imports.get_changed_source())
-    
-    def test_removing_unused_imports_and_functions_of_the_same_name(self):
-        self.mod.write('def a_func():\n    pass\ndef a_func():\n    pass\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_unused_imports()
-        self.assertEquals('def a_func():\n    pass\ndef a_func():\n    pass\n',
-                          module_with_imports.get_changed_source())
-        
-    def test_removing_unused_imports_for_from_import_with_as(self):
-        self.mod.write('a_var = 1\n')
-        self.mod1.write('from mod import a_var as myvar\na_var = myvar\n')
-        pymod = self.pycore.resource_to_pyobject(self.mod1)
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_unused_imports()
-        self.assertEquals('from mod import a_var as myvar\na_var = myvar\n',
-                          module_with_imports.get_changed_source())
-
-    def test_adding_imports(self):
-        self.mod.write('\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        new_import = self.import_tools.get_import_for_module(
-            self.pycore.resource_to_pyobject(self.mod1))
-        module_with_imports.add_import(new_import)
-        self.assertEquals('import pkg1.mod1\n\n', module_with_imports.get_changed_source())
-
-    def test_adding_from_imports(self):
-        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import a_func\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        new_import = self.import_tools.get_from_import_for_module(
-            self.pycore.resource_to_pyobject(self.mod1), 'another_func')
-        module_with_imports.add_import(new_import)
-        self.assertEquals('from pkg1.mod1 import a_func, another_func\n',
-                          module_with_imports.get_changed_source())
-
-    def test_adding_to_star_imports(self):
-        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import *\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        new_import = self.import_tools.get_from_import_for_module(
-            self.pycore.resource_to_pyobject(self.mod1), 'another_func')
-        module_with_imports.add_import(new_import)
-        self.assertEquals('from pkg1.mod1 import *\n',
-                          module_with_imports.get_changed_source())
-
-    def test_adding_star_imports(self):
-        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import a_func\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        new_import = self.import_tools.get_from_import_for_module(
-            self.pycore.resource_to_pyobject(self.mod1), '*')
-        module_with_imports.add_import(new_import)
-        self.assertEquals('from pkg1.mod1 import *\n',
-                          module_with_imports.get_changed_source())
-    
-    def test_not_changing_the_format_of_unchanged_imports(self):
-        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import (a_func,\n    another_func)\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        self.assertEquals('from pkg1.mod1 import (a_func,\n    another_func)\n',
-                          module_with_imports.get_changed_source())
-
-    def test_not_changing_the_format_of_unchanged_imports2(self):
-        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import (a_func)\na_func()\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_unused_imports()
-        self.assertEquals('from pkg1.mod1 import (a_func)\na_func()\n',
-                          module_with_imports.get_changed_source())
-
-    def test_removing_unused_imports_and_reoccuring_names(self):
-        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import *\nfrom pkg1.mod1 import a_func\na_func()\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_unused_imports()
-        self.assertEquals('from pkg1.mod1 import *\na_func()\n',
-                          module_with_imports.get_changed_source())
-
-    def test_removing_unused_imports_and_reoccuring_names2(self):
-        self.mod.write('import pkg2.mod2\nimport pkg2.mod3\nprint pkg2.mod2, pkg2.mod3')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_unused_imports()
-        self.assertEquals('import pkg2.mod2\nimport pkg2.mod3\nprint pkg2.mod2, pkg2.mod3',
-                          module_with_imports.get_changed_source())
-
-    def test_removing_unused_imports_and_common_packages(self):
-        self.mod.write('import pkg1.mod1\nimport pkg1\nprint pkg1, pkg1.mod1\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_unused_imports()
-        self.assertEquals('import pkg1.mod1\nprint pkg1, pkg1.mod1\n',
-                          module_with_imports.get_changed_source())
-
-    def test_removing_unused_imports_and_common_packages_reversed(self):
-        self.mod.write('import pkg1\nimport pkg1.mod1\nprint pkg1, pkg1.mod1\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_duplicates()
-        self.assertEquals('import pkg1.mod1\nprint pkg1, pkg1.mod1\n',
-                          module_with_imports.get_changed_source())
-
-    def test_removing_unused_imports_and_common_packages2(self):
-        self.mod.write('import pkg1.mod1\nimport pkg1.mod2\nprint pkg1\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_unused_imports()
-        self.assertEquals('import pkg1.mod1\nprint pkg1\n',
-                          module_with_imports.get_changed_source())
-
-    def test_removing_unused_imports_and_froms(self):
-        self.mod1.write('def func1():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import func1\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_unused_imports()
-        self.assertEquals('', module_with_imports.get_changed_source())
-
-    def test_removing_unused_imports_and_froms2(self):
-        self.mod1.write('def func1():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import func1\nfunc1()')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_unused_imports()
-        self.assertEquals('from pkg1.mod1 import func1\nfunc1()',
-                          module_with_imports.get_changed_source())
-
-    def test_removing_unused_imports_and_froms3(self):
-        self.mod1.write('def func1():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import func1\ndef a_func():\n    func1()\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_unused_imports()
-        self.assertEquals('from pkg1.mod1 import func1\ndef a_func():\n    func1()\n',
-                          module_with_imports.get_changed_source())
-
-    def test_removing_unused_imports_and_froms4(self):
-        self.mod1.write('def func1():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import func1\nclass A(object):\n'
-                       '    def a_func(self):\n        func1()\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_unused_imports()
-        self.assertEquals('from pkg1.mod1 import func1\nclass A(object):\n'
-                          '    def a_func(self):\n        func1()\n',
-                          module_with_imports.get_changed_source())
-
-    def test_removing_unused_imports_and_getting_attributes(self):
-        self.mod1.write('class A(object):\n    def f(self):\n        pass\n')
-        self.mod.write('from pkg1.mod1 import A\nvar = A().f()')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_unused_imports()
-        self.assertEquals('from pkg1.mod1 import A\nvar = A().f()',
-                          module_with_imports.get_changed_source())
-
-    def test_removing_unused_imports_function_parameters(self):
-        self.mod1.write('def func1():\n    pass\n')
-        self.mod.write('import pkg1\ndef a_func(pkg1):\n    my_var = pkg1\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_unused_imports()
-        self.assertEquals('def a_func(pkg1):\n    my_var = pkg1\n',
-                          module_with_imports.get_changed_source())
-
-    def test_trivial_expanding_star_imports(self):
-        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import *\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.expand_stars()
-        self.assertEquals('', module_with_imports.get_changed_source())
-
-    def test_expanding_star_imports(self):
-        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import *\na_func()\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.expand_stars()
-        self.assertEquals('from pkg1.mod1 import a_func\na_func()\n',
-                          module_with_imports.get_changed_source())
-
-    def test_removing_duplicate_imports(self):
-        self.mod.write('import pkg1\nimport pkg1\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_duplicates()
-        self.assertEquals('import pkg1\n',
-                          module_with_imports.get_changed_source())
-
-    def test_removing_duplicates_and_reoccuring_names(self):
-        self.mod.write('import pkg2.mod2\nimport pkg2.mod3\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_duplicates()
-        self.assertEquals('import pkg2.mod2\nimport pkg2.mod3\n',
-                          module_with_imports.get_changed_source())
-
-    def test_removing_duplicate_imports_for_froms(self):
-        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
-        self.mod.write('from pkg1 import a_func\nfrom pkg1 import a_func, another_func\n')
-        pymod = self.pycore.get_module('mod')
-        module_with_imports = self.import_tools.get_module_with_imports(pymod)
-        module_with_imports.remove_duplicates()
-        self.assertEquals('from pkg1 import a_func, another_func\n',
-                          module_with_imports.get_changed_source())
-    
-    def test_transforming_froms_to_normal_changing_imports(self):
-        self.mod1.write('def a_func():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import a_func\nprint a_func\n')
-        pymod = self.pycore.get_module('mod')
-        changed_module = self.import_tools.transform_froms_to_normal_imports(pymod)
-        self.assertEquals('import pkg1.mod1\nprint pkg1.mod1.a_func\n', changed_module)
-
-    def test_transforming_froms_to_normal_changing_occurances(self):
-        self.mod1.write('def a_func():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import a_func\na_func()')
-        pymod = self.pycore.get_module('mod')
-        changed_module = self.import_tools.transform_froms_to_normal_imports(pymod)
-        self.assertEquals('import pkg1.mod1\npkg1.mod1.a_func()', changed_module)
-
-    def test_transforming_froms_to_normal_for_multi_imports(self):
-        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import *\na_func()\nanother_func()\n')
-        pymod = self.pycore.get_module('mod')
-        changed_module = self.import_tools.transform_froms_to_normal_imports(pymod)
-        self.assertEquals('import pkg1.mod1\npkg1.mod1.a_func()\npkg1.mod1.another_func()\n',
-                          changed_module)
-
-    def test_transforming_froms_to_normal_for_multi_imports_inside_parens(self):
-        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import (a_func, \n    another_func)' \
-                       '\na_func()\nanother_func()\n')
-        pymod = self.pycore.get_module('mod')
-        changed_module = self.import_tools.transform_froms_to_normal_imports(pymod)
-        self.assertEquals('import pkg1.mod1\npkg1.mod1.a_func()\npkg1.mod1.another_func()\n',
-                          changed_module)
-
-    def test_transforming_froms_to_normal_from_stars(self):
-        self.mod1.write('def a_func():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import *\na_func()\n')
-        pymod = self.pycore.get_module('mod')
-        changed_module = self.import_tools.transform_froms_to_normal_imports(pymod)
-        self.assertEquals('import pkg1.mod1\npkg1.mod1.a_func()\n', changed_module)
-
-    def test_transforming_froms_to_normal_from_stars(self):
-        self.mod1.write('a_var = 10')
-        self.mod.write('import pkg1.mod1\nfrom pkg1.mod1 import a_var\n' \
-                       'def a_func():\n    print pkg1.mod1, a_var\n')
-        pymod = self.pycore.get_module('mod')
-        changed_module = self.import_tools.transform_froms_to_normal_imports(pymod)
-        self.assertEquals('import pkg1.mod1\n' \
-                          'def a_func():\n    print pkg1.mod1, pkg1.mod1.a_var\n',
-                          changed_module)
-
-    def test_transforming_froms_to_normal_from_with_alias(self):
-        self.mod1.write('def a_func():\n    pass\n')
-        self.mod.write('from pkg1.mod1 import a_func as another_func\nanother_func()\n')
-        pymod = self.pycore.get_module('mod')
-        changed_module = self.import_tools.transform_froms_to_normal_imports(pymod)
-        self.assertEquals('import pkg1.mod1\npkg1.mod1.a_func()\n', changed_module)
-    
-    def test_transforming_froms_to_normal_for_relatives(self):
-        self.mod2.write('def a_func():\n    pass\n')
-        self.mod3.write('from mod2 import *\na_func()\n')
-        pymod = self.pycore.resource_to_pyobject(self.mod3)
-        changed_module = self.import_tools.transform_froms_to_normal_imports(pymod)
-        self.assertEquals('import pkg2.mod2\npkg2.mod2.a_func()\n', changed_module)
-
-    def test_transform_relatives_imports_to_absolute_imports_doing_nothing(self):
-        self.mod2.write('from pkg1 import mod1\nimport mod1\n')
-        pymod = self.pycore.resource_to_pyobject(self.mod2)
-        self.assertEquals('from pkg1 import mod1\nimport mod1\n',
-                          self.import_tools.transform_relative_imports_to_absolute(pymod))
-        
-    def test_transform_relatives_imports_to_absolute_imports_for_normal_imports(self):
-        self.mod2.write('import mod3\n')
-        pymod = self.pycore.resource_to_pyobject(self.mod2)
-        self.assertEquals('import pkg2.mod3\n',
-                          self.import_tools.transform_relative_imports_to_absolute(pymod))
-        
-    def test_transform_relatives_imports_to_absolute_imports_for_froms(self):
-        self.mod3.write('def a_func():\n    pass\n')
-        self.mod2.write('from mod3 import a_func\n')
-        pymod = self.pycore.resource_to_pyobject(self.mod2)
-        self.assertEquals('from pkg2.mod3 import a_func\n',
-                          self.import_tools.transform_relative_imports_to_absolute(pymod))
-    
-    @testutils.run_only_for_25
-    def test_transform_relatives_imports_to_absolute_imports_for_new_relatives(self):
-        self.mod3.write('def a_func():\n    pass\n')
-        self.mod2.write('from .mod3 import a_func\n')
-        pymod = self.pycore.resource_to_pyobject(self.mod2)
-        self.assertEquals('from pkg2.mod3 import a_func\n',
-                          self.import_tools.transform_relative_imports_to_absolute(pymod))
-    
-    def test_transform_relatives_imports_to_absolute_imports_for_normal_imports2(self):
-        self.mod2.write('import mod3\nprint mod3')
-        pymod = self.pycore.resource_to_pyobject(self.mod2)
-        self.assertEquals('import pkg2.mod3\nprint pkg2.mod3',
-                          self.import_tools.transform_relative_imports_to_absolute(pymod))
-
-    def test_transform_relatives_imports_to_absolute_imports_for_aliases(self):
-        self.mod2.write('import mod3 as mod3\nprint mod3')
-        pymod = self.pycore.resource_to_pyobject(self.mod2)
-        self.assertEquals('import pkg2.mod3 as mod3\nprint mod3',
-                          self.import_tools.transform_relative_imports_to_absolute(pymod))
-    
-    def test_organizing_imports(self):
-        self.mod1.write('import mod1\n')
-        pymod = self.pycore.resource_to_pyobject(self.mod1)
-        self.assertEquals('', self.import_tools.organize_imports(pymod))
-
-    def test_removing_self_imports(self):
-        self.mod.write('import mod\nmod.a_var = 1\nprint mod.a_var\n')
-        pymod = self.pycore.resource_to_pyobject(self.mod)
-        self.assertEquals('a_var = 1\nprint a_var\n',
-                          self.import_tools.organize_imports(pymod))
-
-    def test_removing_self_imports2(self):
-        self.mod1.write('import pkg1.mod1\npkg1.mod1.a_var = 1\nprint pkg1.mod1.a_var\n')
-        pymod = self.pycore.resource_to_pyobject(self.mod1)
-        self.assertEquals('a_var = 1\nprint a_var\n',
-                          self.import_tools.organize_imports(pymod))
-
-    def test_removing_self_imports_with_as(self):
-        self.mod.write('import mod as mymod\nmymod.a_var = 1\nprint mymod.a_var\n')
-        pymod = self.pycore.resource_to_pyobject(self.mod)
-        self.assertEquals('a_var = 1\nprint a_var\n',
-                          self.import_tools.organize_imports(pymod))
-
-    def test_removing_self_imports_for_froms(self):
-        self.mod1.write('from pkg1 import mod1\nmod1.a_var = 1\nprint mod1.a_var\n')
-        pymod = self.pycore.resource_to_pyobject(self.mod1)
-        self.assertEquals('a_var = 1\nprint a_var\n',
-                          self.import_tools.organize_imports(pymod))
-
-    def test_removing_self_imports_for_froms_with_as(self):
-        self.mod1.write('from pkg1 import mod1 as mymod\nmymod.a_var = 1\nprint mymod.a_var\n')
-        pymod = self.pycore.resource_to_pyobject(self.mod1)
-        self.assertEquals('a_var = 1\nprint a_var\n',
-                          self.import_tools.organize_imports(pymod))
-
-    def test_removing_self_imports_for_froms2(self):
-        self.mod.write('from mod import a_var\na_var = 1\nprint a_var\n')
-        pymod = self.pycore.resource_to_pyobject(self.mod)
-        self.assertEquals('a_var = 1\nprint a_var\n',
-                          self.import_tools.organize_imports(pymod))
-
-    def test_removing_self_imports_for_froms3(self):
-        self.mod.write('from mod import a_var\na_var = 1\nprint a_var\n')
-        pymod = self.pycore.resource_to_pyobject(self.mod)
-        self.assertEquals('a_var = 1\nprint a_var\n',
-                          self.import_tools.organize_imports(pymod))
-
-    def test_removing_self_imports_for_froms3(self):
-        self.mod.write('from mod import a_var as myvar\na_var = 1\nprint myvar\n')
-        pymod = self.pycore.resource_to_pyobject(self.mod)
-        self.assertEquals('a_var = 1\nprint a_var\n',
-                          self.import_tools.organize_imports(pymod))
-
-    def test_removing_self_imports_with_no_dot_after_mod(self):
-        self.mod.write('import mod\nprint mod\n')
-        pymod = self.pycore.resource_to_pyobject(self.mod)
-        self.assertEquals('import mod\nprint mod\n',
-                          self.import_tools.organize_imports(pymod))
-
-    def test_removing_self_imports_with_no_dot_after_mod2(self):
-        self.mod.write('import mod\na_var = 1\nprint mod\\\n     \\\n     .var\n\n')
-        pymod = self.pycore.resource_to_pyobject(self.mod)
-        self.assertEquals('a_var = 1\nprint var\n\n',
-                          self.import_tools.organize_imports(pymod))
-
-    # XXX: causes stack overflow
-    def xxx_test_removing_self_imports_for_from_import_star(self):
-        self.mod.write('from mod import *\na_var = 1\nprint myvar\n')
-        pymod = self.pycore.resource_to_pyobject(self.mod)
-        self.assertEquals('a_var = 1\nprint a_var\n',
-                          self.import_tools.organize_imports(pymod))
-
-
-if __name__ == '__main__':
-    unittest.main()

ropetest/refactor/__init__.py

 import ropetest.refactor.inlinetest
 import ropetest.refactor.change_signature_test
 import rope.refactor.introduce_parameter
+import ropetest.refactor.importutilstest
 from rope.refactor import Undo
 from rope.base.exceptions import RefactoringException
 from rope.base.project import Project
     result.addTests(unittest.makeSuite(LocalToFieldTest))
     result.addTests(unittest.makeSuite(ropetest.refactor.change_signature_test.ChangeSignatureTest))
     result.addTests(unittest.makeSuite(IntroduceParameterTest))
+    result.addTests(unittest.makeSuite(ropetest.refactor.importutilstest.ImportUtilsTest))
     return result
 
 

ropetest/refactor/importutilstest.py

+import unittest
+
+from rope.base.project import Project
+from ropetest import testutils
+from rope.refactor.importutils import ImportTools
+
+
+class ImportUtilsTest(unittest.TestCase):
+
+    def setUp(self):
+        super(ImportUtilsTest, self).setUp()
+        self.project_root = 'sample_project'
+        testutils.remove_recursively(self.project_root)
+        self.project = Project(self.project_root)
+        self.pycore = self.project.get_pycore()
+        self.import_tools = ImportTools(self.pycore)
+        
+        self.mod = self.pycore.create_module(self.project.get_root_folder(), 'mod')
+        self.pkg1 = self.pycore.create_package(self.project.get_root_folder(), 'pkg1')
+        self.mod1 = self.pycore.create_module(self.pkg1, 'mod1')
+        self.pkg2 = self.pycore.create_package(self.project.get_root_folder(), 'pkg2')
+        self.mod2 = self.pycore.create_module(self.pkg2, 'mod2')
+        self.mod3 = self.pycore.create_module(self.pkg2, 'mod3')
+
+    def tearDown(self):
+        testutils.remove_recursively(self.project_root)
+        super(ImportUtilsTest, self).tearDown()
+
+    def test_get_import_for_module(self):
+        pymod = self.pycore.get_module('mod')
+        import_statement = self.import_tools.get_import_for_module(pymod)
+        self.assertEquals('import mod', import_statement.get_import_statement())
+
+    def test_get_import_for_module_in_nested_modules(self):
+        pymod = self.pycore.get_module('pkg1.mod1')
+        import_statement = self.import_tools.get_import_for_module(pymod)
+        self.assertEquals('import pkg1.mod1', import_statement.get_import_statement())
+
+    def test_get_import_for_module_in_init_dot_py(self):
+        init_dot_py = self.pkg1.get_child('__init__.py')
+        pymod = self.pycore.resource_to_pyobject(init_dot_py)
+        import_statement = self.import_tools.get_import_for_module(pymod)
+        self.assertEquals('import pkg1', import_statement.get_import_statement())
+    
+    
+    def test_get_from_import_for_module(self):
+        pymod = self.pycore.get_module('mod')
+        import_statement = self.import_tools.get_from_import_for_module(pymod, 'a_func')
+        self.assertEquals('from mod import a_func', 
+                          import_statement.get_import_statement())
+
+    def test_get_from_import_for_module_in_nested_modules(self):
+        pymod = self.pycore.get_module('pkg1.mod1')
+        import_statement = self.import_tools.get_from_import_for_module(pymod, 'a_func')
+        self.assertEquals('from pkg1.mod1 import a_func',
+                          import_statement.get_import_statement())
+
+    def test_get_from_import_for_module_in_init_dot_py(self):
+        init_dot_py = self.pkg1.get_child('__init__.py')
+        pymod = self.pycore.resource_to_pyobject(init_dot_py)
+        import_statement = self.import_tools.get_from_import_for_module(pymod, 'a_func')
+        self.assertEquals('from pkg1 import a_func',
+                          import_statement.get_import_statement())
+    
+    
+    def test_get_import_statements(self):
+        self.mod.write('import pkg1\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        imports = module_with_imports.get_import_statements()
+        self.assertEquals('import pkg1',
+                          imports[0].import_info.get_import_statement())
+
+    def test_get_import_statements_with_alias(self):
+        self.mod.write('import pkg1.mod1 as mod1\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        imports = module_with_imports.get_import_statements()
+        self.assertEquals('import pkg1.mod1 as mod1',
+                          imports[0].import_info.get_import_statement())
+
+    def test_get_import_statements_for_froms(self):
+        self.mod.write('from pkg1 import mod1\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        imports = module_with_imports.get_import_statements()
+        self.assertEquals('from pkg1 import mod1',
+                          imports[0].import_info.get_import_statement())
+
+    def test_get_multi_line_import_statements_for_froms(self):
+        self.mod.write('from pkg1 \\\n    import mod1\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        imports = module_with_imports.get_import_statements()
+        self.assertEquals('from pkg1 import mod1',
+                          imports[0].import_info.get_import_statement())
+
+    def test_get_import_statements_for_from_star(self):
+        self.mod.write('from pkg1 import *\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        imports = module_with_imports.get_import_statements()
+        self.assertEquals('from pkg1 import *',
+                          imports[0].import_info.get_import_statement())
+
+    @testutils.run_only_for_25
+    def test_get_import_statements_for_new_relatives(self):
+        self.mod2.write('from .mod3 import *\n')
+        pymod = self.pycore.get_module('pkg2.mod2')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        imports = module_with_imports.get_import_statements()
+        self.assertEquals('from .mod3 import *',
+                          imports[0].import_info.get_import_statement())
+
+    def test_ignoring_indented_imports(self):
+        self.mod.write('if True:\n    import pkg1\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        imports = module_with_imports.get_import_statements()
+        self.assertEquals(0, len(imports))
+
+    def test_import_get_names(self):
+        self.mod.write('import pkg1 as pkg\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        imports = module_with_imports.get_import_statements()
+        self.assertEquals(['pkg'], imports[0].import_info.get_imported_names())
+
+    def test_import_get_names_with_alias(self):
+        self.mod.write('import pkg1.mod1\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        imports = module_with_imports.get_import_statements()
+        self.assertEquals(['pkg1'], imports[0].import_info.get_imported_names())
+
+    def test_import_get_names_with_alias(self):
+        self.mod1.write('def a_func():\n    pass\n')
+        self.mod.write('from pkg1.mod1 import *\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        imports = module_with_imports.get_import_statements()
+        self.assertEquals(['a_func'], imports[0].import_info.get_imported_names())
+    
+    def test_empty_getting_used_imports(self):
+        self.mod.write('')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        imports = module_with_imports.get_used_imports(pymod)
+        self.assertEquals(0, len(imports))
+    
+    def test_empty_getting_used_imports2(self):
+        self.mod.write('import pkg\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        imports = module_with_imports.get_used_imports(pymod)
+        self.assertEquals(0, len(imports))
+    
+    def test_simple_getting_used_imports(self):
+        self.mod.write('import pkg\nprint pkg\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        imports = module_with_imports.get_used_imports(pymod)
+        self.assertEquals(1, len(imports))
+        self.assertEquals('import pkg', imports[0].get_import_statement())
+
+    def test_simple_getting_used_imports2(self):
+        self.mod.write('import pkg\ndef a_func():\n    print pkg\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        imports = module_with_imports.get_used_imports(pymod)
+        self.assertEquals(1, len(imports))
+        self.assertEquals('import pkg', imports[0].get_import_statement())
+
+    def test_getting_used_imports_for_nested_scopes(self):
+        self.mod.write('import pkg1\nprint pkg1\ndef a_func():\n    pass\nprint pkg1\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        imports = module_with_imports.get_used_imports(
+            pymod.get_attribute('a_func').get_object())
+        self.assertEquals(0, len(imports))
+
+    def test_getting_used_imports_for_nested_scopes2(self):
+        self.mod.write('from pkg1 import mod1\ndef a_func():\n    print mod1\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        imports = module_with_imports.get_used_imports(
+            pymod.get_attribute('a_func').get_object())
+        self.assertEquals(1, len(imports))
+        self.assertEquals('from pkg1 import mod1', imports[0].get_import_statement())
+
+    def test_empty_removing_unused_imports(self):
+        self.mod.write('import pkg1\nprint pkg1\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        module_with_imports.remove_unused_imports()
+        self.assertEquals('import pkg1\nprint pkg1\n',
+                          module_with_imports.get_changed_source())
+        
+    def test_simple_removing_unused_imports(self):
+        self.mod.write('import pkg1\n\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        module_with_imports.remove_unused_imports()
+        self.assertEquals('\n', module_with_imports.get_changed_source())
+        
+    def test_simple_removing_unused_imports_for_froms(self):
+        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
+        self.mod.write('from pkg1.mod1 import a_func, another_func\n\na_func()\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        module_with_imports.remove_unused_imports()
+        self.assertEquals('from pkg1.mod1 import a_func\n\na_func()\n',
+                          module_with_imports.get_changed_source())
+        
+    def test_simple_removing_unused_imports_for_from_stars(self):
+        self.mod.write('from pkg1.mod1 import *\n\n')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        module_with_imports.remove_unused_imports()
+        self.assertEquals('\n', module_with_imports.get_changed_source())
+    
+    def test_simple_removing_unused_imports_for_nested_modules(self):
+        self.mod1.write('def a_func():\n    pass\n')
+        self.mod.write('import pkg1.mod1\npkg1.mod1.a_func()')
+        pymod = self.pycore.get_module('mod')
+        module_with_imports = self.import_tools.get_module_with_imports(pymod)
+        module_with_imports.remove_unused_imports()
+        self.assertEquals('import pkg1.mod1\npkg1.mod1.a_func()',
+                          module_with_imports.get_changed_source())
+    
+    def test_removing_unused_imports_and_functions_of_the_same_name(self):