Anonymous avatar Anonymous committed b804b8c

added autoimport module

Comments (0)

Files changed (2)

rope/contrib/autoimport.py

+from rope.refactor import importutils
+
+class AutoImport(object):
+
+    def __init__(self, project):
+        self.project = project
+        self.names = project.data_files.read_data('globalnames')
+        if self.names is None:
+            self.names = {}
+        project.data_files.add_write_hook(self.write)
+
+    def write(self):
+        self.project.data_files.write_data('globalnames', self.names)
+
+    def get_imports(self, starting):
+        # XXX: breaking if gave up! use generators
+        result = []
+        for module in self.names:
+            for global_name in self.names[module]:
+                if global_name.startswith(starting):
+                    result.append((global_name, module))
+        return result
+
+    def update_resource(self, resource):
+        # XXX: what if there are syntax errors
+        pymodule = self.project.pycore.resource_to_pyobject(resource)
+        modname = importutils.get_module_name(self.project.pycore,
+                                              resource)
+        self._add_names(pymodule, modname)
+
+    def _add_names(self, pymodule, modname):
+        # XXX: exclude imported names
+        globals = [x for x in pymodule._get_structural_attributes()]
+        self.names[modname] = globals
+
+    def update_module(self, modname):
+        # XXX: what if module cannot be found?
+        pymodule = self.project.pycore.get_module(modname)
+        self._add_names(pymodule, modname)

ropetest/contrib/autoimporttest.py

+import unittest
+
+from ropetest import testutils
+from rope.contrib import autoimport
+
+
+class AutoImportTest(unittest.TestCase):
+
+    def setUp(self):
+        super(AutoImportTest, self).setUp()
+        self.project = testutils.sample_project()
+        self.mod1 = testutils.create_module(self.project, 'mod1')
+        self.pkg = testutils.create_package(self.project, 'pkg')
+        self.mod2 = testutils.create_module(self.project, 'mod2', self.pkg)
+
+    def tearDown(self):
+        testutils.remove_project(self.project)
+        super(AutoImportTest, self).tearDown()
+
+    def test_simple_case(self):
+        importer = autoimport.AutoImport(self.project)
+        self.assertEquals([], importer.get_imports('A'))
+
+    def test_update_resource(self):
+        importer = autoimport.AutoImport(self.project)
+        self.mod1.write('myvar = None')
+        importer.update_resource(self.mod1)
+        self.assertEquals([('myvar', 'mod1')],
+                          importer.get_imports('myva'))
+
+    def test_update_module(self):
+        importer = autoimport.AutoImport(self.project)
+        self.mod1.write('myvar = None')
+        importer.update_module('mod1')
+        self.assertEquals([('myvar', 'mod1')],
+                          importer.get_imports('myva'))
+
+
+if __name__ == '__main__':
+    unittest.main()
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.