def leave_SimpleString(
     self, original_node: cst.SimpleString, updated_node: cst.SimpleString
 ) -> cst.SimpleString:
     # For prettiness, convert all single-quoted forward refs to double-quoted.
     if updated_node.value.startswith("'") and updated_node.value.endswith("'"):
         return updated_node.with_changes(value=f'"{updated_node.value[1:-1]}"')
     return updated_node
Exemple #2
0
    def update_version(self, original_node: cst.SimpleString,
                       updated_node: cst.SimpleString) -> cst.SimpleString:
        if self.new_version:
            raise Exception("Multiple versions found.")

        old_version = Version(updated_node.evaluated_value)
        self.new_version = self.version_mod(old_version)
        return updated_node.with_changes(value=f'"{self.new_version}"')
    def visit_SimpleString(self, node: cst.SimpleString) -> None:
        matched = re.match(r"^(\'|\")(?P<igcode>IG\d+ )\S", node.value)

        if matched is not None:
            replacement_string = node.value.replace(matched.group("igcode"), "", 1)
            self.report(
                node,
                self.MESSAGE,
                replacement=node.with_changes(value=replacement_string),
            )
Exemple #4
0
 def leave_SimpleString(self, original_node: cst.SimpleString,
                        updated_node: cst.SimpleString) -> cst.SimpleString:
     # For prettiness, convert all single-quoted forward refs to double-quoted.
     if "'" in updated_node.quote:
         new_value = f'"{updated_node.value[1:-1]}"'
         try:
             if updated_node.evaluated_value == ast.literal_eval(new_value):
                 return updated_node.with_changes(value=new_value)
         except SyntaxError:
             pass
     return updated_node
    def leave_SimpleString(self, original_node: cst.SimpleString,
                           updated_node: cst.SimpleString) -> cst.SimpleString:
        if self.avoid_quote in updated_node.quote:
            # Attempt to swap the value out, verify that the string is still identical
            # before and after transformation.
            new_quote = updated_node.quote.replace(self.avoid_quote,
                                                   self.replace_quote)
            new_value = (
                f"{updated_node.prefix}{new_quote}{updated_node.raw_value}{new_quote}"
            )

            try:
                new_str = ast.literal_eval(new_value)
                if updated_node.evaluated_value != new_str:
                    # This isn't the same!
                    return updated_node

                return updated_node.with_changes(value=new_value)
            except Exception:
                # Failed to parse string, changing the quoting screwed us up.
                pass

        # Either failed to parse the new string, or don't need to make changes.
        return updated_node
 def leave_SimpleString(
         self, original_node: cst.SimpleString,
         updated_node: cst.SimpleString) -> cst.SimpleString:
     return updated_node.with_changes(
         value=
         f'"{"".join(reversed(literal_eval(updated_node.value)))}"')
 def leave_string2(
         self, original_node: cst.SimpleString,
         updated_node: cst.SimpleString) -> cst.SimpleString:
     return updated_node.with_changes(
         value=f'"{literal_eval(updated_node.value)}suffix"')
Exemple #8
0
 def build_path_call(self, pattern, other_args):
     """Build the `Call` node using Django 2.0's `path()` function."""
     route = self.build_route(pattern)
     updated_args = (Arg(value=SimpleString(f"'{route}'")), *other_args)
     return Call(args=updated_args, func=Name("path"))
 def leave_SimpleString(self, original_node: cst.SimpleString,
                        updated_node: cst.SimpleString):
     return updated_node.with_changes(
         value="\"[string]\""
     ) if updated_node.value != '"""[docstring]"""' else updated_node
Exemple #10
0
    def leave_Module(self, original_node: "Module",
                     updated_node: "Module") -> "Module":
        if not self.names:
            return original_node

        modified_body = list(original_node.body)

        indented_space = ParenthesizedWhitespace(
            first_line=TrailingWhitespace(
                whitespace=SimpleWhitespace(value=""),
                comment=None,
                newline=Newline(value=None),
            ),
            empty_lines=[],
            indent=True,
            last_line=SimpleWhitespace(value="    "),
        )

        indented_comma = Comma(
            whitespace_before=SimpleWhitespace(value=""),
            whitespace_after=indented_space,
        )
        line_break = ParenthesizedWhitespace(first_line=TrailingWhitespace(
            whitespace=SimpleWhitespace(value=""),
            comment=None,
            newline=Newline(value=None),
        ))

        list_values = [
            Element(SimpleString(value=f'"{global_name}"'),
                    comma=indented_comma) for global_name in self.names[:-1]
        ]
        list_values.append(
            Element(
                SimpleString(value=f'"{self.names[-1]}"'),
                comma=Comma(
                    whitespace_before=SimpleWhitespace(value=""),
                    whitespace_after=line_break,
                ),
            ))

        all_names = Assign(
            targets=(AssignTarget(target=Name(value="__all__")), ),
            value=List(
                list_values,
                lbracket=LeftSquareBracket(
                    whitespace_after=ParenthesizedWhitespace(
                        first_line=TrailingWhitespace(
                            whitespace=SimpleWhitespace(value=""),
                            comment=None,
                            newline=Newline(value=None),
                        ),
                        empty_lines=[],
                        indent=True,
                        last_line=SimpleWhitespace(value="    "),
                    )),
                rbracket=RightSquareBracket(whitespace_before=SimpleWhitespace(
                    value="")),
            ),
        )

        modified_body.append(Newline())
        modified_body.append(all_names)
        return updated_node.with_changes(body=modified_body)