Example #1
0
    def leave_Assign(self, original_node: cst.Assign,
                     updated_node: cst.Assign):

        new_targets = []
        new_value = self.obf_universal(updated_node.value)
        for el in updated_node.targets:
            new_targets.append(self.obf_universal(el, 'v', 'a', 'ca'))

        updated_node = updated_node.with_changes(targets=new_targets)
        updated_node = updated_node.with_changes(value=new_value)

        return updated_node
Example #2
0
 def leave_Assign(self, original_node: libcst.Assign,
                  updated_node: libcst.Assign) -> libcst.Assign:
     assign_value = updated_node.value
     if hasattr(assign_value, "lpar"):
         parenthesized_value = assign_value.with_changes(
             lpar=[libcst.LeftParen()], rpar=[libcst.RightParen()])
         return updated_node.with_changes(value=parenthesized_value)
     return updated_node
Example #3
0
 def leave_Assign(self, original_node: libcst.Assign,
                  updated_node: libcst.Assign) -> libcst.Assign:
     assign_value = updated_node.value
     assign_whitespace = updated_node.targets[-1].whitespace_after_equal
     if libcst_matchers.matches(assign_whitespace,
                                libcst_matchers.ParenthesizedWhitespace()):
         adjusted_target = updated_node.targets[-1].with_changes(
             whitespace_after_equal=libcst.SimpleWhitespace(value=" "))
         updated_targets = list(updated_node.targets[:-1])
         updated_targets.append(adjusted_target)
         return updated_node.with_changes(
             targets=tuple(updated_targets),
             value=LineBreakTransformer.basic_parenthesize(
                 assign_value, assign_whitespace),
         )
     return updated_node.with_changes(
         value=LineBreakTransformer.basic_parenthesize(assign_value))
Example #4
0
 def leave_Assign(self, original_node: cst.Assign,
                  updated_node: cst.Assign) -> cst.Assign:
     new_value = updated_node.value.visit(self)
     assert not self._assigned_names, (to_module(original_node).code,
                                       self._assigned_names)
     new_targets = [t.visit(self) for t in updated_node.targets]
     final_node = updated_node.with_changes(value=new_value,
                                            targets=new_targets)
     for name in self._assigned_names:
         self.name_assignments[name] = original_node
     self._assigned_names = []
     return final_node
Example #5
0
    def leave_FunctionDef(
        self, original_node: "FunctionDef", updated_node: "FunctionDef"
    ) -> Union["BaseStatement", RemovalSentinel]:
        modified_defaults = []
        mutable_args = {}

        for default_param in original_node.params.default_params:
            if isinstance(default_param.default, (List, Dict)):
                mutable_args[
                    default_param.name] = default_param.default.deep_clone()
                modified_defaults.append(
                    default_param.with_changes(default=Name("None")))
            else:
                modified_defaults.append(default_param)

        modified_params = original_node.params.with_changes(
            default_params=modified_defaults)

        inits = [
            If(
                test=Comparison(
                    left=Name(value=arg.value, lpar=[], rpar=[]),
                    comparisons=[
                        ComparisonTarget(
                            operator=Is(),
                            comparator=Name(value="None", lpar=[], rpar=[]),
                        )
                    ],
                ),
                body=IndentedBlock(
                    body=[
                        SimpleStatementLine(body=[
                            Assign(
                                targets=[
                                    AssignTarget(target=Name(value=arg.value))
                                ],
                                value=init,
                            )
                        ])
                    ],
                    footer=[Newline(value=None)],
                ),
            ) for arg, init in mutable_args.items()
        ]

        modified_body = (*inits, *original_node.body.body)

        return updated_node.with_changes(
            params=modified_params,
            body=original_node.body.with_changes(body=modified_body),
        )
Example #6
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)