|
| 1 | +from typing import List, Dict, Tuple, Literal |
| 2 | + |
| 3 | +import ast |
| 4 | + |
| 5 | + |
| 6 | +class MethodInfo: |
| 7 | + """Information about a method.""" |
| 8 | + |
| 9 | + def __init__(self, name: str, node: ast.FunctionDef): |
| 10 | + self.name = name |
| 11 | + self.node = node |
| 12 | + self.is_target = not name.startswith( |
| 13 | + "_" |
| 14 | + ) # all the public method is our target method for further processing |
| 15 | + self.is_async = name.endswith("_async") |
| 16 | + self.base_name = name[:-6] if self.is_async else name |
| 17 | + |
| 18 | + |
| 19 | +class ParseTargetClassFunctionsVisitor(ast.NodeVisitor): |
| 20 | + """AST visitor to parse functions out of target class name for given module tree""" |
| 21 | + |
| 22 | + def __init__(self, class_name: str): |
| 23 | + self.class_name = class_name |
| 24 | + self.methods: List[MethodInfo] = [] |
| 25 | + self.is_cur_node_in_target_class = False |
| 26 | + |
| 27 | + def visit_ClassDef(self, node: ast.ClassDef): |
| 28 | + if node.name == self.class_name: |
| 29 | + self.is_cur_node_in_target_class = True |
| 30 | + self.generic_visit(node) |
| 31 | + self.is_cur_node_in_target_class = False |
| 32 | + else: |
| 33 | + self.generic_visit(node) |
| 34 | + |
| 35 | + def visit_FunctionDef(self, node: ast.FunctionDef): |
| 36 | + if self.is_cur_node_in_target_class: |
| 37 | + # add all the method to the methods |
| 38 | + self.methods.append(MethodInfo(node.name, node)) |
| 39 | + self.generic_visit(node) |
| 40 | + |
| 41 | + def visit_AsyncFunctionDef(self, node): |
| 42 | + if self.is_cur_node_in_target_class: |
| 43 | + # add all the method to the methods |
| 44 | + self.methods.append(MethodInfo(node.name, node)) |
| 45 | + self.generic_visit(node) |
| 46 | + |
| 47 | + |
| 48 | +class FillMissingMethodsToClass(ast.NodeTransformer): |
| 49 | + """AST Transformer to fill missing async_methods back to target class""" |
| 50 | + |
| 51 | + def __init__(self, class_name: str, to_add_async_methods: Dict[str, MethodInfo]): |
| 52 | + self.class_name = class_name |
| 53 | + self.to_add_async_methods = to_add_async_methods |
| 54 | + |
| 55 | + def visit_ClassDef(self, node: ast.ClassDef): |
| 56 | + if node.name == self.class_name: |
| 57 | + for sync_func_name, async_func_node in self.to_add_async_methods.items(): |
| 58 | + idx = next( |
| 59 | + ( |
| 60 | + i |
| 61 | + for i, stmt in enumerate(node.body) |
| 62 | + if isinstance(stmt, ast.FunctionDef) |
| 63 | + and stmt.name == sync_func_name |
| 64 | + ), |
| 65 | + -1, |
| 66 | + ) |
| 67 | + |
| 68 | + if idx != -1: |
| 69 | + node.body.insert(idx + 1, async_func_node.node) |
| 70 | + |
| 71 | + return self.generic_visit(node) |
| 72 | + |
| 73 | + |
| 74 | +def parse_methods_info_from_target_class( |
| 75 | + module_tree: ast.Module, target_class: Literal["PGMQueue", "PGMQOperation"] |
| 76 | +) -> Tuple[List[MethodInfo], set[str]]: |
| 77 | + """ |
| 78 | + Parse methods of target class from give module AST Tree |
| 79 | +
|
| 80 | + Args: |
| 81 | + module_tree: ast.Module |
| 82 | + target_class: either "PGMQueue" or "PGMQOperation" str |
| 83 | +
|
| 84 | + Returns: |
| 85 | + Tuple of sync_methods, missing_async_set |
| 86 | + """ |
| 87 | + |
| 88 | + analyzer = ParseTargetClassFunctionsVisitor(target_class) |
| 89 | + analyzer.visit(module_tree) |
| 90 | + |
| 91 | + # Categorize methods |
| 92 | + # We use sync methods as source of truth |
| 93 | + async_methods_set = set() |
| 94 | + missing_async_set = set() |
| 95 | + |
| 96 | + for method_info in analyzer.methods: |
| 97 | + # skip non target methods |
| 98 | + if not method_info.is_target: |
| 99 | + continue |
| 100 | + |
| 101 | + if method_info.is_async: |
| 102 | + async_methods_set.add(method_info.base_name) |
| 103 | + |
| 104 | + # Find missing async methods and generate class with interleaved methods |
| 105 | + for method_info in analyzer.methods: |
| 106 | + # skip non target methods |
| 107 | + if not method_info.is_target: |
| 108 | + continue |
| 109 | + |
| 110 | + if method_info.base_name not in async_methods_set: |
| 111 | + missing_async_set.add(method_info.base_name) |
| 112 | + |
| 113 | + return analyzer.methods, missing_async_set |
| 114 | + |
| 115 | + |
| 116 | +def fill_missing_methods_to_class( |
| 117 | + module_tree: ast.Module, |
| 118 | + target_class: Literal["PGMQueue", "PGMQOperation"], |
| 119 | + to_add_async_methods: Dict[str, MethodInfo], |
| 120 | +): |
| 121 | + transformer = FillMissingMethodsToClass( |
| 122 | + class_name=target_class, to_add_async_methods=to_add_async_methods |
| 123 | + ) |
| 124 | + transformer.visit(module_tree) |
0 commit comments