Commits

Anonymous committed ae46732

Adding ImportContext for removing pycore and current folder in FromImport

  • Participants
  • Parent commits d81623e

Comments (0)

Files changed (6)

docs/dev/workingon.txt

-Handling Recursive Restructurings
-=================================
-
-- Preventing recursion when matching ``${?a}``
+Small Stories
+=============
 
 * Refactor `rope.base.refactor.importutils`
 * Not holding current_folder in `FromImport`?

rope/refactor/importutils/__init__.py

 
     def get_from_import_for_module(self, module, name):
         module_name = get_module_name(self.pycore, module.get_resource())
-        return FromImport(module_name, 0, ((name, None),),
-                          module.get_resource().parent, self.pycore)
+        return FromImport(module_name, 0, ((name, None),))
 
     def get_module_imports(self, module):
         return module_imports.ModuleImports(self.pycore, module)

rope/refactor/importutils/actions.py

         self.to_be_absolute = []
         self.pycore = pycore
         self.current_folder = current_folder
+        self.context = importinfo.ImportContext(pycore, current_folder)
 
     def visitNormalImport(self, import_stmt, import_info):
         self.to_be_absolute.extend(self._get_relative_to_absolute_list(import_info))
         return result
 
     def visitFromImport(self, import_stmt, import_info):
-        resource = import_info.get_imported_resource()
+        resource = import_info.get_imported_resource(self.context)
         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)
+                absolute_name, 0, import_info.names_and_aliases)
 
 
 class FilteringVisitor(ImportInfoVisitor):
 
-    def __init__(self, pycore, can_select):
+    def __init__(self, pycore, folder, can_select):
         self.to_be_absolute = []
         self.pycore = pycore
         self.can_select = self._transform_can_select(can_select)
+        self.context = importinfo.ImportContext(pycore, folder)
 
     def _transform_can_select(self, can_select):
         def can_select_name_and_alias(name, alias):
     def visitFromImport(self, import_stmt, import_info):
         new_pairs = []
         if import_info.is_star_import():
-            for name in import_info.get_imported_names():
+            for name in import_info.get_imported_names(self.context):
                 if self.can_select(name, None):
                     new_pairs.append(import_info.names_and_aliases[0])
                     break
                 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)
+            import_info.module_name, import_info.level, new_pairs)
 
 
 class RemovingVisitor(ImportInfoVisitor):
 
-    def __init__(self, pycore, can_select):
+    def __init__(self, pycore, folder, can_select):
         self.to_be_absolute = []
         self.pycore = pycore
-        self.filtering = FilteringVisitor(pycore, can_select)
+        self.filtering = FilteringVisitor(pycore, folder, can_select)
 
     def dispatch(self, import_):
         result = self.filtering.dispatch(import_)
                 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)
+                import_info.module_name, import_info.level, new_pairs)
             return True
 
 
 class ExpandStarsVisitor(ImportInfoVisitor):
 
-    def __init__(self, pycore, can_select):
+    def __init__(self, pycore, folder, can_select):
         self.pycore = pycore
-        self.filtering = FilteringVisitor(pycore, can_select)
+        self.filtering = FilteringVisitor(pycore, folder, can_select)
+        self.context = importinfo.ImportContext(pycore, folder)
 
     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():
+            for name in import_info.get_imported_names(self.context):
                 new_pairs.append((name, None))
             new_import = importinfo.FromImport(
-                import_info.module_name, import_info.level, new_pairs,
-                import_info.current_folder, self.pycore)
-            import_stmt.import_info = self.filtering.visitFromImport(None, new_import)
+                import_info.module_name, import_info.level, new_pairs)
+            import_stmt.import_info = \
+                self.filtering.visitFromImport(None, new_import)
         else:
             self.filtering.dispatch(import_stmt)
 
         self.resource = resource
         self.to_be_fixed = set()
         self.to_be_renamed = set()
+        self.context = importinfo.ImportContext(pycore, current_folder)
 
     def visitNormalImport(self, import_stmt, import_info):
         new_pairs = []
             import_stmt.import_info = importinfo.NormalImport(new_pairs)
 
     def visitFromImport(self, import_stmt, import_info):
-        resource = import_info.get_imported_resource()
+        resource = import_info.get_imported_resource(self.context)
         if resource is None:
             return
         if resource == self.resource:
         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)
+                import_info.module_name, import_info.level, new_pairs)
 
     def _importing_names_from_self(self, import_info, import_stmt):
         if not import_info.is_star_import():
         self.standard = set()
         self.third_party = set()
         self.in_project = set()
+        self.context = importinfo.ImportContext(pycore, current_folder)
 
     def visitNormalImport(self, import_stmt, import_info):
         if import_info.names_and_aliases:
             self._check_imported_resource(import_stmt, resource, name)
 
     def visitFromImport(self, import_stmt, import_info):
-        resource = import_info.get_imported_resource()
+        resource = import_info.get_imported_resource(self.context)
         self._check_imported_resource(import_stmt, resource,
                                       import_info.module_name)
 
                 from_ = name[:last_dot]
                 imported = name[last_dot + 1:]
                 self.new_imports.append(
-                    importinfo.FromImport(from_, 0, ((imported, None), ),
-                                          self.current_folder, self.pycore))
+                    importinfo.FromImport(from_, 0, ((imported, None), )))
 
     def _is_long(self, name):
         return name.count('.') > self.maxdots or \

rope/refactor/importutils/importinfo.py

 
 class ImportInfo(object):
 
-    def get_imported_primaries(self):
+    def get_imported_primaries(self, context):
         pass
 
-    def get_imported_names(self):
+    def get_imported_names(self, context):
         return [primary.split('.')[0]
-                for primary in self.get_imported_primaries()]
+                for primary in self.get_imported_primaries(context)]
 
     def get_import_statement(self):
         pass
     def __init__(self, names_and_aliases):
         self.names_and_aliases = names_and_aliases
 
-    def get_imported_primaries(self):
+    def get_imported_primaries(self, context):
         result = []
         for name, alias in self.names_and_aliases:
             if alias:
 
 class FromImport(ImportInfo):
 
-    def __init__(self, module_name, level,
-                 names_and_aliases, current_folder, pycore):
+    def __init__(self, module_name, level, names_and_aliases):
         self.module_name = module_name
         self.level = level
         self.names_and_aliases = names_and_aliases
-        self.current_folder = current_folder
-        self.pycore = pycore
 
-    def get_imported_primaries(self):
+    def get_imported_primaries(self, context):
         if self.names_and_aliases[0][0] == '*':
-            module = self.get_imported_module()
+            module = self.get_imported_module(context)
             return [name for name in module.get_attributes().keys()
                     if not name.startswith('_')]
         result = []
                 result.append(name)
         return result
 
-    def get_imported_module(self):
+    def get_imported_module(self, context):
         if self.level == 0:
-            return self.pycore.get_module(self.module_name,
-                                          self.current_folder)
+            return context.pycore.get_module(
+                self.module_name, context.folder)
         else:
-            return self.pycore.get_relative_module(
-                self.module_name, self.current_folder, self.level)
+            return context.pycore.get_relative_module(
+                self.module_name, context.folder, self.level)
 
-    def get_imported_resource(self):
+    def get_imported_resource(self, context):
         if self.level == 0:
-            return self.pycore.find_module(
-                self.module_name, current_folder=self.current_folder)
+            return context.pycore.find_module(
+                self.module_name, current_folder=context.folder)
         else:
-            return self.pycore.find_relative_module(
-                self.module_name, self.current_folder, self.level)
+            return context.pycore.find_relative_module(
+                self.module_name, context.folder, self.level)
 
     def get_import_statement(self):
         result = 'from ' + '.' * self.level + self.module_name + ' import '
     def is_empty(self):
         return True
 
-    def get_imported_primaries(self):
+    def get_imported_primaries(self, context):
         return []
 
 
         module_name = source_folder.name + '.' + module_name
         source_folder = source_folder.parent
     return module_name
+
+
+class ImportContext(object):
+
+    def __init__(self, pycore, folder):
+        self.pycore = pycore
+        self.folder = folder

rope/refactor/importutils/module_imports.py

 
     def remove_unused_imports(self):
         can_select = _OneTimeSelector(self._get_unbound_names(self.pymodule))
-        visitor = actions.RemovingVisitor(self.pycore, can_select)
+        visitor = actions.RemovingVisitor(
+            self.pycore, self._current_folder(), 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 = actions.FilteringVisitor(self.pycore, can_select)
+        visitor = actions.FilteringVisitor(
+            self.pycore, self._current_folder(), 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():
                                blank_lines=blank_lines))
 
     def filter_names(self, can_select):
-        visitor = actions.RemovingVisitor(self.pycore, can_select)
+        visitor = actions.RemovingVisitor(
+            self.pycore, self._current_folder(), 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 = actions.ExpandStarsVisitor(self.pycore, can_select)
+        visitor = actions.ExpandStarsVisitor(
+            self.pycore, self._current_folder(), can_select)
         for import_statement in self.get_import_statements():
             import_statement.accept(visitor)
 
         if node.level:
             level = node.level
         import_info = importinfo.FromImport(
-            node.module, level, self._get_names(node.names),
-            self.current_folder, self.pycore)
+            node.module, level, self._get_names(node.names))
         start_line = node.lineno
         self.imports.append(importinfo.ImportStatement(
                             import_info, node.lineno, end_line,

ropetest/refactor/importutilstest.py

 import unittest
 
-from rope.refactor.importutils import ImportTools
+from rope.refactor.importutils import ImportTools, importinfo
 from ropetest import testutils
 
 
         pymod = self.pycore.get_module('mod')
         module_with_imports = self.import_tools.get_module_imports(pymod)
         imports = module_with_imports.get_import_statements()
-        self.assertEquals(['pkg'], imports[0].import_info.get_imported_names())
+        context = importinfo.ImportContext(self.pycore, self.project.root)
+        self.assertEquals(['pkg'],
+                          imports[0].import_info.get_imported_names(context))
 
     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_imports(pymod)
         imports = module_with_imports.get_import_statements()
-        self.assertEquals(['pkg1'], imports[0].import_info.get_imported_names())
+        context = importinfo.ImportContext(self.pycore, self.project.root)
+        self.assertEquals(['pkg1'],
+                          imports[0].import_info.get_imported_names(context))
 
     def test_import_get_names_with_alias2(self):
         self.mod1.write('def a_func():\n    pass\n')
         pymod = self.pycore.get_module('mod')
         module_with_imports = self.import_tools.get_module_imports(pymod)
         imports = module_with_imports.get_import_statements()
-        self.assertEquals(['a_func'], imports[0].import_info.get_imported_names())
+        context = importinfo.ImportContext(self.pycore, self.project.root)
+        self.assertEquals(['a_func'],
+                          imports[0].import_info.get_imported_names(context))
 
     def test_empty_getting_used_imports(self):
         self.mod.write('')