Anonymous avatar Anonymous committed aacd238

module_imports: renamed ModuleImports.get_import_statement() to imports

Comments (0)

Files changed (5)

rope/refactor/importutils/__init__.py

     def froms_to_imports(self, pymodule, import_filter=None):
         pymodule = self._clean_up_imports(pymodule, import_filter)
         module_imports = self.module_imports(pymodule, import_filter)
-        for import_stmt in module_imports.get_import_statements():
+        for import_stmt in module_imports.imports:
             if import_stmt.readonly or \
                not self._is_transformable_to_normal(import_stmt.import_info):
                 continue
 
         # Adding normal imports in place of froms
         module_imports = self.module_imports(pymodule, import_filter)
-        for import_stmt in module_imports.get_import_statements():
+        for import_stmt in module_imports.imports:
             if not import_stmt.readonly and \
                self._is_transformable_to_normal(import_stmt.import_info):
                 import_stmt.import_info = \
     pymodule = pydefined.get_module()
     module = module_imports.ModuleImports(pycore, pymodule)
     if pymodule == pydefined:
-        return [stmt.import_info for stmt in module.get_import_statements()]
+        return [stmt.import_info for stmt in module.imports]
     return module.get_used_imports(pydefined)
 
 
 
     visitor = actions.AddingVisitor(pycore, candidates)
     selected_import = normal_import
-    for import_statement in imports.get_import_statements():
+    for import_statement in imports.imports:
         if import_statement.accept(visitor):
             selected_import = visitor.import_info
             break

rope/refactor/importutils/module_imports.py

 import rope.base.pynames
-from rope.base import ast
+from rope.base import ast, utils
 from rope.refactor.importutils import importinfo
 from rope.refactor.importutils import actions
 
     def __init__(self, pycore, pymodule, import_filter=None):
         self.pycore = pycore
         self.pymodule = pymodule
-        self.import_statements = None
         self.separating_lines = 0
         self.filter = import_filter
 
-    def get_import_statements(self):
-        if self.import_statements is None:
-            finder = _GlobalImportFinder(self.pymodule, self.pycore)
-            self.import_statements = finder.find_import_statements()
-            self.separating_lines = finder.get_separating_line_count()
-            if self.filter is not None:
-                for import_stmt in self.import_statements:
-                    if not self.filter(import_stmt):
-                        import_stmt.readonly = True
-        return self.import_statements
+    @property
+    @utils.cacheit
+    def imports(self):
+        finder = _GlobalImportFinder(self.pymodule, self.pycore)
+        result = finder.find_import_statements()
+        self.separating_lines = finder.get_separating_line_count()
+        if self.filter is not None:
+            for import_stmt in result:
+                if not self.filter(import_stmt):
+                    import_stmt.readonly = True
+        return result
 
     def _get_unbound_names(self, defined_pyobject):
         visitor = _GlobalUnboundNameFinder(self.pymodule, defined_pyobject)
         can_select = _OneTimeSelector(self._get_unbound_names(self.pymodule))
         visitor = actions.RemovingVisitor(
             self.pycore, self._current_folder(), can_select)
-        for import_statement in self.get_import_statements():
+        for import_statement in self.imports:
             import_statement.accept(visitor)
 
     def get_used_imports(self, defined_pyobject):
-        all_import_statements = self.get_import_statements()
         result = []
         can_select = _OneTimeSelector(self._get_unbound_names(defined_pyobject))
         visitor = actions.FilteringVisitor(
             self.pycore, self._current_folder(), can_select)
-        for import_statement in all_import_statements:
+        for import_statement in self.imports:
             new_import = import_statement.accept(visitor)
             if new_import is not None and not new_import.is_empty():
                 result.append(new_import)
         return result
 
     def get_changed_source(self):
-        imports = self.get_import_statements()
+        imports = self.imports
         after_removing = self._remove_imports(imports)
         imports = [stmt for stmt in imports
                    if not stmt.import_info.is_empty()]
 
     def add_import(self, import_info):
         visitor = actions.AddingVisitor(self.pycore, [import_info])
-        for import_statement in self.get_import_statements():
+        for import_statement in self.imports:
             if import_statement.accept(visitor):
                 break
         else:
-            all_imports = self.get_import_statements()
             lineno = self._get_new_import_lineno()
             blanks = self._get_new_import_blanks()
-            all_imports.append(importinfo.ImportStatement(
-                               import_info, lineno, lineno,
-                               blank_lines=blanks))
+            self.imports.append(importinfo.ImportStatement(
+                                import_info, lineno, lineno,
+                                blank_lines=blanks))
 
     def _get_new_import_blanks(self):
         return 0
 
     def _get_new_import_lineno(self):
-        imports = self.get_import_statements()
-        if imports:
-            return imports[-1].end_line
+        if self.imports:
+            return self.imports[-1].end_line
         return 1
 
     def filter_names(self, can_select):
         visitor = actions.RemovingVisitor(
             self.pycore, self._current_folder(), can_select)
-        for import_statement in self.get_import_statements():
+        for import_statement in self.imports:
             import_statement.accept(visitor)
 
     def expand_stars(self):
         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():
+        for import_statement in self.imports:
             import_statement.accept(visitor)
 
     def remove_duplicates(self):
-        imports = self.get_import_statements()
         added_imports = []
-        for import_stmt in imports:
+        for import_stmt in self.imports:
             visitor = actions.AddingVisitor(self.pycore,
                                             [import_stmt.import_info])
             for added_import in added_imports:
     def get_relative_to_absolute_list(self):
         visitor = rope.refactor.importutils.actions.RelativeToAbsoluteVisitor(
             self.pycore, self._current_folder())
-        for import_stmt in self.get_import_statements():
+        for import_stmt in self.imports:
             if not import_stmt.readonly:
                 import_stmt.accept(visitor)
         return visitor.to_be_absolute
     def get_self_import_fix_and_rename_list(self):
         visitor = rope.refactor.importutils.actions.SelfImportVisitor(
             self.pycore, self._current_folder(), self.pymodule.get_resource())
-        for import_stmt in self.get_import_statements():
+        for import_stmt in self.imports:
             if not import_stmt.readonly:
                 import_stmt.accept(visitor)
         return visitor.to_be_fixed, visitor.to_be_renamed
 
     def sort_imports(self):
         # IDEA: Sort from import list
-        all_import_statements = self.get_import_statements()
         visitor = actions.SortingVisitor(self.pycore, self._current_folder())
-        for import_statement in all_import_statements:
+        for import_statement in self.imports:
             import_statement.accept(visitor)
         in_projects = sorted(visitor.in_project, self._compare_imports)
         third_party = sorted(visitor.third_party, self._compare_imports)
     def handle_long_imports(self, maxdots, maxlength):
         visitor = actions.LongImportVisitor(
             self._current_folder(), self.pycore, maxdots, maxlength)
-        for import_statement in self.get_import_statements():
+        for import_statement in self.imports:
             if not import_statement.readonly:
                 import_statement.accept(visitor)
         for import_info in visitor.new_imports:

rope/refactor/move.py

 
         module_with_imports = self.import_tools.module_imports(pymodule)
         source = pymodule.source_code
-        if module_with_imports.get_import_statements():
+        if module_with_imports.imports:
             start = pymodule.lines.get_line_end(
-                module_with_imports.get_import_statements()[-1].end_line - 1)
+                module_with_imports.imports[-1].end_line - 1)
             result = source[:start + 1] + '\n\n'
         else:
             result = ''
     origin = pycore.resource_to_pyobject(resource)
 
     imports = []
-    for stmt in import_tools.module_imports(origin).get_import_statements():
+    for stmt in import_tools.module_imports(origin).imports:
         imports.append(stmt.import_info)
 
     back_names = []
     # extracting imports after changes
     module_with_imports = import_tools.module_imports(pymodule)
     imports = [import_stmt.import_info
-               for import_stmt in module_with_imports.get_import_statements()]
+               for import_stmt in module_with_imports.imports]
     start = 1
-    if module_with_imports.get_import_statements():
-        start = module_with_imports.get_import_statements()[-1].end_line
+    if module_with_imports.imports:
+        start = module_with_imports.imports[-1].end_line
     lines = codeanalyze.SourceLinesAdapter(source)
     while start < lines.length() and not lines.get_line(start).strip():
         start += 1

rope/refactor/restructure.py

                                                  resource)
         imports = module_imports.ModuleImports(self.pycore, pymodule)
         return [imports.import_info
-                for imports in imports.get_import_statements()]
+                for imports in imports.imports]
 
     def make_checks(self, string_checks):
         """Convert str to str dicts to str to PyObject dicts

ropetest/refactor/importutilstest.py

         self.mod.write('import pkg1\n')
         pymod = self.pycore.get_module('mod')
         module_with_imports = self.import_tools.module_imports(pymod)
-        imports = module_with_imports.get_import_statements()
+        imports = module_with_imports.imports
         self.assertEquals('import pkg1',
                           imports[0].import_info.get_import_statement())
 
         self.mod.write('import pkg1.mod1 as mod1\n')
         pymod = self.pycore.get_module('mod')
         module_with_imports = self.import_tools.module_imports(pymod)
-        imports = module_with_imports.get_import_statements()
+        imports = module_with_imports.imports
         self.assertEquals('import pkg1.mod1 as mod1',
                           imports[0].import_info.get_import_statement())
 
         self.mod.write('from pkg1 import mod1\n')
         pymod = self.pycore.get_module('mod')
         module_with_imports = self.import_tools.module_imports(pymod)
-        imports = module_with_imports.get_import_statements()
+        imports = module_with_imports.imports
         self.assertEquals('from pkg1 import mod1',
                           imports[0].import_info.get_import_statement())
 
         self.mod.write('from pkg1 \\\n    import mod1\n')
         pymod = self.pycore.get_module('mod')
         module_with_imports = self.import_tools.module_imports(pymod)
-        imports = module_with_imports.get_import_statements()
+        imports = module_with_imports.imports
         self.assertEquals('from pkg1 import mod1',
                           imports[0].import_info.get_import_statement())
 
         self.mod.write('from pkg1 import *\n')
         pymod = self.pycore.get_module('mod')
         module_with_imports = self.import_tools.module_imports(pymod)
-        imports = module_with_imports.get_import_statements()
+        imports = module_with_imports.imports
         self.assertEquals('from pkg1 import *',
                           imports[0].import_info.get_import_statement())
 
         self.mod2.write('from .mod3 import x\n')
         pymod = self.pycore.get_module('pkg2.mod2')
         module_with_imports = self.import_tools.module_imports(pymod)
-        imports = module_with_imports.get_import_statements()
+        imports = module_with_imports.imports
         self.assertEquals('from .mod3 import x',
                           imports[0].import_info.get_import_statement())
 
         self.mod.write('if True:\n    import pkg1\n')
         pymod = self.pycore.get_module('mod')
         module_with_imports = self.import_tools.module_imports(pymod)
-        imports = module_with_imports.get_import_statements()
+        imports = module_with_imports.imports
         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.module_imports(pymod)
-        imports = module_with_imports.get_import_statements()
+        imports = module_with_imports.imports
         context = importinfo.ImportContext(self.pycore, self.project.root)
         self.assertEquals(['pkg'],
                           imports[0].import_info.get_imported_names(context))
         self.mod.write('import pkg1.mod1\n')
         pymod = self.pycore.get_module('mod')
         module_with_imports = self.import_tools.module_imports(pymod)
-        imports = module_with_imports.get_import_statements()
+        imports = module_with_imports.imports
         context = importinfo.ImportContext(self.pycore, self.project.root)
         self.assertEquals(['pkg1'],
                           imports[0].import_info.get_imported_names(context))
         self.mod.write('from pkg1.mod1 import *\n')
         pymod = self.pycore.get_module('mod')
         module_with_imports = self.import_tools.module_imports(pymod)
-        imports = module_with_imports.get_import_statements()
+        imports = module_with_imports.imports
         context = importinfo.ImportContext(self.pycore, self.project.root)
         self.assertEquals(['a_func'],
                           imports[0].import_info.get_imported_names(context))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.