-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_api_client_close.py
More file actions
61 lines (50 loc) · 1.89 KB
/
patch_api_client_close.py
File metadata and controls
61 lines (50 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
"""Re-apply ApiClient.close() after OpenAPI regeneration."""
from __future__ import annotations
import pathlib
import sys
ROOT = pathlib.Path(__file__).resolve().parents[1]
def patch_rest_client() -> None:
path = ROOT / "hotdata" / "rest.py"
src = path.read_text()
needle = " self.pool_manager = urllib3.PoolManager(**pool_args)\n\n def request("
insert = (
" self.pool_manager = urllib3.PoolManager(**pool_args)\n\n"
" def close(self) -> None:\n"
" if self.pool_manager is not None:\n"
" self.pool_manager.clear()\n\n"
" def request("
)
if "def close(self) -> None:" in src and "pool_manager.clear()" in src:
return
if needle not in src:
sys.exit(f"Failed to patch {path}: RESTClientObject anchor not found")
path.write_text(src.replace(needle, insert, 1))
def patch_api_client() -> None:
path = ROOT / "hotdata" / "api_client.py"
src = path.read_text()
needle = (
" def __enter__(self):\n"
" return self\n\n"
" def __exit__(self, exc_type, exc_value, traceback):\n"
" pass\n"
)
replacement = (
" def __enter__(self):\n"
" return self\n\n"
" def close(self) -> None:\n"
" if self.rest_client is not None:\n"
" self.rest_client.close()\n\n"
" def __exit__(self, exc_type, exc_value, traceback):\n"
" self.close()\n"
)
if "def close(self) -> None:" in src and "self.rest_client.close()" in src:
return
if needle not in src:
sys.exit(f"Failed to patch {path}: ApiClient context manager anchor not found")
path.write_text(src.replace(needle, replacement, 1))
def main() -> None:
patch_rest_client()
patch_api_client()
if __name__ == "__main__":
main()