예제 #1
0
    def _change_occurrences_in_module(self, dest, pymodule=None,
                                      resource=None):
        if not self.tools.occurs_in_module(pymodule=pymodule,
                                           resource=resource):
            return
        if pymodule is None:
            pymodule = self.project.get_pymodule(resource)
        new_name = self._new_modname(dest)
        module_imports = importutils.get_module_imports(self.project, pymodule)
        changed = False

        source = None
        if libutils.modname(dest):
            changed = self._change_import_statements(dest, new_name,
                                                     module_imports)
            if changed:
                source = module_imports.get_changed_source()
                source = self.tools.new_source(pymodule, source)
                pymodule = self.tools.new_pymodule(pymodule, source)

        new_import = self._new_import(dest)
        source = self.tools.rename_in_module(
            new_name, imports=True, pymodule=pymodule,
            resource=resource if not changed else None)
        should_import = self.tools.occurs_in_module(
            pymodule=pymodule, resource=resource, imports=False)
        pymodule = self.tools.new_pymodule(pymodule, source)
        source = self.tools.remove_old_imports(pymodule)
        if should_import:
            pymodule = self.tools.new_pymodule(pymodule, source)
            source = self.tools.add_imports(pymodule, [new_import])
        source = self.tools.new_source(pymodule, source)
        if source is not None and source != pymodule.resource.read():
            return source
        return None
예제 #2
0
    def _change_occurrences_in_module(self, dest, pymodule=None, resource=None):
        if not self.tools.occurs_in_module(pymodule=pymodule, resource=resource):
            return
        if pymodule is None:
            pymodule = self.project.get_pymodule(resource)
        new_name = self._new_modname(dest)
        module_imports = importutils.get_module_imports(self.project, pymodule)
        changed = False
        source = None
        if libutils.modname(dest):
            changed = self._change_import_statements(dest, new_name, module_imports)
            if changed:
                source = module_imports.get_changed_source()
                source = self.tools.new_source(pymodule, source)
                pymodule = self.tools.new_pymodule(pymodule, source)

        new_import = self._new_import(dest)
        source = self.tools.rename_in_module(
            new_name, imports=True, pymodule=pymodule, resource=resource if not changed else None
        )
        should_import = self.tools.occurs_in_module(pymodule=pymodule, resource=resource, imports=False)
        pymodule = self.tools.new_pymodule(pymodule, source)
        source = self.tools.remove_old_imports(pymodule)
        if should_import:
            pymodule = self.tools.new_pymodule(pymodule, source)
            source = self.tools.add_imports(pymodule, [new_import])
        source = self.tools.new_source(pymodule, source)
        if source is not None and source != pymodule.resource.read():
            return source
        return None
예제 #3
0
파일: inline.py 프로젝트: AndreiRO/vim
def _add_imports(pycore, source, resource, imports):
    if not imports:
        return source
    pymodule = pycore.get_string_module(source, resource)
    module_import = importutils.get_module_imports(pycore, pymodule)
    for import_info in imports:
        module_import.add_import(import_info)
    source = module_import.get_changed_source()
    pymodule = pycore.get_string_module(source, resource)
    import_tools = importutils.ImportTools(pycore)
    return import_tools.organize_imports(pymodule, unused=False, sort=False)
예제 #4
0
def _add_imports(project, source, resource, imports):
    if not imports:
        return source
    pymodule = libutils.get_string_module(project, source, resource)
    module_import = importutils.get_module_imports(project, pymodule)
    for import_info in imports:
        module_import.add_import(import_info)
    source = module_import.get_changed_source()
    pymodule = libutils.get_string_module(project, source, resource)
    import_tools = importutils.ImportTools(project)
    return import_tools.organize_imports(pymodule, unused=False, sort=False)
예제 #5
0
 def _add_imports(self, source, file):
     if not self.imports:
         return source
     pymodule = self.pycore.get_string_module(source, file)
     module_import = importutils.get_module_imports(self.pycore, pymodule)
     for import_info in self.imports:
         module_import.add_import(import_info)
     source = module_import.get_changed_source()
     pymodule = self.pycore.get_string_module(source, file)
     source = self.import_tools.organize_imports(
         pymodule, unused=False, sort=False)
     return source
예제 #6
0
 def find_insertion_line(self, code):
     """Guess at what line the new import should be inserted"""
     match = re.search(r'^(def|class)\s+', code)
     if match is not None:
         code = code[:match.start()]
     try:
         pymodule = libutils.get_string_module(self.project, code)
     except exceptions.ModuleSyntaxError:
         return 1
     testmodname = '__rope_testmodule_rope'
     importinfo = importutils.NormalImport(((testmodname, None), ))
     module_imports = importutils.get_module_imports(self.project, pymodule)
     module_imports.add_import(importinfo)
     code = module_imports.get_changed_source()
     offset = code.index(testmodname)
     lineno = code.count('\n', 0, offset) + 1
     return lineno
예제 #7
0
 def find_insertion_line(self, code):
     """Guess at what line the new import should be inserted"""
     match = re.search(r'^(def|class)\s+', code)
     if match is not None:
         code = code[:match.start()]
     try:
         pymodule = self.project.pycore.get_string_module(code)
     except exceptions.ModuleSyntaxError:
         return 1
     testmodname = '__rope_testmodule_rope'
     importinfo = importutils.NormalImport(((testmodname, None),))
     module_imports = importutils.get_module_imports(
         self.project.pycore, pymodule)
     module_imports.add_import(importinfo)
     code = module_imports.get_changed_source()
     offset = code.index(testmodname)
     lineno = code.count('\n', 0, offset) + 1
     return lineno
예제 #8
0
파일: inline.py 프로젝트: AndreiRO/vim
def _remove_from(pycore, pyname, source, resource):
    pymodule = pycore.get_string_module(source, resource)
    module_import = importutils.get_module_imports(pycore, pymodule)
    module_import.remove_pyname(pyname)
    return module_import.get_changed_source()
예제 #9
0
def _remove_from(project, pyname, source, resource):
    pymodule = libutils.get_string_module(project, source, resource)
    module_import = importutils.get_module_imports(project, pymodule)
    module_import.remove_pyname(pyname)
    return module_import.get_changed_source()