예제 #1
0
def main():
    from rope.base.project import Project
    from rope.base import libutils
    from rope.refactor.importutils import ImportOrganizer
    from rope.base.exceptions import RopeError

    path = editor.get_path()
    selection = editor.get_selection()

    if not path or not selection:
        console.hud_alert('Not a Python file', 'error')
        return

    tab.save()

    project = None
    try:
        project = Project(os.path.dirname(path), ropefolder=None)
        resource = libutils.path_to_resource(project, path)
        if not libutils.is_python_file(project, resource):
            console.hud_alert('Not a Python file', 'error')
            return

        organizer = ImportOrganizer(project)
        change_set = organizer.organize_imports(resource)

        if not change_set:
            console.hud_alert('No changes required')
            return

        if refactoring.ask_if_apply_change_set(change_set):
            refactoring.apply_change_set(change_set, path, selection)
            console.hud_alert('Imports organized')

    except RopeError as e:
        console.hud_alert(str(e), 'error')

    except KeyboardInterrupt:
        pass

    finally:
        if project:
            project.close()
예제 #2
0
def organize_imports(_, project, file_path):
    try:
        project.validate()
        module = libutils.path_to_resource(project, file_path)
        organizer = ImportOrganizer(project)
        pending_changes = organizer.organize_imports(module)
        return False, pending_changes
    except RopeError as e:
        error = RefactoringError()
        error.exc = str(e)
        error.traceback = traceback.format_exc()
        error.critical = False
        return error
    except Exception as e:
        error = RefactoringError()
        error.exc = str(e)
        error.traceback = traceback.format_exc()
        error.critical = True
        return error
예제 #3
0
def organize_imports(_, project, file_path):
    try:
        project.validate()
        module = libutils.path_to_resource(project, file_path)
        organizer = ImportOrganizer(project)
        pending_changes = organizer.organize_imports(module)
        return False, pending_changes
    except RopeError as e:
        error = RefactoringError()
        error.exc = str(e)
        error.traceback = traceback.format_exc()
        error.critical = False
        return error
    except Exception as e:
        error = RefactoringError()
        error.exc = str(e)
        error.traceback = traceback.format_exc()
        error.critical = True
        return error
예제 #4
0
def organize_imports():
    with ropemate.context as context:
        result = context.input
        try:
            organizer = ImportOrganizer(context.project)
            
            operations = [organizer.organize_imports,
                        organizer.handle_long_imports,
                        organizer.expand_star_imports]
            # haven't found a way to easily combine the changes in-memory
            # so i commit all of them and then return the changed file's content
            for op in operations:
                change = op(context.resource)
                if change:
                    context.project.do(change)
            
            with open(context.resource.real_path, "r") as f:
                result = f.read()
        except Exception, e:
            tooltip(e)
        return result
예제 #5
0
파일: refactor.py 프로젝트: exedre/elpy
 def refactor_organize_imports(self):
     """Clean up and organize imports."""
     refactor = ImportOrganizer(self.project)
     changes = refactor.organize_imports(self.resource)
     return translate_changes(changes)
예제 #6
0
파일: refactor.py 프로젝트: exedre/elpy
 def refactor_froms_to_imports(self, offset):
     """Converting imports of the form "from ..." to "import ..."."""
     refactor = ImportOrganizer(self.project)
     changes = refactor.froms_to_imports(self.resource, offset)
     return translate_changes(changes)
예제 #7
0
 def refactor_organize_imports(self):
     """Clean up and organize imports."""
     refactor = ImportOrganizer(self.project)
     changes = refactor.organize_imports(self.resource)
     return translate_changes(changes)
예제 #8
0
 def refactor_froms_to_imports(self, offset):
     """Converting imports of the form "from ..." to "import ..."."""
     refactor = ImportOrganizer(self.project)
     changes = refactor.froms_to_imports(self.resource, offset)
     return translate_changes(changes)
예제 #9
0
 def get_changeset(self):
     import_organizer = ImportOrganizer(self.project)
     return import_organizer.organize_imports(self.resource)