|
| 1 | +""" |
| 2 | +This script fixes deprecated urllib3 methods in the generated code by replacing: |
| 3 | +- response.getheader('content-type') → response.headers.get('content-type') |
| 4 | +- response.getheaders() → response.headers |
| 5 | +
|
| 6 | +This addresses deprecation warnings that will become breaking errors in urllib3 2.6.0. |
| 7 | +Based on urllib3 v2.0 migration guide: https://urllib3.readthedocs.io/en/stable/v2-migration-guide.html |
| 8 | +
|
| 9 | +Files affected: |
| 10 | +- openapi_generated/pinterest_client/rest.py (lines 37-43) |
| 11 | +- openapi_generated/pinterest_client/api_client.py (lines 224, 243, 320) |
| 12 | +- openapi_generated/pinterest_client/exceptions.py (line 107) |
| 13 | +""" |
| 14 | +import os |
| 15 | +import re |
| 16 | + |
| 17 | +directory = '../openapi_generated' |
| 18 | + |
| 19 | +# Define the replacements - apply multiple times until no more changes |
| 20 | +def apply_replacements(content): |
| 21 | + """Apply all replacements until no more changes are made""" |
| 22 | + original_content = content |
| 23 | + |
| 24 | + # First: Clean up any multiple urllib3_response references (make it idempotent) |
| 25 | + content = re.sub(r'(\.urllib3_response)+', '.urllib3_response', content) |
| 26 | + |
| 27 | + # Second: Replace deprecated getheader() calls with headers.get() |
| 28 | + # All response objects are RESTResponse wrappers, so use .urllib3_response.headers |
| 29 | + content = re.sub(r'(response_data|response|http_resp)\.getheader\(([^)]+)\)', r'\1.urllib3_response.headers.get(\2)', content) |
| 30 | + |
| 31 | + # Third: Replace deprecated getheaders() calls with headers |
| 32 | + # All response objects are RESTResponse wrappers, so use .urllib3_response.headers |
| 33 | + content = re.sub(r'(response_data|response|http_resp)\.getheaders\(\)', r'\1.urllib3_response.headers', content) |
| 34 | + |
| 35 | + return content |
| 36 | + |
| 37 | +print("Starting the script to fix deprecated urllib3 methods in the 'openapi_generated' directory...") |
| 38 | +print("This ensures compatibility with both urllib3 1.x and 2.x") |
| 39 | + |
| 40 | +files_modified = 0 |
| 41 | +total_replacements = 0 |
| 42 | + |
| 43 | +for root, dirs, files in os.walk(directory): |
| 44 | + for file in files: |
| 45 | + if file.endswith(".py"): |
| 46 | + file_path = os.path.join(root, file) |
| 47 | + |
| 48 | + try: |
| 49 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 50 | + content = f.read() |
| 51 | + |
| 52 | + original_content = content |
| 53 | + |
| 54 | + # Apply all replacements using the function |
| 55 | + content = apply_replacements(content) |
| 56 | + |
| 57 | + # Write back if changes were made |
| 58 | + if content != original_content: |
| 59 | + with open(file_path, 'w', encoding='utf-8') as f: |
| 60 | + f.write(content) |
| 61 | + |
| 62 | + # Count replacements made in this file |
| 63 | + file_replacements = len(original_content) - len(content) + content.count('urllib3_response') - original_content.count('urllib3_response') |
| 64 | + |
| 65 | + print(f"Modified: {file_path} ({file_replacements} replacements)") |
| 66 | + files_modified += 1 |
| 67 | + total_replacements += file_replacements |
| 68 | + |
| 69 | + except Exception as e: |
| 70 | + print(f"Error processing {file_path}: {e}") |
| 71 | + |
| 72 | +print(f"Script completed. Modified {files_modified} files with {total_replacements} total replacements.") |
| 73 | +print("Deprecated urllib3 methods have been updated to use the new headers API.") |
| 74 | +print("Your package now supports both urllib3 1.x and 2.x!") |
0 commit comments