Anonymous avatar Anonymous committed 53aa045

autoimport: added docs

Comments (0)

Files changed (1)

rope/contrib/autoimport.py

 
 
 class AutoImport(object):
+    """A class for finding the module that provides a name
+
+    This class maintains a cache of global names in python modules.
+    Note that this cache is not accurate and might be out of date.
+
+    """
 
     def __init__(self, project, observe=False):
+        """Construct an AutoImport object
+
+        If `observe` is `True`, listen for project changes and update
+        the cache.
+
+        """
         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)
-        # XXX: handle moved and removed
+        project.data_files.add_write_hook(self._write)
+        # XXX: using a filtered observer
         observer = resourceobserver.ResourceObserver(
             changed=self._changed, moved=self._moved, removed=self._removed)
         if observe:
             project.add_observer(observer)
 
-    def write(self):
-        self.project.data_files.write_data('globalnames', self.names)
+    def import_assist(self, starting):
+        """Return a list of ``(name, module)`` tuples
 
-    def import_assist(self, starting):
+        This function tries to find modules that have a global name
+        that starts with `starting`.
+
+        """
         # XXX: breaking if gave up! use generators
         result = []
         for module in self.names:
         return result
 
     def get_modules(self, name):
+        """Return the list of modules that have global `name`"""
         result = []
         for module in self.names:
             if name in self.names[module]:
         return result
 
     def update_resource(self, resource):
+        """Update the cache for global names in `resource`"""
         try:
             pymodule = self.project.pycore.resource_to_pyobject(resource)
             modname = self._module_name(resource)
         except exceptions.ModuleSyntaxError:
             pass
 
+    def update_module(self, modname):
+        """Update the cache for global names in `modname` module
+
+        `modname` is the name of a module.
+        """
+        try:
+            pymodule = self.project.pycore.get_module(modname)
+            self._add_names(pymodule, modname)
+        except exceptions.ModuleNotFoundError:
+            pass
+
     def _module_name(self, resource):
         return importutils.get_module_name(self.project.pycore, resource)
 
     def _add_names(self, pymodule, modname):
-        # XXX: exclude imported names
         globals = []
         for name, pyname in pymodule._get_structural_attributes().items():
             if isinstance(pyname, (pynames.AssignedName, pynames.DefinedName)):
                 globals.append(name)
         self.names[modname] = globals
 
-    def update_module(self, modname):
-        try:
-            pymodule = self.project.pycore.get_module(modname)
-            self._add_names(pymodule, modname)
-        except exceptions.ModuleNotFoundError:
-            pass
+    def _write(self):
+        self.project.data_files.write_data('globalnames', self.names)
 
     def _changed(self, resource):
         if not resource.is_folder():
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.