Coverage for src/rejuvenation/python_rst_example.py: 93%
41 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-15 10:34 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-15 10:34 +0000
1# Copyright (C) 2024-2026 TNO
2#
3# Licensed under the terms specified in the LICENSE file in the root directory
4# of this repository, also available at:
5# https://github.com/TNO/Renaissance.Py/blob/main/LICENSE
7"""AI: Example script demonstrating RST-based rewriting of Python source, including nested replacements and multiple patterns."""
9# This script demonstrates the use of the syntax_tree library to parse and rewrite Python code.
10# It specifically showcases nested replacements and multiple patterns.
11import textwrap
13from renaissance.integrations.python.ast.factory import PythonFactory, PythonPatternFactory
14from renaissance.integrations.python.ast.rst_node import PythonRstNode
15from renaissance.syntax_tree import ASTRewriter, ASTShower
16from renaissance.syntax_tree.ast_finder import find_semantic_kind
17from renaissance.syntax_tree.match_finder import match_pattern
18from renaissance.syntax_tree.semantic_kind import SemanticKind
20example_code = """
21from module import foo, bar, baz, quux
22ba(51)
23na(52)
24na(53)
25pa(54)
26if pa():
27 ba()
28pa(54)
29"""
32def python_rst_smoke_test():
33 """AI: Demonstrate matching and rewriting `example_code` using the RST integration."""
34 atu: PythonRstNode = PythonRstNode.load_from_text(example_code)
36 factory = PythonFactory(PythonRstNode)
37 pattern_factory = PythonPatternFactory(factory)
39 atu = factory.create_from_text(example_code, "example.py")
41 pattern1 = pattern_factory.create_statement("if pa(): $$stmts")
42 pattern2 = pattern_factory.create_expression("na($a)")
44 print("_______________pattern 1____________________________________")
45 ASTShower.show_node(pattern1.node, include_properties=True)
46 print("_______________pattern 1____________________________________")
47 ASTShower.show_node(pattern2.node, include_properties=False)
48 print("_______________ast____________________________________")
49 ASTShower.focus = "ba"
50 ASTShower.show_node(atu)
52 print("_______________simple find____________________________________")
53 nodes = find_semantic_kind(atu, SemanticKind.CALL)
55 ASTShower.show_node(nodes[0])
57 pattern1replacement = textwrap.dedent("""
58 # changed if expr to const
59 isAOne=True
60 if(isAOne):
61 $$stmts
62 """)
63 pattern2replacement = "# changed function f1 to f2\nf2($a,123456)\n"
65 rewriter = ASTRewriter(atu)
67 for match in match_pattern(atu.body, [pattern1]):
68 refactor(match, pattern1replacement, rewriter)
70 for match in match_pattern(atu.body, [pattern2]):
71 refactor(match, pattern2replacement, rewriter)
73 return rewriter.apply_to_string()
76def refactor(match, replacement_text, rewriter):
77 """AI: Expand placeholders in `replacement_text` from `match` and queue the rewrite."""
78 for placeholder in match.expansions:
79 replacement_text = replacement_text.replace(placeholder, match[placeholder])
80 return rewriter.replace(replacement_text, match.nodes)
83if __name__ == "__main__":
84 result = python_rst_smoke_test()
85 print("_______________end result_________________________________")
86 print(result)