Beispiel #1
0
 def get_changes(self, classname=None, new_class_name=None):
     if new_class_name is not None:
         warnings.warn(
             "new_class_name parameter is deprecated; use classname",
             DeprecationWarning,
             stacklevel=2,
         )
         classname = new_class_name
     collector = codeanalyze.ChangeCollector(self.pymodule.source_code)
     start, end = sourceutils.get_body_region(self.pyfunction)
     indents = sourceutils.get_indents(
         self.pymodule.lines,
         self.pyfunction.get_scope().get_start()) + sourceutils.get_indent(
             self.project)
     new_contents = " " * indents + "return %s(%s)()\n" % (
         classname,
         ", ".join(self._get_parameter_names()),
     )
     collector.add_change(start, end, new_contents)
     insertion = self._get_class_insertion_point()
     collector.add_change(insertion, insertion,
                          "\n\n" + self.get_new_class(classname))
     changes = change.ChangeSet(
         "Replace method with method object refactoring")
     changes.add_change(
         change.ChangeContents(self.resource, collector.get_changed()))
     return changes
Beispiel #2
0
 def get_insertion_offsets(self):
     if self.goal_scope.get_kind() == "Class":
         start, end = sourceutils.get_body_region(self.goal_scope.pyobject)
         if self.goal_pymodule.source_code[start:end].strip() == "pass":
             return start, end
     lines = self.goal_pymodule.lines
     start = lines.get_line_start(self.get_insertion_lineno())
     return (start, start)
Beispiel #3
0
 def _get_changes_made_by_old_class(self, dest_attr, new_name):
     pymodule = self.pyfunction.get_module()
     indents = self._get_scope_indents(self.pyfunction)
     body = 'return self.%s.%s(%s)\n' % (
         dest_attr, new_name, self._get_passed_arguments_string())
     region = sourceutils.get_body_region(self.pyfunction)
     return (pymodule.get_resource(), region[0], region[1],
             sourceutils.fix_indentation(body, indents))
Beispiel #4
0
 def get_insertion_offsets(self):
     if self.goal_scope.get_kind() == 'Class':
         start, end = sourceutils.get_body_region(self.goal_scope.pyobject)
         if self.goal_pymodule.source_code[start:end].strip() == 'pass':
             return start, end
     lines = self.goal_pymodule.lines
     start = lines.get_line_start(self.get_insertion_lineno())
     return (start, start)
Beispiel #5
0
 def _get_changes_made_by_old_class(self, dest_attr, new_name):
     pymodule = self.pyfunction.get_module()
     indents = self._get_scope_indents(self.pyfunction)
     body = 'return self.%s.%s(%s)\n' % (
         dest_attr, new_name, self._get_passed_arguments_string())
     region = sourceutils.get_body_region(self.pyfunction)
     return (pymodule.get_resource(), region[0], region[1],
             sourceutils.fix_indentation(body, indents))
Beispiel #6
0
 def _is_the_last_method_of_a_class(self):
     pyclass = self.pyfunction.parent
     if not isinstance(pyclass, pyobjects.PyClass):
         return False
     class_start, class_end = sourceutils.get_body_region(pyclass)
     source = self.pymodule.source_code
     func_start, func_end = self._get_scope_range()
     if source[class_start:func_start].strip() == "" and source[func_end:class_end].strip() == "":
         return True
     return False
Beispiel #7
0
 def _is_the_last_method_of_a_class(self):
     pyclass = self.pyfunction.parent
     if not isinstance(pyclass, pyobjects.PyClass):
         return False
     class_start, class_end = sourceutils.get_body_region(pyclass)
     source = self.pymodule.source_code
     func_start, func_end = self._get_scope_range()
     if source[class_start:func_start].strip() == '' and \
        source[func_end:class_end].strip() == '':
         return True
     return False
Beispiel #8
0
    def _restructure(self, resources, task_handle, others=True):
        pattern = self._make_pattern()
        goal = self._make_goal(import_=others)
        imports = None
        if others:
            imports = ["import %s" % self._module_name()]

        body_region = sourceutils.get_body_region(self.pyfunction)
        args_value = {"skip": (self.resource, body_region)}
        args = {"": args_value}

        restructuring = restructure.Restructure(self.project, pattern, goal, args=args, imports=imports)
        return restructuring.get_changes(resources=resources, task_handle=task_handle)
Beispiel #9
0
 def get_changes(self, new_parameter):
     definition_info = functionutils.DefinitionInfo.read(self.pyfunction)
     definition_info.args_with_defaults.append((new_parameter, self._get_primary()))
     collector = codeanalyze.ChangeCollector(self.resource.read())
     header_start, header_end = self._get_header_offsets()
     body_start, body_end = sourceutils.get_body_region(self.pyfunction)
     collector.add_change(header_start, header_end, definition_info.to_string())
     self._change_function_occurrences(
         collector, body_start, body_end, new_parameter
     )
     changes = rope.base.change.ChangeSet("Introduce parameter <%s>" % new_parameter)
     change = rope.base.change.ChangeContents(self.resource, collector.get_changed())
     changes.add_change(change)
     return changes
Beispiel #10
0
    def _restructure(self, resources, task_handle, others=True):
        pattern = self._make_pattern()
        goal = self._make_goal(import_=others)
        imports = None
        if others:
            imports = ['import %s' % self._module_name()]

        body_region = sourceutils.get_body_region(self.pyfunction)
        args_value = {'skip': (self.resource, body_region)}
        args = {'': args_value}

        restructuring = restructure.Restructure(
            self.project, pattern, goal, args=args, imports=imports)
        return restructuring.get_changes(resources=resources,
                                         task_handle=task_handle)
 def get_changes(self, new_parameter):
     definition_info = functionutils.DefinitionInfo.read(self.pyfunction)
     definition_info.args_with_defaults.append((new_parameter,
                                                self._get_primary()))
     collector = codeanalyze.ChangeCollector(self.resource.read())
     header_start, header_end = self._get_header_offsets()
     body_start, body_end = sourceutils.get_body_region(self.pyfunction)
     collector.add_change(header_start, header_end,
                          definition_info.to_string())
     self._change_function_occurances(collector, body_start,
                                      body_end, new_parameter)
     changes = rope.base.change.ChangeSet('Introduce parameter <%s>' %
                                          new_parameter)
     change = rope.base.change.ChangeContents(self.resource,
                                              collector.get_changed())
     changes.add_change(change)
     return changes
Beispiel #12
0
 def _get_changes_made_by_new_class(self, dest_attr, new_name):
     old_pyclass = self.pyfunction.parent
     if dest_attr not in old_pyclass:
         raise exceptions.RefactoringError("Destination attribute <%s> not found" % dest_attr)
     pyclass = old_pyclass[dest_attr].get_object().get_type()
     if not isinstance(pyclass, pyobjects.PyClass):
         raise exceptions.RefactoringError("Unknown class type for attribute <%s>" % dest_attr)
     pymodule = pyclass.get_module()
     resource = pyclass.get_module().get_resource()
     start, end = sourceutils.get_body_region(pyclass)
     pre_blanks = "\n"
     if pymodule.source_code[start:end].strip() != "pass":
         pre_blanks = "\n\n"
         start = end
     indents = self._get_scope_indents(pyclass)
     body = pre_blanks + sourceutils.fix_indentation(self.get_new_method(new_name), indents)
     return resource, start, end, body
Beispiel #13
0
 def _get_changes_made_by_new_class(self, dest_attr, new_name):
     old_pyclass = self.pyfunction.parent
     if dest_attr not in old_pyclass:
         raise exceptions.RefactoringError(
             'Destination attribute <%s> not found' % dest_attr)
     pyclass = old_pyclass[dest_attr].get_object().get_type()
     if not isinstance(pyclass, pyobjects.PyClass):
         raise exceptions.RefactoringError(
             'Unknown class type for attribute <%s>' % dest_attr)
     pymodule = pyclass.get_module()
     resource = pyclass.get_module().get_resource()
     start, end = sourceutils.get_body_region(pyclass)
     pre_blanks = '\n'
     if pymodule.source_code[start:end].strip() != 'pass':
         pre_blanks = '\n\n'
         start = end
     indents = self._get_scope_indents(pyclass)
     body = pre_blanks + sourceutils.fix_indentation(
         self.get_new_method(new_name), indents)
     return resource, start, end, body
    def _change_holding_module(self, changes, renamer, getter, setter):
        pymodule = self.pycore.resource_to_pyobject(self.resource)
        class_scope = self._get_defining_class_scope()
        defining_object = self._get_defining_scope().pyobject
        start, end = sourceutils.get_body_region(defining_object)

        new_source = renamer.get_changed_module(pymodule=pymodule,
                                                skip_start=start, skip_end=end)
        if new_source is not None:
            pymodule = self.pycore.get_string_module(new_source, self.resource)
            class_scope = pymodule.get_scope().\
                          get_inner_scope_for_line(class_scope.get_start())
        indents = sourceutils.get_indent(self.pycore) * ' '
        getter = 'def %s(self):\n%sreturn self.%s' % \
                 (getter, indents, self.name)
        setter = 'def %s(self, value):\n%sself.%s = value' % \
                 (setter, indents, self.name)
        new_source = sourceutils.add_methods(pymodule, class_scope,
                                             [getter, setter])
        return new_source
Beispiel #15
0
 def get_changes(self, classname=None, new_class_name=None):
     if new_class_name is not None:
         warnings.warn(
             'new_class_name parameter is deprecated; use classname',
             DeprecationWarning, stacklevel=2)
         classname = new_class_name
     collector = codeanalyze.ChangeCollector(self.pymodule.source_code)
     start, end = sourceutils.get_body_region(self.pyfunction)
     indents = sourceutils.get_indents(
         self.pymodule.lines, self.pyfunction.get_scope().get_start()) + \
         sourceutils.get_indent(self.pycore)
     new_contents = ' ' * indents + 'return %s(%s)()\n' % \
                    (classname, ', '.join(self._get_parameter_names()))
     collector.add_change(start, end, new_contents)
     insertion = self._get_class_insertion_point()
     collector.add_change(insertion, insertion,
                          '\n\n' + self.get_new_class(classname))
     changes = change.ChangeSet('Replace method with method object refactoring')
     changes.add_change(change.ChangeContents(self.resource,
                                              collector.get_changed()))
     return changes