示例#1
0
def moving_code_with_imports(project, resource, source):
    import_tools = importutils.ImportTools(project)
    pymodule = libutils.get_string_module(project, source, resource)
    origin = project.get_pymodule(resource)

    imports = []
    for stmt in import_tools.module_imports(origin).imports:
        imports.append(stmt.import_info)

    back_names = []
    for name in origin:
        if name not in pymodule:
            back_names.append(name)
    imports.append(import_tools.get_from_import(resource, back_names))

    source = _add_imports_to_module(import_tools, pymodule, imports)
    pymodule = libutils.get_string_module(project, source, resource)

    source = import_tools.relatives_to_absolutes(pymodule)
    pymodule = libutils.get_string_module(project, source, resource)
    source = import_tools.organize_imports(pymodule, selfs=False)
    pymodule = libutils.get_string_module(project, source, resource)

    # extracting imports after changes
    module_imports = import_tools.module_imports(pymodule)
    imports = [import_stmt.import_info
               for import_stmt in module_imports.imports]
    start = 1
    if module_imports.imports:
        start = module_imports.imports[-1].end_line
    lines = codeanalyze.SourceLinesAdapter(source)
    while start < lines.length() and not lines.get_line(start).strip():
        start += 1
    moving = source[lines.get_line_start(start):]
    return moving, imports
示例#2
0
文件: generate.py 项目: climbus/rope
def _add_relative_import_to_module(project, resource, imported, name):
    pymodule = project.get_pymodule(resource)
    import_tools = importutils.ImportTools(project)
    module_imports = import_tools.module_imports(pymodule)
    new_import = import_tools.get_from_import(imported, name)
    module_imports.add_import(new_import)
    return change.ChangeContents(resource, module_imports.get_changed_source())
示例#3
0
def _insert_import(name, module, ctx):
    if not ctx.resource:
        source, _ = env.get_offset_params()
        lineno = ctx.importer.find_insertion_line(source)
        line = 'from %s import %s' % (module, name)
        env.curbuf[lineno - 1:lineno -
                   1] = [env.prepare_value(line, dumps=False)]
        return True

    pyobject = ctx.project.pycore.resource_to_pyobject(ctx.resource)
    import_tools = importutils.ImportTools(ctx.project)
    module_imports = import_tools.module_imports(pyobject)
    new_import = importutils.FromImport(module, 0, [[name, None]])
    module_imports.add_import(new_import)
    changes = change.ChangeContents(ctx.resource,
                                    module_imports.get_changed_source())

    action = env.user_input_choices('Choose what to do:', 'perform', 'preview')

    if not action:
        return False

    if action == 'preview':
        print("\n   ")
        print("-------------------------------")
        print("\n%s\n" % changes.get_description())
        print("-------------------------------\n\n")
        if not env.user_confirm('Do the changes?'):
            return False

    progress = ProgressHandler('Apply changes ...')
    ctx.project.do(changes, task_handle=progress.handle)
    reload_changes(changes)
示例#4
0
文件: generate.py 项目: climbus/rope
def _add_import_to_module(project, resource, imported):
    pymodule = project.get_pymodule(resource)
    import_tools = importutils.ImportTools(project)
    module_imports = import_tools.module_imports(pymodule)
    module_name = libutils.modname(imported)
    new_import = importutils.NormalImport(((module_name, None), ))
    module_imports.add_import(new_import)
    return change.ChangeContents(resource, module_imports.get_changed_source())
示例#5
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)
示例#6
0
    def get_changes(
            self,
            dest_attr,
            new_name=None,
            resources=None,
            task_handle=taskhandle.NullTaskHandle(),
    ):
        """Return the changes needed for this refactoring

        Parameters:

        - `dest_attr`: the name of the destination attribute
        - `new_name`: the name of the new method; if `None` uses
          the old name
        - `resources` can be a list of `rope.base.resources.File` to
          apply this refactoring on.  If `None`, the restructuring
          will be applied to all python files.

        """
        changes = ChangeSet("Moving method <%s>" % self.method_name)
        if resources is None:
            resources = self.project.get_python_files()
        if new_name is None:
            new_name = self.get_method_name()
        resource1, start1, end1, new_content1 = self._get_changes_made_by_old_class(
            dest_attr, new_name)
        collector1 = codeanalyze.ChangeCollector(resource1.read())
        collector1.add_change(start1, end1, new_content1)

        resource2, start2, end2, new_content2 = self._get_changes_made_by_new_class(
            dest_attr, new_name)
        if resource1 == resource2:
            collector1.add_change(start2, end2, new_content2)
        else:
            collector2 = codeanalyze.ChangeCollector(resource2.read())
            collector2.add_change(start2, end2, new_content2)
            result = collector2.get_changed()
            import_tools = importutils.ImportTools(self.project)
            new_imports = self._get_used_imports(import_tools)
            if new_imports:
                goal_pymodule = libutils.get_string_module(
                    self.project, result, resource2)
                result = _add_imports_to_module(import_tools, goal_pymodule,
                                                new_imports)
            if resource2 in resources:
                changes.add_change(ChangeContents(resource2, result))

        if resource1 in resources:
            changes.add_change(
                ChangeContents(resource1, collector1.get_changed()))
        return changes
示例#7
0
def moving_code_with_imports(project, resource, source):
    import_tools = importutils.ImportTools(project)
    pymodule = libutils.get_string_module(project, source, resource)

    # Strip comment prefix, if any. These need to stay before the moving
    # section, but imports would be added between them.
    lines = codeanalyze.SourceLinesAdapter(source)
    start = 1
    while start < lines.length() and lines.get_line(start).startswith('#'):
        start += 1
    moving_prefix = source[:lines.get_line_start(start)]
    pymodule = libutils.get_string_module(project,
                                          source[lines.get_line_start(start):],
                                          resource)

    origin = project.get_pymodule(resource)

    imports = []
    for stmt in import_tools.module_imports(origin).imports:
        imports.append(stmt.import_info)

    back_names = []
    for name in origin:
        if name not in pymodule:
            back_names.append(name)
    imports.append(import_tools.get_from_import(resource, back_names))

    source = _add_imports_to_module(import_tools, pymodule, imports)
    pymodule = libutils.get_string_module(project, source, resource)

    source = import_tools.relatives_to_absolutes(pymodule)
    pymodule = libutils.get_string_module(project, source, resource)
    source = import_tools.organize_imports(pymodule, selfs=False)
    pymodule = libutils.get_string_module(project, source, resource)

    # extracting imports after changes
    module_imports = import_tools.module_imports(pymodule)
    imports = [
        import_stmt.import_info for import_stmt in module_imports.imports
    ]
    start = 1
    if module_imports.imports:
        start = module_imports.imports[-1].end_line
    lines = codeanalyze.SourceLinesAdapter(source)
    while start < lines.length() and not lines.get_line(start).strip():
        start += 1

    # Reinsert the prefix which was removed at the beginning
    moving = moving_prefix + source[lines.get_line_start(start):]
    return moving, imports
示例#8
0
 def __init__(self, project, source, pyname, old_name):
     self.project = project
     self.source = source
     self.old_pyname = pyname
     self.old_name = old_name
     self.import_tools = importutils.ImportTools(self.project)
 def __init__(self, pycore, source, pyname, old_name):
     self.pycore = pycore
     self.source = source
     self.old_pyname = pyname
     self.old_name = old_name
     self.import_tools = importutils.ImportTools(self.pycore)