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