Beispiel #1
0
 def occurred_outside_skip(self, change_collector, occurrence):
     start, end = occurrence.get_primary_range()
     if not occurrence.is_called():
         raise rope.base.exceptions.RefactoringError(
             'Reference to inlining function other than function call'
             ' in <file: %s, offset: %d>' % (self.resource.path, start))
     if self.aim is not None and (self.aim < start or self.aim > end):
         return
     end_parens = self._find_end_parens(self.source, end - 1)
     lineno = self.lines.get_line_number(start)
     start_line, end_line = self.pymodule.logical_lines.\
                            logical_line_in(lineno)
     line_start = self.lines.get_line_start(start_line)
     line_end = self.lines.get_line_end(end_line)
     returns = self.source[line_start:start].strip() != '' or \
               self.source[end_parens:line_end].strip() != ''
     indents = sourceutils.get_indents(self.lines, start_line)
     primary, pyname = occurrence.get_primary_and_pyname()
     definition, returned = self.generator.get_definition(
         primary, pyname, self.source[start:end_parens], returns=returns)
     end = min(line_end + 1, len(self.source))
     change_collector.add_change(
         line_start, end, sourceutils.fix_indentation(definition, indents))
     if returns:
         name = returned
         if name is None:
             name = 'None'
         change_collector.add_change(
             line_end, end, self.source[line_start:start] + name +
             self.source[end_parens:end])
Beispiel #2
0
 def get_new_class(self, name):
     body = sourceutils.fix_indentation(
         self._get_body(),
         sourceutils.get_indent(self.pycore) * 2)
     return 'class %s(object):\n\n%s%sdef __call__(self):\n%s' % \
            (name, self._get_init(),
             ' ' * sourceutils.get_indent(self.pycore), body)
Beispiel #3
0
 def occurred_outside_skip(self, change_collector, occurrence):
     start, end = occurrence.get_primary_range()
     # we remove out of date imports later
     if occurrence.is_in_import_statement():
         return
     # the function is referenced outside an import statement
     if not occurrence.is_called():
         raise rope.base.exceptions.RefactoringError(
             'Reference to inlining function other than function call'
             ' in <file: %s, offset: %d>' % (self.resource.path, start))
     if self.aim is not None and (self.aim < start or self.aim > end):
         return
     end_parens = self._find_end_parens(self.source, end - 1)
     lineno = self.lines.get_line_number(start)
     start_line, end_line = self.pymodule.logical_lines.\
                            logical_line_in(lineno)
     line_start = self.lines.get_line_start(start_line)
     line_end = self.lines.get_line_end(end_line)
     returns = self.source[line_start:start].strip() != '' or \
               self.source[end_parens:line_end].strip() != ''
     indents = sourceutils.get_indents(self.lines, start_line)
     primary, pyname = occurrence.get_primary_and_pyname()
     definition, returned = self.generator.get_definition(
         primary, pyname, self.source[start:end_parens], returns=returns)
     end = min(line_end + 1, len(self.source))
     change_collector.add_change(
         line_start, end, sourceutils.fix_indentation(definition, indents))
     if returns:
         name = returned
         if name is None:
             name = 'None'
         change_collector.add_change(
             line_end, end, self.source[line_start:start] + name +
             self.source[end_parens:end])
Beispiel #4
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 #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 _get_unindented_function_body(self, returns):
     if self.info.one_line:
         return 'return ' + _join_lines(self.info.extracted)
     extracted_body = self.info.extracted
     unindented_body = sourceutils.fix_indentation(extracted_body, 0)
     if returns:
         unindented_body += '\nreturn %s' % self._get_comma_form(returns)
     return unindented_body
Beispiel #7
0
 def get_new_class(self, name):
     body = sourceutils.fix_indentation(
         self._get_body(),
         sourceutils.get_indent(self.project) * 2)
     return "class %s(object):\n\n%s%sdef __call__(self):\n%s" % (
         name,
         self._get_init(),
         " " * sourceutils.get_indent(self.project),
         body,
     )
Beispiel #8
0
 def extract(self):
     extract_info = self._collect_info()
     content = codeanalyze.ChangeCollector(self.info.source)
     definition = extract_info.definition
     lineno, indents = extract_info.definition_location
     offset = self.info.lines.get_line_start(lineno)
     indented = sourceutils.fix_indentation(definition, indents)
     content.add_change(offset, offset, indented)
     self._replace_occurrences(content, extract_info)
     return content.get_changed()
Beispiel #9
0
    def get_changes(self):
        changes = change.ChangeSet('Generate %s <%s>' %
                                   (self._get_element_kind(), self.name))
        indents = self.info.get_scope_indents()
        blanks = self.info.get_blank_lines()
        base_definition = sourceutils.fix_indentation(self._get_element(), indents)
        definition = '\n' * blanks[0] + base_definition + '\n' * blanks[1]

        resource = self.info.get_insertion_resource()
        start, end = self.info.get_insertion_offsets()

        collector = codeanalyze.ChangeCollector(resource.read())
        collector.add_change(start, end, definition)
        changes.add_change(change.ChangeContents(
                           resource, collector.get_changed()))
        return changes
Beispiel #10
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 #11
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 #12
0
    def get_changes(self):
        changes = change.ChangeSet("Generate %s <%s>" %
                                   (self._get_element_kind(), self.name))
        indents = self.info.get_scope_indents()
        blanks = self.info.get_blank_lines()
        base_definition = sourceutils.fix_indentation(self._get_element(),
                                                      indents)
        definition = "\n" * blanks[0] + base_definition + "\n" * blanks[1]

        resource = self.info.get_insertion_resource()
        start, end = self.info.get_insertion_offsets()

        collector = codeanalyze.ChangeCollector(resource.read())
        collector.add_change(start, end, definition)
        changes.add_change(
            change.ChangeContents(resource, collector.get_changed()))
        if self.goal_resource:
            relative_import = _add_relative_import_to_module(
                self.project, self.resource, self.goal_resource, self.name)
            changes.add_change(relative_import)
        return changes
Beispiel #13
0
    def occurred_outside_skip(self, change_collector, occurrence):
        start, end = occurrence.get_primary_range()
        # we remove out of date imports later
        if occurrence.is_in_import_statement():
            return
        # the function is referenced outside an import statement
        if not occurrence.is_called():
            raise rope.base.exceptions.RefactoringError(
                "Reference to inlining function other than function call"
                " in <file: %s, offset: %d>" % (self.resource.path, start)
            )
        if self.aim is not None and (self.aim < start or self.aim > end):
            return
        end_parens = self._find_end_parens(self.source, end - 1)
        lineno = self.lines.get_line_number(start)
        start_line, end_line = self.pymodule.logical_lines.logical_line_in(lineno)
        line_start = self.lines.get_line_start(start_line)
        line_end = self.lines.get_line_end(end_line)

        returns = self.source[line_start:start].strip() != "" or self.source[end_parens:line_end].strip() != ""
        indents = sourceutils.get_indents(self.lines, start_line)
        primary, pyname = occurrence.get_primary_and_pyname()

        host = self.pymodule
        scope = host.scope.get_inner_scope_for_line(lineno)
        definition, returned = self.generator.get_definition(
            primary, pyname, self.source[start:end_parens], scope.get_names(), returns=returns
        )

        end = min(line_end + 1, len(self.source))
        change_collector.add_change(line_start, end, sourceutils.fix_indentation(definition, indents))
        if returns:
            name = returned
            if name is None:
                name = "None"
            change_collector.add_change(
                line_end, end, self.source[line_start:start] + name + self.source[end_parens:end]
            )
Beispiel #14
0
def _parse_text(body):
    body = sourceutils.fix_indentation(body, 0)
    node = ast.parse(body)
    return node
Beispiel #15
0
 def get_new_class(self, name):
     body = sourceutils.fix_indentation(
         self._get_body(), sourceutils.get_indent(self.pycore) * 2)
     return 'class %s(object):\n\n%s%sdef __call__(self):\n%s' % \
            (name, self._get_init(),
             ' ' * sourceutils.get_indent(self.pycore), body)
Beispiel #16
0
 def _get_body(self):
     result = sourceutils.fix_indentation(self.info.extracted, 0)
     if self.info.one_line:
         result = '(%s)' % result
     return result
Beispiel #17
0
Datei: move.py Projekt: Kha/rope
 def get_new_method(self, name):
     return '%s\n%s' % (
         self._get_new_header(name),
         sourceutils.fix_indentation(self._get_body(),
                                     sourceutils.get_indent(self.pycore)))
Beispiel #18
0
 def get_new_method(self, name):
     return "%s\n%s" % (
         self._get_new_header(name),
         sourceutils.fix_indentation(self._get_body(), sourceutils.get_indent(self.project)),
     )
Beispiel #19
0
 def get_new_method(self, name):
     return '%s\n%s' % (self._get_new_header(name),
                        sourceutils.fix_indentation(
                            self._get_body(),
                            sourceutils.get_indent(self.project)))