Debian Codegraph @

scripts/unasync

raw
1 #!/usr/bin/env -S uv run --script
2 # /// script
3 # requires-python = ">=3.10"
4 # dependencies = []
5 # ///
6
7
8 import os
9 import re
10 import sys
11
12 SUBS = [
13 # builtins
14 ("async def", "def"),
15 ("async with", "with"),
16 ("await ", ""),
17 ("async for", "for"),
18 ("AsyncIterator", "Iterator"),
19 ("AsyncIterable", "Iterable"),
20 ("__aiter__", "__iter__"),
21 ("__anext__", "__next__"),
22 ("AsyncMock", "MagicMock"),
23 ("assert_awaited_once", "assert_called_once"),
24 (r"Awaitable\[([^\]]+)\]", r"\1"),
25 # our public API
26 ("AsyncCacheProxy", "SyncCacheProxy"),
27 ("AsyncBaseStorage", "SyncBaseStorage"),
28 ("AsyncCacheClient", "SyncCacheClient"),
29 ("AsyncSqliteStorage", "SyncSqliteStorage"),
30 ("anysqlite", "sqlite3"),
31 ("@pytest.mark.anyio", ""),
32 ("from anyio import Lock", "from threading import RLock"),
33 ("self._lock = Lock", "self._lock = RLock"),
34 ("aiter_stream", "iter_stream"),
35 ("aiter_raw", "iter_raw"),
36 ("aprint_sqlite_state", "print_sqlite_state"),
37 ("make_async_iterator", "make_sync_iterator"),
38 ("AsyncCacheTransport", "SyncCacheTransport"),
39 (
40 "hishel._core._storages._async_base",
41 "hishel._core._storages._sync_base",
42 ),
43 # Third-party libraries
44 ("AsyncClient", "Client"),
45 ("aread", "read"),
46 ("aclose", "close"),
47 ("handle_async_request", "handle_request"),
48 ("AsyncBaseTransport", "BaseTransport"),
49 ("AsyncHTTPTransport", "HTTPTransport"),
50 ]
51 COMPILED_SUBS = [(re.compile(regex), repl) for regex, repl in SUBS]
52
53 USED_SUBS = set()
54
55
56 def unasync_line(line):
57 for index, (regex, repl) in enumerate(COMPILED_SUBS):
58 old_line = line
59 line = re.sub(regex, repl, line)
60 if index not in USED_SUBS:
61 if line != old_line:
62 USED_SUBS.add(index)
63 return line
64
65
66 def unasync_file(in_path, out_path):
67 with open(in_path) as in_file:
68 with open(out_path, "w", newline="") as out_file:
69 for line in in_file.readlines():
70 line = unasync_line(line)
71 out_file.write(line)
72
73
74 def unasync_file_check(in_path, out_path):
75 with open(in_path) as in_file:
76 with open(out_path) as out_file:
77 for in_line, out_line in zip(in_file.readlines(), out_file.readlines()):
78 expected = unasync_line(in_line)
79 if out_line != expected:
80 print(f"unasync mismatch between {in_path!r} and {out_path!r}")
81 print(f"Async code: {in_line!r}")
82 print(f"Expected sync code: {expected!r}")
83 print(f"Actual sync code: {out_line!r}")
84 sys.exit(1)
85
86
87 def unasync_dir(in_dir, out_dir, check_only=False):
88 for dirpath, dirnames, filenames in os.walk(in_dir):
89 for filename in filenames:
90 if not filename.endswith(".py"):
91 continue
92 rel_dir = os.path.relpath(dirpath, in_dir)
93 in_path = os.path.normpath(os.path.join(in_dir, rel_dir, filename))
94 out_path = os.path.normpath(os.path.join(out_dir, rel_dir, filename))
95 print(in_path, "->", out_path)
96 if check_only:
97 unasync_file_check(in_path, out_path)
98 else:
99 unasync_file(in_path, out_path)
100
101
102 def main():
103 check_only = "--check" in sys.argv
104
105 FILES = [
106 (
107 "tests/_core/_async/test_sqlite_storage.py",
108 "tests/_core/_sync/test_sqlite_storage.py",
109 ),
110 ("tests/test_async_httpx.py", "tests/test_sync_httpx.py"),
111 ("hishel/_async_cache.py", "hishel/_sync_cache.py"),
112 ("hishel/_core/_storages/_async_base.py", "hishel/_core/_storages/_sync_base.py"),
113 ("hishel/_core/_storages/_async_sqlite.py", "hishel/_core/_storages/_sync_sqlite.py"),
114 ("hishel/_async_httpx.py", "hishel/_sync_httpx.py"),
115 ]
116
117 for in_path, out_path in FILES:
118 if check_only:
119 unasync_file_check(in_path, out_path)
120 else:
121 unasync_file(in_path, out_path)
122 print(f"Wrote {out_path}")
123
124 if len(USED_SUBS) != len(SUBS):
125 unused_subs = [SUBS[i] for i in range(len(SUBS)) if i not in USED_SUBS]
126
127 from pprint import pprint
128
129 print("This SUBS was not used")
130 pprint(unused_subs)
131
132
133 if __name__ == "__main__":
134 main()