Beispiel #1
0
 def refactor_create_inline(self, offset, only_this):
     """Inline the function call at point."""
     refactor = create_inline(self.project, self.resource, offset)
     if only_this:
         return self._get_changes(refactor, remove=False, only_current=True)
     else:
         return self._get_changes(refactor, remove=True, only_current=False)
Beispiel #2
0
    def get_refactor(ctx):
        """ Function description.

        :return Rename:

        """
        _, offset = env.get_offset_params()
        return inline.create_inline(ctx.project, ctx.resource, offset)
Beispiel #3
0
    def get_refactor(ctx):
        """ Function description.

        :return Rename:

        """
        _, offset = env.get_offset_params()
        return inline.create_inline(ctx.project, ctx.resource, offset)
Beispiel #4
0
 def onRefactor(self):
     renamed = create_inline(self.project, self.resource, self._startOffset)
     changes = renamed.get_changes(resources=[self.resource], task_handle=self._handle)
     for item in changes.changes:
         if isinstance(item, rope.base.change.ChangeContents):
             self.changes.append(
                 Change(item.resource.real_path, ChangeType.EDIT, get_diff(item)))
         else:
             raise Exception('Unknown Change')
Beispiel #5
0
def inline(inline_fns, dirname, filename):
    project = Project(dirname)
    resource = project.get_resource(filename)
    for inline_fn in inline_fns:
        try:  # FIXME: temporarily handling not implemented error
            changes = create_inline(
                project, resource,
                resource.read().index(inline_fn)).get_changes(remove=True)
            project.do(changes)
        except Exception as e:
            pass
Beispiel #6
0
 def run(self):
     self.error = None
     self.changedFiles = []
     try:
         inlined = inline.create_inline(
             self.ropeProject, self.resource, self.offset)
         changes = inlined.get_changes()
         self.ropeProject.do(changes)
         changed = changes.get_changed_resources()
         # changed is a set
         for i in changed:
             self.changedFiles.append(i.real_path)
     except Exception as err:
         self.error = str(err)
Beispiel #7
0
 def run(self):
     self.error = None
     self.changedFiles = []
     try:
         inlined = inline.create_inline(self.ropeProject, self.resource,
                                        self.offset)
         changes = inlined.get_changes()
         self.ropeProject.do(changes)
         changed = changes.get_changed_resources()
         # changed is a set
         for i in changed:
             self.changedFiles.append(i.real_path)
     except Exception as err:
         self.error = str(err)
Beispiel #8
0
 def run(self):
     self.error = None
     self.changedFiles = []
     try:
         inlined = inline.create_inline(
             self.ropeProject, self.resource, self.offset)
         changes = inlined.get_changes()
         self.ropeProject.do(changes)
         changed = changes.get_changed_resources()
         # changed is a set
         for i in changed:
             self.changedFiles.append(i.real_path)
     except Exception as err:
         exc_type, exc_value, exc_traceback = sys.exc_info()
         logging.error(repr(traceback.format_exception(exc_type, exc_value,
                      exc_traceback)))
         
         self.error = str(err)
Beispiel #9
0
 def run(self):
     self.error = None
     self.changedFiles = []
     try:
         inlined = inline.create_inline(
             self.ropeProject, self.resource, self.offset)
         changes = inlined.get_changes()
         self.ropeProject.do(changes)
         changed = changes.get_changed_resources()
         # changed is a set
         for i in changed:
             self.changedFiles.append(i.real_path)
     except Exception as err:
         exc_type, exc_value, exc_traceback = sys.exc_info()
         logging.error(repr(traceback.format_exception(exc_type, exc_value,
                      exc_traceback)))
         
         self.error = str(err)
Beispiel #10
0
 def _inline2(self, resource, offset, **kwds):
     inliner = inline.create_inline(self.project, resource, offset)
     changes = inliner.get_changes(**kwds)
     self.project.do(changes)
     return self.mod.read()
Beispiel #11
0
def compile_with_top(source_path, api_path):
    # parse the inputs to get the path subcomponents
    source_path = op.abspath(source_path)
    api_path = op.abspath(api_path)
    api_root, api_name = op.split(api_path)
    api_module = op.basename(op.normpath(api_root))
    source_root, source_name = op.split(source_path)

    # Create the two projects that we will be using
    from rope.base.project import Project
    project_const = Project(source_root, **prefs)  # original project, used as a source and will not be modified
    project_swap = Project(op.join(source_root, 'top_compiled'), **prefs)

    if(op.isdir(source_path)):
        pass

    # copy over the files that will be compiled
    source_mod = project_const.root.get_child(source_name)
    try:
        mod_code = project_swap.root.get_child(source_name)
    except ResourceNotFoundError:
        mod_code = project_swap.root.create_file(source_name)

    mod_code.write(source_mod.read())

    # Generate the api library package and copy it over
    from rope.contrib import generate
    api_pkg = None
    if api_root != source_root: # If the api is in its own module, reproduce it
        try:
            api_pkg = project_swap.root.get_child(api_module)
        except ResourceNotFoundError:
            api_pkg = generate.create_package(project_swap, api_module)
    try:
        mod_api = project_swap.root.get_child('{}/{}'.format(api_module, api_name))
    except ResourceNotFoundError:
        mod_api = generate.create_module(project_swap, 'api', api_pkg)
    # copy over the contents of the api so that rope can modify it
    with open(api_path, 'r') as a:
        api_content = a.read()

    # mod_api.truncate().write(api_content)
    mod_api.write(api_content)

    # inline each API call
    # first, get the list of all API function calls possible
    import imp, types
    api = imp.load_source(api_name, api_path)
    api_calls = [a for a in dir(api) if isinstance(api.__dict__.get(a), types.FunctionType)]
    # perform the replacement
    for a in api_calls:
        # We did not use this API call in the code, so skip replacing it
        if re.search(r'\b{}\b'.format(a), mod_code.read()) is None:
            continue
        # print re.search(r'\b{}\b'.format(a), mod_code.read())
        ind = re.search(r'\b{}\b'.format(a), mod_code.read()).start()
        try:
            inl = inline.create_inline(project_swap, mod_code, ind)
            change = inl.get_changes()
            project_const.do(change)
        except RefactoringError:
            print "The api cannot be properly connected from the code. Please ensure that you are importing the file with the API commands directly."

    # Cleaning up
    api_pkg.remove() # remove the API library from the compiled files
    project_const.close()
    project_swap.close()
Beispiel #12
0
 def _inline2(self, resource, offset, **kwds):
     inliner = inline.create_inline(self.project, resource, offset)
     changes = inliner.get_changes(**kwds)
     self.project.do(changes)
     return self.mod.read()
Beispiel #13
0
def inline(source_string, offset):
    project, resource = make_temporary_project_and_resource(source_string)
    inliner = create_inline(project, resource, offset)
    changes = inliner.get_changes()

    return get_result(changes, project, resource)