Commits

Anonymous committed 2f0b057

Added import_filter argument to ImportTools.expand_stars and relative_to_absolute

  • Participants
  • Parent commits 80c0e9f

Comments (0)

Files changed (6)

File docs/dev/issues.txt

 
 * `Memory management`_
 * Should `rope.base` be thread safe? which parts?
-* Better `WordRangeFinder`
 
 
 Command Prefix
 * Better import handling
 
 
-Remove `PyCore._add_dependency()`
-=================================
-
-After the discussions about concluded data invalidation, module
-dependency information is not used anymore.  Maybe we can remove them
-altogether in the hope that maybe in future a better approach is
-possible.
-
-
 Cache Invalidation Again ...
 ============================
 
 every change.
 
 
+Removing `PyCore._add_dependency()`
+-----------------------------------
+
+After the discussions about concluded data invalidation, module
+dependency information is not used anymore.  Maybe we can remove them
+altogether in the hope that maybe in future a better approach is
+possible.
+
+
+
 Separating Ropeide
 ==================
 

File docs/dev/stories.txt

 > Public Release 0.6.2
 
 
+* ``C-u`` action prefix
+
+
 * An option to decide whether to use froms or normal imports for back
   imports
 
 * Inlining a single occurrence
 
 
-* Changing only one import in `importutils.actions`
+* Adding an option for removing the definition when inlining
 
 
-* Performing import actions only on one import statement
-
-
-* ``C-u`` action prefix
+* Performing import actions on individual imports

File docs/dev/workingon.txt

-Small Stories
-=============
+Performing Import Actions On Individual Imports
+===============================================
 
-- Removing pycore._add_dependency?
+* Adding an function to `ImportTools` method that returns `True` if
+  action can be performed on the import statement passed to it.
 
+  - Filtered expanding stars
+  - Filtered relative to absolute
+
+  * Filtered handle long imports
+  * Filtered froms to normals
+
+* Relative to absolute uses two phases; filtering problems
+* Handle long imports should work on filtered imports unconditionally?
+
+* Better name for import_filter
+* Changing `rope.refactor.ImportOrganizer` to add offset -> import
+  filter transformation? offset or line_number
+* Think about the UI; Adding prefix?
+
+* Should removing duplicate imports in froms to imports be changed?
+* Removing relative to absolute or changing it to support ``from .
+  import x`` style imports?
+* Better handling of ``__init__.py``\s in change buffer
+* Sorting items in find file or buffer list
 * Refactor codeanalyze
 * Adding an option to inline not to remove the definition
 * Renaming ``rope.py`` to ``ropeide.py`` and ``~/.rope`` to

File rope/refactor/importutils/__init__.py

      (NormalImport, FromImport, get_module_name)
 
 
+def filter_none(import_stmt):
+    return True
+
+
 class ImportTools(object):
 
     def __init__(self, pycore):
     def get_module_imports(self, module):
         return module_imports.ModuleImports(self.pycore, module)
 
-    def froms_to_imports(self, pymodule):
-        pymodule = self._clean_up_imports(pymodule)
+    def froms_to_imports(self, pymodule, import_filter=filter_none):
+        pymodule = self._clean_up_imports(pymodule, import_filter)
         module_imports = self.get_module_imports(pymodule)
         for import_stmt in module_imports.get_import_statements():
-            if not self._can_import_be_transformed_to_normal_import(
-                import_stmt.import_info):
+            if not self._is_transformable_to_normal(import_stmt.import_info):
                 continue
             pymodule = self._from_to_normal(pymodule, import_stmt)
 
         # Adding normal imports in place of froms
         module_imports = self.get_module_imports(pymodule)
         for import_stmt in module_imports.get_import_statements():
-            if self._can_import_be_transformed_to_normal_import(import_stmt.import_info):
+            if self._is_transformable_to_normal(import_stmt.import_info):
                 import_stmt.import_info = \
                     NormalImport(((import_stmt.import_info.module_name, None),))
         module_imports.remove_duplicates()
         return module_imports.get_changed_source()
 
-    def expand_stars(self, pymodule):
+    def expand_stars(self, pymodule, import_filter=filter_none):
         module_imports = self.get_module_imports(pymodule)
-        module_imports.expand_stars()
+        module_imports.expand_stars(import_filter)
         return module_imports.get_changed_source()
 
     def _from_to_normal(self, pymodule, import_stmt):
                 pymodule = self.pycore.get_string_module(source, resource)
         return pymodule
 
-    def _clean_up_imports(self, pymodule):
+    def _clean_up_imports(self, pymodule, import_filter):
         resource = pymodule.get_resource()
         module_with_imports = self.get_module_imports(pymodule)
         module_with_imports.expand_stars()
             pymodule = self.pycore.get_string_module(source, resource)
         return pymodule
 
-    def relatives_to_absolutes(self, pymodule):
+    def relatives_to_absolutes(self, pymodule, import_filter=filter_none):
         module_imports = self.get_module_imports(pymodule)
-        to_be_absolute_list = module_imports.get_relative_to_absolute_list()
+        to_be_absolute_list = module_imports.get_relative_to_absolute_list(
+            import_filter)
         for name, absolute_name in to_be_absolute_list:
             pymodule = self._rename_in_module(pymodule, name, absolute_name)
         module_imports = self.get_module_imports(pymodule)
-        module_imports.get_relative_to_absolute_list()
+        module_imports.get_relative_to_absolute_list(import_filter)
         source = module_imports.get_changed_source()
         if source is None:
             source = pymodule.source_code
         return source
 
-    def _can_import_be_transformed_to_normal_import(self, import_info):
+    def _is_transformable_to_normal(self, import_info):
         if not isinstance(import_info, FromImport):
             return False
         return True

File rope/refactor/importutils/module_imports.py

 from rope.refactor.importutils import actions
 
 
+def pass_all_imports(import_stmt):
+    return True
+
+
 class ModuleImports(object):
 
     def __init__(self, pycore, pymodule):
         for import_statement in self.get_import_statements():
             import_statement.accept(visitor)
 
-    def expand_stars(self):
+    def expand_stars(self, import_filter=pass_all_imports):
         can_select = _OneTimeSelector(self._get_unbound_names(self.pymodule))
         visitor = actions.ExpandStarsVisitor(
             self.pycore, self._current_folder(), can_select)
         for import_statement in self.get_import_statements():
-            import_statement.accept(visitor)
+            if import_filter(import_statement):
+                import_statement.accept(visitor)
 
     def remove_duplicates(self):
         imports = self.get_import_statements()
             else:
                 added_imports.append(import_stmt)
 
-    def get_relative_to_absolute_list(self):
+    def get_relative_to_absolute_list(self, import_filter=pass_all_imports):
         visitor = rope.refactor.importutils.actions.RelativeToAbsoluteVisitor(
             self.pycore, self._current_folder())
         for import_stmt in self.get_import_statements():
-            import_stmt.accept(visitor)
+            if import_filter(import_stmt):
+                import_stmt.accept(visitor)
         return visitor.to_be_absolute
 
     def get_self_import_fix_and_rename_list(self):

File ropetest/refactor/importutilstest.py

             '"""\ndocs\n"""\nimport mod\n\n\ndef f():\n    print(mod)\n',
             self.import_tools.sort_imports(pymod))
 
-    def test_selective_import_organization(self):
+    def test_customized_import_organization(self):
         self.mod.write('import sys\nimport sys\n')
         pymod = self.pycore.resource_to_pyobject(self.mod)
         self.assertEquals(
             'import sys\n',
             self.import_tools.organize_imports(pymod, unused=False))
 
-    def test_selective_import_organization2(self):
+    def test_customized_import_organization2(self):
         self.mod.write('import sys\n')
         pymod = self.pycore.resource_to_pyobject(self.mod)
         self.assertEquals(
             'import sys\n',
             self.import_tools.organize_imports(pymod, unused=False))
 
-    def test_selective_import_organization3(self):
+    def test_customized_import_organization3(self):
         self.mod.write('import sys\nimport mod\n\n\nvar = 1\nprint(mod.var)\n')
         pymod = self.pycore.resource_to_pyobject(self.mod)
         self.assertEquals(
             'import sys\n\n\nvar = 1\nprint(var)\n',
             self.import_tools.organize_imports(pymod, unused=False))
 
+    def test_trivial_filtered_expand_stars(self):
+        self.pkg1.get_child('__init__.py').write('var1 = 1\n')
+        self.pkg2.get_child('__init__.py').write('var2 = 1\n')
+        self.mod.write('from pkg1 import *\nfrom pkg2 import *\n\n'
+                       'print(var1, var2)\n')
+        pymod = self.pycore.resource_to_pyobject(self.mod)
+        self.assertEquals(
+            'from pkg1 import *\nfrom pkg2 import *\n\nprint(var1, var2)\n',
+             self.import_tools.expand_stars(pymod, lambda stmt: False))
+
+    def _get_line_filter(self, lineno):
+        def import_filter(import_stmt):
+            return import_stmt.start_line <= lineno < import_stmt.end_line
+        return import_filter
+        
+    def test_filtered_expand_stars(self):
+        self.pkg1.get_child('__init__.py').write('var1 = 1\n')
+        self.pkg2.get_child('__init__.py').write('var2 = 1\n')
+        self.mod.write('from pkg1 import *\nfrom pkg2 import *\n\n'
+                     'print(var1, var2)\n')
+        pymod = self.pycore.resource_to_pyobject(self.mod)
+        self.assertEquals(
+            'from pkg1 import *\nfrom pkg2 import var2\n\nprint(var1, var2)\n',
+             self.import_tools.expand_stars(pymod, self._get_line_filter(2)))
+
+    def test_filtered_relative_to_absolute(self):
+        self.mod3.write('var = 1')
+        self.mod2.write('import mod3\n\nprint(mod3.var)\n')
+        pymod = self.pycore.resource_to_pyobject(self.mod2)
+        self.assertEquals(
+            'import mod3\n\nprint(mod3.var)\n',
+             self.import_tools.relatives_to_absolutes(
+                          pymod, lambda stmt: False))
+        self.assertEquals(
+            'import pkg2.mod3\n\nprint(pkg2.mod3.var)\n',
+             self.import_tools.relatives_to_absolutes(
+                          pymod, self._get_line_filter(1)))
+
 
 if __name__ == '__main__':
     unittest.main()