33
44import re
55from pathlib import Path
6- from typing import Any , Dict , List , Optional , Set
7-
8- import yaml
6+ from typing import Any , Dict , List
97
108
119def to_snake_case (name : str ) -> str :
@@ -21,7 +19,7 @@ def get_python_type(schema: Dict[str, Any]) -> str:
2119 """Convert OpenAPI schema type to Python type hint."""
2220 if not schema :
2321 return "Any"
24-
22+
2523 type_mapping = {
2624 "string" : "str" ,
2725 "integer" : "int" ,
@@ -30,7 +28,7 @@ def get_python_type(schema: Dict[str, Any]) -> str:
3028 "array" : "List[Any]" ,
3129 "object" : "Dict[str, Any]" ,
3230 }
33-
31+
3432 schema_type = schema .get ("type" , "string" )
3533 return type_mapping .get (schema_type , "Any" )
3634
@@ -213,7 +211,7 @@ def create_manual_tools() -> List[Dict[str, Any]]:
213211 },
214212 },
215213 ]
216-
214+
217215 return tools
218216
219217
@@ -224,11 +222,11 @@ def generate_method_code(tool_info: Dict[str, Any]) -> str:
224222 summary = tool_info ["summary" ]
225223 description = tool_info ["description" ]
226224 parameters = tool_info ["parameters" ]
227-
225+
228226 # Build parameter list
229227 param_list = ["self" , "input_file: FileInput" ]
230228 param_docs = []
231-
229+
232230 # Add required parameters first
233231 for param_name , param_info in parameters .items ():
234232 if param_info ["required" ]:
@@ -238,10 +236,10 @@ def generate_method_code(tool_info: Dict[str, Any]) -> str:
238236 param_type = "'FileInput'" # Forward reference
239237 param_list .append (f"{ param_name } : { param_type } " )
240238 param_docs .append (f" { param_name } : { param_info ['description' ]} " )
241-
239+
242240 # Always add output_path
243241 param_list .append ("output_path: Optional[str] = None" )
244-
242+
245243 # Add optional parameters
246244 for param_name , param_info in parameters .items ():
247245 if not param_info ["required" ]:
@@ -251,7 +249,7 @@ def generate_method_code(tool_info: Dict[str, Any]) -> str:
251249 base_type = param_type
252250 else :
253251 base_type = param_type
254-
252+
255253 default = param_info .get ("default" )
256254 if default is None :
257255 param_list .append (f"{ param_name } : Optional[{ base_type } ] = None" )
@@ -261,30 +259,30 @@ def generate_method_code(tool_info: Dict[str, Any]) -> str:
261259 else :
262260 param_list .append (f"{ param_name } : { base_type } = { default } " )
263261 param_docs .append (f" { param_name } : { param_info ['description' ]} " )
264-
262+
265263 # Build method signature
266264 if len (param_list ) > 3 : # Multiple parameters
267265 params_str = ",\n " .join (param_list )
268266 method_signature = f" def { method_name } (\n { params_str } ,\n ) -> Optional[bytes]:"
269267 else :
270268 params_str = ", " .join (param_list )
271269 method_signature = f" def { method_name } ({ params_str } ) -> Optional[bytes]:"
272-
270+
273271 # Build docstring
274272 docstring_lines = [f' """{ summary } ' ]
275273 if description and description != summary :
276274 docstring_lines .append ("" )
277275 docstring_lines .append (f" { description } " )
278-
276+
279277 docstring_lines .extend ([
280278 "" ,
281279 " Args:" ,
282280 " input_file: Input file (path, bytes, or file-like object)." ,
283281 ])
284-
282+
285283 if param_docs :
286284 docstring_lines .extend (param_docs )
287-
285+
288286 docstring_lines .extend ([
289287 " output_path: Optional path to save the output file." ,
290288 "" ,
@@ -296,22 +294,22 @@ def generate_method_code(tool_info: Dict[str, Any]) -> str:
296294 " APIError: For other API errors." ,
297295 ' """' ,
298296 ])
299-
297+
300298 # Build method body
301299 method_body = []
302-
300+
303301 # Collect kwargs
304302 kwargs_params = [
305- f"{ name } ={ name } "
303+ f"{ name } ={ name } "
306304 for name in parameters .keys ()
307305 ]
308-
306+
309307 if kwargs_params :
310308 kwargs_str = ", " .join (kwargs_params )
311309 method_body .append (f' return self._process_file("{ tool_name } ", input_file, output_path, { kwargs_str } )' )
312310 else :
313311 method_body .append (f' return self._process_file("{ tool_name } ", input_file, output_path)' )
314-
312+
315313 # Combine all parts
316314 return "\n " .join ([
317315 method_signature ,
@@ -325,10 +323,10 @@ def generate_api_methods(spec_path: Path, output_path: Path) -> None:
325323 # For Nutrient API, we'll use manually defined tools since they use
326324 # a build endpoint with actions rather than individual endpoints
327325 tools = create_manual_tools ()
328-
326+
329327 # Sort tools by method name
330328 tools .sort (key = lambda t : t ["method_name" ])
331-
329+
332330 # Generate code
333331 code_lines = [
334332 '"""Direct API methods for individual document processing tools.' ,
@@ -350,12 +348,12 @@ def generate_api_methods(spec_path: Path, output_path: Path) -> None:
350348 ' """' ,
351349 '' ,
352350 ]
353-
351+
354352 # Add methods
355353 for tool in tools :
356354 code_lines .append (generate_method_code (tool ))
357355 code_lines .append ("" ) # Empty line between methods
358-
356+
359357 # Write to file
360358 output_path .write_text ("\n " .join (code_lines ))
361359 print (f"Generated { len (tools )} API methods in { output_path } " )
@@ -364,5 +362,5 @@ def generate_api_methods(spec_path: Path, output_path: Path) -> None:
364362if __name__ == "__main__" :
365363 spec_path = Path ("openapi_spec.yml" )
366364 output_path = Path ("src/nutrient/api/direct.py" )
367-
368- generate_api_methods (spec_path , output_path )
365+
366+ generate_api_methods (spec_path , output_path )
0 commit comments