1+ """
2+ Post-generation script to simplify filters parameter.
3+ This script replaces complex QueryParameterDynamicFilterSchema type hints
4+ with simple Optional[str] to allow users to pass filter strings directly.
5+ """
6+
7+ import re
8+ import sys
9+ from pathlib import Path
10+
11+
12+ def simplify_filters_in_file (api_file ):
13+ """Simplify filters parameter type hints in a single API file.
14+
15+ Args:
16+ api_file: Path to the API file to fix
17+
18+ Returns:
19+ bool: True if file was processed successfully
20+ """
21+ print (f"Processing { api_file } ..." )
22+
23+ content = api_file .read_text ()
24+ original_content = content
25+
26+ # Pattern 1: Replace the import of QueryParameterDynamicFilterSchema
27+ import_pattern = r'from cloudbeds_pms\.models\.query_parameter_dynamic_filter_schema import QueryParameterDynamicFilterSchema\n'
28+ content = re .sub (import_pattern , '' , content )
29+
30+ # Pattern 2: Replace type hints in method signatures
31+ # Match: filters: Annotated[Optional[QueryParameterDynamicFilterSchema], ...]
32+ # Replace with: filters: Annotated[Optional[str], ...]
33+ type_hint_pattern = r'(filters:\s*Annotated\[)Optional\[QueryParameterDynamicFilterSchema\]'
34+ content = re .sub (type_hint_pattern , r'\1Optional[str]' , content )
35+
36+ # Pattern 3: Update docstrings that mention the type
37+ docstring_pattern = r':type filters: QueryParameterDynamicFilterSchema'
38+ content = re .sub (docstring_pattern , ':type filters: str' , content )
39+
40+ if content != original_content :
41+ api_file .write_text (content )
42+ print (f" ✓ Simplified filters parameter in { api_file .name } " )
43+ return True
44+ else :
45+ print (f" No QueryParameterDynamicFilterSchema found in { api_file .name } " )
46+ return True
47+
48+
49+ def simplify_filters ():
50+ """Simplify filters parameter in all generated API client files."""
51+
52+ api_dir = Path ("cloudbeds_pms/api" )
53+
54+ if not api_dir .exists ():
55+ print (f"Error: { api_dir } not found" )
56+ return False
57+
58+ print (f"Scanning for API files in { api_dir } ...\n " )
59+
60+ # Find all Python files in the api directory
61+ api_files = list (api_dir .glob ("*_api.py" ))
62+
63+ if not api_files :
64+ print ("No API files found" )
65+ return False
66+
67+ print (f"Found { len (api_files )} API file(s)\n " )
68+
69+ # Process each file
70+ success = True
71+ modified_count = 0
72+
73+ for api_file in sorted (api_files ):
74+ if not simplify_filters_in_file (api_file ):
75+ success = False
76+ else :
77+ modified_count += 1
78+
79+ print (f"\n Processed { modified_count } /{ len (api_files )} file(s) successfully" )
80+ return success
81+
82+
83+ def main ():
84+ """Main function."""
85+ success = simplify_filters ()
86+ sys .exit (0 if success else 1 )
87+
88+
89+ if __name__ == "__main__" :
90+ main ()
0 commit comments