Repository navigation
Expand file tree
/
Copy pathgenerate_api_methods.py
More file actions
executable file
·376 lines (340 loc) · 12.7 KB
/
Copy pathgenerate_api_methods.py
File metadata and controls
executable file
·376 lines (340 loc) · 12.7 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/env python3
"""Generate Direct API methods from OpenAPI specification."""
import re
from pathlib import Path
from typing import Any
def to_snake_case(name: str) -> str:
"""Convert string to snake_case."""
# Handle common patterns
name = name.replace("-", "_")
# Insert underscore before uppercase letters
name = re.sub(r"(?<!^)(?=[A-Z])", "_", name).lower()
return name
def get_python_type(schema: dict[str, Any]) -> str:
"""Convert OpenAPI schema type to Python type hint."""
if not schema:
return "Any"
type_mapping = {
"string": "str",
"integer": "int",
"number": "float",
"boolean": "bool",
"array": "List[Any]",
"object": "Dict[str, Any]",
}
schema_type = schema.get("type", "string")
return type_mapping.get(schema_type, "Any")
def create_manual_tools() -> list[dict[str, Any]]:
"""Create tool definitions based on the specification documentation.
Since the Nutrient API uses a build endpoint with actions rather than
individual tool endpoints, we'll create convenience methods that wrap
the build API.
"""
tools = [
{
"tool_name": "convert-to-pdf",
"method_name": "convert_to_pdf",
"summary": "Convert a document to PDF",
"description": "Convert various document formats (DOCX, XLSX, PPTX, etc.) to PDF.",
"parameters": {},
},
{
"tool_name": "convert-to-pdfa",
"method_name": "convert_to_pdfa",
"summary": "Convert a document to PDF/A",
"description": "Convert documents to PDF/A format for long-term archiving.",
"parameters": {
"conformance_level": {
"type": "str",
"required": False,
"description": "PDF/A conformance level (e.g., '2b', '3b')",
"default": "2b",
},
},
},
{
"tool_name": "ocr-pdf",
"method_name": "ocr_pdf",
"summary": "Perform OCR on a PDF",
"description": "Apply optical character recognition to make scanned PDFs searchable.",
"parameters": {
"language": {
"type": "str",
"required": False,
"description": "OCR language code (e.g., 'en', 'de', 'fr')",
"default": "en",
},
},
},
{
"tool_name": "rotate-pages",
"method_name": "rotate_pages",
"summary": "Rotate PDF pages",
"description": "Rotate pages in a PDF document.",
"parameters": {
"degrees": {
"type": "int",
"required": True,
"description": "Rotation angle in degrees (90, 180, 270)",
},
"page_indexes": {
"type": "List[int]",
"required": False,
"description": (
"List of page indexes to rotate (0-based). "
"If not specified, all pages are rotated."
),
},
},
},
{
"tool_name": "flatten-annotations",
"method_name": "flatten_annotations",
"summary": "Flatten PDF annotations",
"description": "Flatten annotations and form fields in a PDF.",
"parameters": {},
},
{
"tool_name": "watermark-pdf",
"method_name": "watermark_pdf",
"summary": "Add watermark to PDF",
"description": "Add text or image watermark to PDF pages.",
"parameters": {
"text": {
"type": "str",
"required": False,
"description": "Watermark text",
},
"image_url": {
"type": "str",
"required": False,
"description": "URL of watermark image",
},
"opacity": {
"type": "float",
"required": False,
"description": "Watermark opacity (0.0 to 1.0)",
"default": 0.5,
},
"position": {
"type": "str",
"required": False,
"description": "Watermark position",
"default": "center",
},
},
},
{
"tool_name": "sign-pdf",
"method_name": "sign_pdf",
"summary": "Digitally sign a PDF",
"description": "Add a digital signature to a PDF document.",
"parameters": {
"certificate_file": {
"type": "FileInput",
"required": True,
"description": "Digital certificate file (P12/PFX format)",
},
"certificate_password": {
"type": "str",
"required": True,
"description": "Certificate password",
},
"reason": {
"type": "str",
"required": False,
"description": "Reason for signing",
},
"location": {
"type": "str",
"required": False,
"description": "Location of signing",
},
},
},
{
"tool_name": "redact-pdf",
"method_name": "redact_pdf",
"summary": "Redact sensitive information from PDF",
"description": "Use AI to automatically redact sensitive information from a PDF.",
"parameters": {
"types": {
"type": "List[str]",
"required": False,
"description": "Types of information to redact (e.g., 'email', 'phone', 'ssn')",
},
},
},
{
"tool_name": "export-pdf-to-office",
"method_name": "export_pdf_to_office",
"summary": "Export PDF to Office format",
"description": "Convert PDF to Microsoft Office formats (DOCX, XLSX, PPTX).",
"parameters": {
"format": {
"type": "str",
"required": True,
"description": "Output format ('docx', 'xlsx', 'pptx')",
},
},
},
{
"tool_name": "export-pdf-to-images",
"method_name": "export_pdf_to_images",
"summary": "Export PDF pages as images",
"description": "Convert PDF pages to image files.",
"parameters": {
"format": {
"type": "str",
"required": False,
"description": "Image format ('png', 'jpeg', 'webp')",
"default": "png",
},
"dpi": {
"type": "int",
"required": False,
"description": "Image resolution in DPI",
"default": 150,
},
"page_indexes": {
"type": "List[int]",
"required": False,
"description": "List of page indexes to export (0-based)",
},
},
},
]
return tools
def generate_method_code(tool_info: dict[str, Any]) -> str:
"""Generate Python method code for a tool."""
method_name = tool_info["method_name"]
tool_name = tool_info["tool_name"]
summary = tool_info["summary"]
description = tool_info["description"]
parameters = tool_info["parameters"]
# Build parameter list
param_list = ["self", "input_file: FileInput"]
param_docs = []
# Add required parameters first
for param_name, param_info in parameters.items():
if param_info["required"]:
param_type = param_info["type"]
# Handle imports for complex types
if param_type == "FileInput":
param_type = "'FileInput'" # Forward reference
param_list.append(f"{param_name}: {param_type}")
param_docs.append(f" {param_name}: {param_info['description']}")
# Always add output_path
param_list.append("output_path: Optional[str] = None")
# Add optional parameters
for param_name, param_info in parameters.items():
if not param_info["required"]:
param_type = param_info["type"]
# Handle List types
base_type = param_type
default = param_info.get("default")
if default is None:
param_list.append(f"{param_name}: Optional[{base_type}] = None")
else:
if isinstance(default, str):
param_list.append(f'{param_name}: {base_type} = "{default}"')
else:
param_list.append(f"{param_name}: {base_type} = {default}")
param_docs.append(f" {param_name}: {param_info['description']}")
# Build method signature
if len(param_list) > 3: # Multiple parameters
params_str = ",\n ".join(param_list)
method_signature = (
f" def {method_name}(\n {params_str},\n ) -> Optional[bytes]:"
)
else:
params_str = ", ".join(param_list)
method_signature = f" def {method_name}({params_str}) -> Optional[bytes]:"
# Build docstring
docstring_lines = [f' """{summary}']
if description and description != summary:
docstring_lines.append("")
docstring_lines.append(f" {description}")
docstring_lines.extend(
[
"",
" Args:",
" input_file: Input file (path, bytes, or file-like object).",
]
)
if param_docs:
docstring_lines.extend(param_docs)
docstring_lines.extend(
[
" output_path: Optional path to save the output file.",
"",
" Returns:",
" Processed file as bytes, or None if output_path is provided.",
"",
" Raises:",
" AuthenticationError: If API key is missing or invalid.",
" APIError: For other API errors.",
' """',
]
)
# Build method body
method_body = []
# Collect kwargs
kwargs_params = [f"{name}={name}" for name in parameters]
if kwargs_params:
kwargs_str = ", ".join(kwargs_params)
method_body.append(
f' return self._process_file("{tool_name}", input_file, '
f"output_path, {kwargs_str})"
)
else:
method_body.append(
f' return self._process_file("{tool_name}", input_file, output_path)'
)
# Combine all parts
return "\n".join(
[
method_signature,
"\n".join(docstring_lines),
"\n".join(method_body),
]
)
def generate_api_methods(spec_path: Path, output_path: Path) -> None:
"""Generate API methods from OpenAPI specification."""
# For Nutrient API, we'll use manually defined tools since they use
# a build endpoint with actions rather than individual endpoints
tools = create_manual_tools()
# Sort tools by method name
tools.sort(key=lambda t: t["method_name"])
# Generate code
code_lines = [
'"""Direct API methods for individual document processing tools.',
"",
"This file provides convenient methods that wrap the Nutrient Build API",
"for common document processing operations.",
'"""',
"",
"from typing import List, Optional",
"",
"from nutrient_dws.file_handler import FileInput",
"",
"",
"class DirectAPIMixin:",
' """Mixin class containing Direct API methods.',
" ",
" These methods provide a simplified interface to common document",
" processing operations. They internally use the Build API.",
' """',
"",
]
# Add methods
for tool in tools:
code_lines.append(generate_method_code(tool))
code_lines.append("") # Empty line between methods
# Write to file
output_path.write_text("\n".join(code_lines))
print(f"Generated {len(tools)} API methods in {output_path}")
if __name__ == "__main__":
spec_path = Path("openapi_spec.yml")
output_path = Path("src/nutrient/api/direct.py")
generate_api_methods(spec_path, output_path)