Coverage for src/rejuvenation/python_rst_example.py: 93%
41 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-10-02 13:12 +0000
« prev ^ index » next coverage.py v7.16.1, created at 2026-10-02 13:12 +0000
1"""AI: Example script demonstrating RST-based rewriting of Python source, including nested replacements and multiple patterns."""
3# This script demonstrates the use of the syntax_tree library to parse and rewrite Python code.
4# It specifically showcases nested replacements and multiple patterns.
5import textwrap
7from renaissance.integrations.python.ast.factory import PythonFactory, PythonPatternFactory
8from renaissance.integrations.python.ast.rst_node import PythonRstNode
9from renaissance.syntax_tree import ASTRewriter, ASTShower
10from renaissance.syntax_tree.ast_finder import find_semantic_kind
11from renaissance.syntax_tree.match_finder import match_pattern
12from renaissance.syntax_tree.semantic_kind import SemanticKind
14example_code = """
15from module import foo, bar, baz, quux
16ba(51)
17na(52)
18na(53)
19pa(54)
20if pa():
21 ba()
22pa(54)
23"""
26def python_rst_smoke_test():
27 """AI: Demonstrate matching and rewriting `example_code` using the RST integration."""
28 atu: PythonRstNode = PythonRstNode.load_from_text(example_code)
30 factory = PythonFactory(PythonRstNode)
31 pattern_factory = PythonPatternFactory(factory)
33 atu = factory.create_from_text(example_code, "example.py")
35 pattern1 = pattern_factory.create_statement("if pa(): $$stmts")
36 pattern2 = pattern_factory.create_expression("na($a)")
38 print("_______________pattern 1____________________________________")
39 ASTShower.show_node(pattern1.node, include_properties=True)
40 print("_______________pattern 1____________________________________")
41 ASTShower.show_node(pattern2.node, include_properties=False)
42 print("_______________ast____________________________________")
43 ASTShower.focus = "ba"
44 ASTShower.show_node(atu)
46 print("_______________simple find____________________________________")
47 nodes = find_semantic_kind(atu, SemanticKind.CALL)
49 ASTShower.show_node(nodes[0])
51 pattern1replacement = textwrap.dedent("""
52 # changed if expr to const
53 isAOne=True
54 if(isAOne):
55 $$stmts
56 """)
57 pattern2replacement = "# changed function f1 to f2\nf2($a,123456)\n"
59 rewriter = ASTRewriter(atu)
61 for match in match_pattern(atu.body, [pattern1]):
62 refactor(match, pattern1replacement, rewriter)
64 for match in match_pattern(atu.body, [pattern2]):
65 refactor(match, pattern2replacement, rewriter)
67 return rewriter.apply_to_string()
70def refactor(match, replacement_text, rewriter):
71 """AI: Expand placeholders in `replacement_text` from `match` and queue the rewrite."""
72 for placeholder in match.expansions:
73 replacement_text = replacement_text.replace(placeholder, match[placeholder])
74 return rewriter.replace(replacement_text, match.nodes)
77if __name__ == "__main__":
78 result = python_rst_smoke_test()
79 print("_______________end result_________________________________")
80 print(result)