|
| 1 | +"""Generate async api from sync api""" |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +import libcst as cst |
| 5 | +from libcst._nodes.expression import Await |
| 6 | +from libcst._nodes.whitespace import SimpleWhitespace |
| 7 | + |
| 8 | + |
| 9 | +class SyncToAsyncTransformer(cst.CSTTransformer): |
| 10 | + def __init__(self): |
| 11 | + self.stack = [] |
| 12 | + self.fn_should_async = None |
| 13 | + |
| 14 | + # PATH MAKING |
| 15 | + def visit_ClassDef(self, node: cst.ClassDef) -> Optional[bool]: |
| 16 | + self.stack.append(node.name.value) |
| 17 | + |
| 18 | + def leave_ClassDef( |
| 19 | + self, original_node: cst.ClassDef, updated_node: cst.ClassDef |
| 20 | + ) -> cst.CSTNode: |
| 21 | + self.stack.pop() |
| 22 | + return updated_node |
| 23 | + |
| 24 | + def visit_FunctionDef(self, node: cst.FunctionDef) -> Optional[bool]: |
| 25 | + self.stack.append(node.name.value) |
| 26 | + |
| 27 | + # END PATH MAKING |
| 28 | + |
| 29 | + def leave_ImportAlias( |
| 30 | + self, original_node: cst.ImportAlias, updated_node: cst.ImportAlias |
| 31 | + ) -> cst.CSTNode: |
| 32 | + """Replace requests import with httpx""" |
| 33 | + |
| 34 | + if original_node.name.value == "requests": |
| 35 | + return updated_node.with_changes( |
| 36 | + name=cst.Name("httpx"), |
| 37 | + ) |
| 38 | + |
| 39 | + return updated_node |
| 40 | + |
| 41 | + def leave_Attribute( |
| 42 | + self, original_node: cst.Attribute, updated_node: cst.Attribute |
| 43 | + ) -> cst.CSTNode: |
| 44 | + """Replace requests attrs with httpx attrs""" |
| 45 | + |
| 46 | + if ( |
| 47 | + isinstance(original_node.value, cst.Name) |
| 48 | + and original_node.value.value == "requests" |
| 49 | + ): |
| 50 | + mapping = {"Session": "AsyncClient"} |
| 51 | + |
| 52 | + return updated_node.with_changes( |
| 53 | + value=cst.Name("httpx"), |
| 54 | + attr=cst.Name(mapping[original_node.attr.value]), |
| 55 | + ) |
| 56 | + |
| 57 | + return updated_node |
| 58 | + |
| 59 | + def leave_Call(self, original_node: cst.FunctionDef, updated_node: cst.FunctionDef): |
| 60 | + """Await calls to `method` of TelegraphApi""" |
| 61 | + path = [] |
| 62 | + |
| 63 | + a = original_node.func |
| 64 | + while isinstance(a, cst.Attribute) or isinstance(a, cst.Name): |
| 65 | + if isinstance(a, cst.Attribute): |
| 66 | + path.append(a.attr.value) |
| 67 | + else: |
| 68 | + path.append(a.value) |
| 69 | + a = a.value |
| 70 | + |
| 71 | + # await the call if it's API class method |
| 72 | + should_await = ( |
| 73 | + path[-2:] == ["session", "self"] |
| 74 | + or path[-3:] == [ |
| 75 | + "method", |
| 76 | + "_telegraph", |
| 77 | + "self", |
| 78 | + ] |
| 79 | + or path[-3:] == [ |
| 80 | + "upload_file", |
| 81 | + "_telegraph", |
| 82 | + "self", |
| 83 | + ] |
| 84 | + ) |
| 85 | + if not should_await: |
| 86 | + return updated_node |
| 87 | + |
| 88 | + self.fn_should_async = self.stack # mark current fn as async on leave |
| 89 | + # await the call |
| 90 | + return Await( |
| 91 | + updated_node, |
| 92 | + lpar=[cst.LeftParen()], |
| 93 | + rpar=[cst.RightParen()], |
| 94 | + ) |
| 95 | + |
| 96 | + def leave_FunctionDef( |
| 97 | + self, original_node: cst.FunctionDef, updated_node: cst.FunctionDef |
| 98 | + ): |
| 99 | + should_async = self.stack == self.fn_should_async |
| 100 | + self.fn_should_async = None |
| 101 | + self.stack.pop() |
| 102 | + |
| 103 | + if not should_async: |
| 104 | + return updated_node |
| 105 | + |
| 106 | + # mark fn as async |
| 107 | + return updated_node.with_changes( |
| 108 | + asynchronous=cst.Asynchronous() |
| 109 | + ) |
| 110 | + |
| 111 | + |
| 112 | +def main(): |
| 113 | + with open("telegraph/api.py") as f: |
| 114 | + py_source = f.read() |
| 115 | + |
| 116 | + source_tree = cst.parse_module(py_source) |
| 117 | + |
| 118 | + transformer = SyncToAsyncTransformer() |
| 119 | + modified_tree = source_tree.visit(transformer) |
| 120 | + |
| 121 | + with open("telegraph/aio.py", "w") as f: |
| 122 | + f.write(modified_tree.code) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + main() |
0 commit comments