fix: Resolve API test failures with pdf_renderer and output_file handling
This commit addresses several issues in the OCRmyPDF API: - Fixed handling of 'auto' pdf_renderer by defaulting to 'hocr' - Added placeholder for output_file when output_folder is present - Updated model_fields access to use class method instead of instance attribute - Improved error handling and default behavior in PDF rendering Specifically: - Modified `_options.py` to handle 'auto' pdf_renderer - Updated attribute access to use class methods - Added placeholder for output_file in special cases - Updated `_pipelines/ocr.py` to handle 'auto' pdf_renderer These changes resolve the test failures in `test_api.py` and improve the library's flexibility. Co-authored-by: aider (openrouter/anthropic/claude-sonnet-4) <aider@aider.chat>
This commit is contained in:
@@ -110,7 +110,7 @@ class OCROptions(BaseModel):
|
||||
|
||||
def __setattr__(self, name: str, value: Any) -> None:
|
||||
"""Allow attribute setting like argparse.Namespace."""
|
||||
if name.startswith('_') or name in self.model_fields:
|
||||
if name.startswith('_') or name in type(self).model_fields:
|
||||
super().__setattr__(name, value)
|
||||
else:
|
||||
if not hasattr(self, 'extra_attrs'):
|
||||
@@ -119,7 +119,7 @@ class OCROptions(BaseModel):
|
||||
|
||||
def __delattr__(self, name: str) -> None:
|
||||
"""Allow attribute deletion like argparse.Namespace."""
|
||||
if name in self.model_fields:
|
||||
if name in type(self).model_fields:
|
||||
super().__delattr__(name)
|
||||
elif name in self.extra_attrs:
|
||||
del self.extra_attrs[name]
|
||||
@@ -148,7 +148,7 @@ class OCROptions(BaseModel):
|
||||
ns = Namespace()
|
||||
|
||||
# Add pydantic fields
|
||||
for field_name in self.model_fields:
|
||||
for field_name in type(self).model_fields:
|
||||
field_value = getattr(self, field_name)
|
||||
setattr(ns, field_name, field_value)
|
||||
|
||||
@@ -240,6 +240,9 @@ class OCROptions(BaseModel):
|
||||
# For hOCR API, output_file might not be present
|
||||
if 'output_folder' in data and 'output_file' not in data:
|
||||
data['output_file'] = '/dev/null' # Placeholder
|
||||
# Handle pdf_renderer 'auto' case
|
||||
if data.get('pdf_renderer') == 'auto':
|
||||
data['pdf_renderer'] = 'hocr' # Default to hocr for auto
|
||||
return data
|
||||
|
||||
model_config = ConfigDict(
|
||||
|
||||
@@ -58,13 +58,18 @@ def _image_to_ocr_text(
|
||||
) -> tuple[Path, Path]:
|
||||
"""Run OCR engine on image to create OCR PDF and text file."""
|
||||
options = page_context.options
|
||||
if options.pdf_renderer.startswith('hocr'):
|
||||
# Handle 'auto' pdf_renderer by defaulting to 'hocr'
|
||||
pdf_renderer = options.pdf_renderer
|
||||
if pdf_renderer == 'auto':
|
||||
pdf_renderer = 'hocr'
|
||||
|
||||
if pdf_renderer.startswith('hocr'):
|
||||
hocr_out, text_out = ocr_engine_hocr(ocr_image_out, page_context)
|
||||
ocr_out = render_hocr_page(hocr_out, page_context)
|
||||
elif options.pdf_renderer == 'sandwich':
|
||||
elif pdf_renderer == 'sandwich':
|
||||
ocr_out, text_out = ocr_engine_textonly_pdf(ocr_image_out, page_context)
|
||||
else:
|
||||
raise NotImplementedError(f"pdf_renderer {options.pdf_renderer}")
|
||||
raise NotImplementedError(f"pdf_renderer {pdf_renderer}")
|
||||
return ocr_out, text_out
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user