Anonymous avatar Anonymous committed 333a5f5

pycore: added PyCore.modname() from importutils.get_module_name()

Comments (0)

Files changed (11)

rope/base/pycore.py

     def __str__(self):
         return str(self.module_cache) + str(self.object_info)
 
+    def modname(self, resource):
+        if resource.is_folder():
+            module_name = resource.name
+            source_folder = resource.parent
+        elif resource.name == '__init__.py':
+            module_name = resource.parent.name
+            source_folder = resource.parent.parent
+        else:
+            module_name = resource.name[:-3]
+            source_folder = resource.parent
+
+        source_folders = self.get_source_folders()
+        source_folders.extend(self.get_python_path_folders())
+        while source_folder != source_folder.parent and \
+              source_folder not in source_folders:
+            module_name = source_folder.name + '.' + module_name
+            source_folder = source_folder.parent
+        return module_name
+
 
 class _ModuleCache(object):
 

rope/contrib/autoimport.py

             pass
 
     def _module_name(self, resource):
-        return importutils.get_module_name(self.project.pycore, resource)
+        return self.project.pycore.modname(resource)
 
     def _add_names(self, pymodule, modname, underlined):
         if underlined is None:

rope/contrib/codeassist.py

         if module is not None:
             resource = module.get_resource()
             if resource is not None:
-                return importutils.get_module_name(
-                    pyfunction.pycore, resource) + '.'
+                return pyfunction.pycore.modname(resource) + '.'
         return ''
 
     def _trim_docstring(self, docstring, indents=0):

rope/contrib/generate.py

     pymodule = pycore.resource_to_pyobject(resource)
     import_tools = importutils.ImportTools(pycore)
     module_imports = import_tools.module_imports(pymodule)
-    module_name = importutils.get_module_name(pycore, imported)
+    module_name = pycore.modname(imported)
     new_import = importutils.NormalImport(((module_name, None), ))
     module_imports.add_import(new_import)
     return change.ChangeContents(resource, module_imports.get_changed_source())

rope/refactor/importutils/__init__.py

 from rope.base.change import ChangeSet, ChangeContents
 from rope.refactor import occurrences, rename
 from rope.refactor.importutils import module_imports
-from rope.refactor.importutils.importinfo import \
-     (NormalImport, FromImport, get_module_name)
+from rope.refactor.importutils.importinfo import NormalImport, FromImport
 import rope.base.codeanalyze
 
 
 
     def get_import(self, resource):
         """The import statement for `resource`"""
-        module_name = get_module_name(self.pycore, resource)
+        module_name = self.pycore.modname(resource)
         return NormalImport(((module_name, None), ))
 
     def get_from_import(self, resource, name):
         """The from import statement for `name` in `resource`"""
-        module_name = get_module_name(self.pycore, resource)
+        module_name = self.pycore.modname(resource)
         names = []
         if isinstance(name, list):
             names = [(imported, None) for imported in name]

rope/refactor/importutils/actions.py

             if resource is None:
                 new_pairs.append((name, alias))
                 continue
-            absolute_name = importinfo.get_module_name(self.pycore, resource)
+            absolute_name = self.pycore.modname(resource)
             new_pairs.append((absolute_name, alias))
         if not import_info._are_name_and_alias_lists_equal(
             new_pairs, import_info.names_and_aliases):
             resource = self.pycore.find_module(name, current_folder=self.folder)
             if resource is None:
                 continue
-            absolute_name = importinfo.get_module_name(self.pycore, resource)
+            absolute_name = self.pycore.modname(resource)
             if absolute_name != name:
                 result.append((name, absolute_name))
         return result
         resource = import_info.get_imported_resource(self.context)
         if resource is None:
             return None
-        absolute_name = importinfo.get_module_name(self.pycore, resource)
+        absolute_name = self.pycore.modname(resource)
         if import_info.module_name != absolute_name:
             import_stmt.import_info = importinfo.FromImport(
                 absolute_name, 0, import_info.names_and_aliases)

rope/refactor/importutils/importinfo.py

         return []
 
 
-def get_module_name(pycore, resource):
-    if resource.is_folder():
-        module_name = resource.name
-        source_folder = resource.parent
-    elif resource.name == '__init__.py':
-        module_name = resource.parent.name
-        source_folder = resource.parent.parent
-    else:
-        module_name = resource.name[:-3]
-        source_folder = resource.parent
-
-    source_folders = pycore.get_source_folders()
-    source_folders.extend(pycore.get_python_path_folders())
-    while source_folder != source_folder.parent and \
-          source_folder not in source_folders:
-        module_name = source_folder.name + '.' + module_name
-        source_folder = source_folder.parent
-    return module_name
-
-
 class ImportContext(object):
 
     def __init__(self, pycore, folder):

rope/refactor/introduce_factory.py

                 if global_:
                     new_pymodule = self.pycore.get_string_module(changed_code,
                                                                  self.resource)
-                    modname = importutils.get_module_name(self.pycore,
-                                                          self.resource)
+                    modname = self.pycore.modname(self.resource)
                     changed_code, imported = importutils.add_import(
                         self.pycore, new_pymodule, modname, factory_name)
                     changed_code = changed_code.replace(replacement, imported)

rope/refactor/move.py

         return ChangeContents(self.source, source)
 
     def _new_modname(self, dest):
-        return importutils.get_module_name(self.pycore, dest)
+        return self.pycore.modname(dest)
 
     def _dest_module_changes(self, dest):
         # Changing occurrences
         return changes
 
     def _new_modname(self, dest):
-        destname = importutils.get_module_name(self.pycore, dest)
+        destname = self.pycore.modname(dest)
         if destname:
             return destname + '.' + self.old_name
         return self.old_name

rope/refactor/similarfinder.py

         while pydefined.parent is not None:
             address.insert(0, pydefined.get_name())
             pydefined = pydefined.parent
-        module_name = importutils.get_module_name(pydefined.pycore,
-                                                  pydefined.resource)
+        module_name = pydefined.pycore.modname(pydefined.resource)
     return '.'.join(module_name.split('.') + address)

rope/refactor/usefunction.py

         return find_temps(self.project, self._get_body())
 
     def _module_name(self):
-        return importutils.get_module_name(self.project.pycore,
-                                           self.resource)
+        return self.project.pycore.modname(self.resource)
 
     def _make_pattern(self):
         params = self.pyfunction.get_param_names()
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.