
    j5j_%                    &   d Z ddlmZ ddlZddlZddlZddlZddlmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ ddlZdd	lmZ ddlZdd
lmZmZmZ ddlmZ ddlmZ ddlmZ ddlm Z  erddl!m"Z" ddl#m$Z$ ddl%m&Z&  G d d      Z'	 d	 	 	 ddZ(ddgZ)y)z5Plugin manager using pluggy with type-safe interface.    )annotationsN)ArgumentParser)Sequence)Handler)Path)TYPE_CHECKING)	BaseModel)Executor
PdfContext
pluginspec)
OcrOptions)ProgressBar)
Resolution)	OcrEngine)Image)PageContext)PdfInfoc                  b   e Zd ZdZdd	 	 	 ddZedd       Zd Zd Zd Z	dd	Z
dd
ZddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d dZ	 	 	 	 	 	 d!dZ	 	 	 	 	 	 d"dZ	 	 	 	 	 	 	 	 d#dZddd$dZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d%dZ	 	 	 	 	 	 	 	 	 	 	 	 d&dZd'dZd(dZd)dZd*dZd+dZd,dZy)-OcrmypdfPluginManagera  Type-safe wrapper around pluggy.PluginManager.

    Capable of reconstructing itself in child workers via pickle.

    This class provides type-safe methods for all hooks defined in pluginspec.py,
    removing the need for unsafe `hook.method_name()` calls.
    T)builtinsc                   || _         || _        || _        || _        t	        j
                  |i || _        | j                          y )N)
_init_args_init_kwargs_plugins	_builtinspluggyPluginManager_pm_setup_plugins)selfpluginsr   argskwargss        N/var/www/html/qr/venv/lib/python3.12/site-packages/ocrmypdf/_plugin_manager.py__init__zOcrmypdfPluginManager.__init__,   sD     "!''88    c                    | j                   S )zAccess the underlying pluggy.PluginManager for advanced use cases.

        This is useful for plugins that need to call methods like set_blocked()
        in their initialize hook.
        )r   r    s    r$   r   zOcrmypdfPluginManager.pluggy:   s     xxr&   c                t    t        | j                  | j                  | j                  | j                        }|S )N)	init_argsr!   r   init_kwargs)dictr   r   r   r   r    states     r$   __getstate__z"OcrmypdfPluginManager.__getstate__C   s1    ooMM^^))	
 r&   c                F     | j                   |d   |d   |d   d|d    y )Nr*   r!   r   r!   r   r+   )r%   r-   s     r$   __setstate__z"OcrmypdfPluginManager.__setstate__L   s7    ;)$:&	
 M"		
r&   c                N   | j                   j                  t               | j                  rzt	        t        j                  t        j                  j                              D ]A  }d|j                   }t        j                  |      }| j                   j                  |       C | j                   j                  d       | j                  D ]  }t!        |t"              s|j%                  d      rt#        |      j&                  }t        j(                  j+                  ||      }t        j(                  j-                  |      }|t.        j0                  |<   |j2                  j5                  |       nt        j                  |      }| j                   j                  |        y )Nzocrmypdf.builtin_plugins.ocrmypdfz.py)r   add_hookspecsr   r   sortedpkgutiliter_modulesr4   builtin_plugins__path__name	importlibimport_moduleregisterload_setuptools_entrypointsr   
isinstancer   endswithstemutilspec_from_file_locationmodule_from_specsysmodulesloaderexec_module)r    moduler;   module_namespecs        r$   r   z$OcrmypdfPluginManager._setup_pluginsT   s8   z* >> $$X%=%=%F%FG * 36;;-@"006!!&)* 	,,Z8 MM 	&D$%u)="4joo ~~==k4P"88>+1K(''/ #006HHf%	&r&   c                J    | j                   j                  j                         S )z@Returns a custom logging handler for progress bar compatibility.)r   hookget_logging_consoler(   s    r$   rO   z)OcrmypdfPluginManager.get_logging_consolew   s    xx}}0022r&   c               N    | j                   j                  j                  |      S )z,Returns an executor for parallel processing.)progressbar_class)r   rN   get_executor)r    rQ   s     r$   rR   z"OcrmypdfPluginManager.get_executor{   s    xx}}))<M)NNr&   c                J    | j                   j                  j                         S )zReturns a progress bar class.)r   rN   get_progressbar_classr(   s    r$   rT   z+OcrmypdfPluginManager.get_progressbar_class   s    xx}}2244r&   c               b    | j                   j                  j                  |||||||||	|
|      S )z4Rasterize one page of a PDF at specified resolution.)
input_fileoutput_fileraster_device
raster_dpipagenopage_dpirotationfilter_vectorstop_on_soft_erroroptionsuse_cropbox)r   rN   rasterize_pdf_page)r    rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   s               r$   ra   z(OcrmypdfPluginManager.rasterize_pdf_page   sE      xx}}//!#'!'1# 0 
 	
r&   c               P    | j                   j                  j                  ||      S )z*Filter the image before it is sent to OCR.)pageimage)r   rN   filter_ocr_image)r    rc   rd   s      r$   re   z&OcrmypdfPluginManager.filter_ocr_image   s"     xx}}--4u-EEr&   c               P    | j                   j                  j                  ||      S )z?Filter the whole page image before it is inserted into the PDF.)rc   image_filename)r   rN   filter_page_image)r    rc   rg   s      r$   rh   z'OcrmypdfPluginManager.filter_page_image   s"     xx}}..D.XXr&   c                   | j                   j                  j                  |||      }|t        d      ||k7  rt        d      |S )z/Convert a filtered whole page image into a PDF.)rc   rg   
output_pdfzNo PDF producedz&filter_pdf_page must return output_pdf)r   rN   filter_pdf_page
ValueError)r    rc   rg   rj   results        r$   rk   z%OcrmypdfPluginManager.filter_pdf_page   sT     ..n / 
 >.//ZEFFr&   Nr_   c               l    | j                   j                  j                  |      }|t        d      |S )zReturns an OcrEngine to use for processing.

        Args:
            options: OcrOptions to pass to the hook for engine selection.
        rn   zNo OCR engine selected)r   rN   get_ocr_enginerl   )r    r_   rm   s      r$   rp   z$OcrmypdfPluginManager.get_ocr_engine   s5     --g->>566r&   c          
     \    | j                   j                  j                  ||||||||      S )zGenerate a PDF/A file.)	pdf_pagespdfmarkrW   contextpdf_version	pdfa_partrQ   r^   )r   rN   generate_pdfa)	r    rr   rs   rW   rt   ru   rv   rQ   r^   s	            r$   rw   z#OcrmypdfPluginManager.generate_pdfa   s<     xx}}**##/1 + 	
 		
r&   c               f    | j                   j                  j                  |||||      }||g fS |S )z$Optimize a PDF after OCR processing.)	input_pdfrj   rt   executor	linearize)r   rN   optimize_pdf)r    ry   rj   rt   rz   r{   rm   s          r$   r|   z"OcrmypdfPluginManager.optimize_pdf   sF     ++! , 
 >b= r&   c               N    | j                   j                  j                  |      S )z:Returns whether optimization is enabled for given context.)rt   )r   rN   is_optimization_enabled)r    rt   s     r$   r~   z-OcrmypdfPluginManager.is_optimization_enabled   s    xx}}44W4EEr&   c               N    | j                   j                  j                  |      S )zCalled when plugins are first loaded.

        Args:
            plugin_manager: The underlying pluggy.PluginManager, allowing
                plugins to call methods like set_blocked().
        )plugin_manager)r   rN   
initialize)r    r   s     r$   r   z OcrmypdfPluginManager.initialize   s      xx}}''~'FFr&   c               N    | j                   j                  j                  |      S )z5Allows plugins to add command line and API arguments.)parser)r   rN   add_options)r    r   s     r$   r   z!OcrmypdfPluginManager.add_options  s    xx}}(((77r&   c                J    | j                   j                  j                         S )z0Returns plugin option models keyed by namespace.)r   rN   register_optionsr(   s    r$   r   z&OcrmypdfPluginManager.register_options  s    xx}}--//r&   c               N    | j                   j                  j                  |      S )z)Called to validate options after parsing.rn   )r   rN   check_options)r    r_   s     r$   r   z#OcrmypdfPluginManager.check_options	  s    xx}}**7*;;r&   c               P    | j                   j                  j                  ||      S )z;Called to validate options and pdfinfo after PDF is loaded.)pdfinfor_   )r   rN   validate)r    r   r_   s      r$   r   zOcrmypdfPluginManager.validate  s     xx}}%%gw%GGr&   )r!   zSequence[str | Path]r   bool)returnpluggy.PluginManager)r   zHandler | None)rQ   ztype[ProgressBar]r   zExecutor | None)r   type[ProgressBar] | None)rV   r   rW   r   rX   strrY   r   rZ   intr[   zResolution | Noner\   z
int | Noner]   r   r^   r   r_   OcrOptions | Noner`   r   r   Path | None)rc   r   rd   zImage.Imager   zImage.Image | None)rc   r   rg   r   r   r   )rc   r   rg   r   rj   r   r   r   )r_   r   r   r   )rr   z
list[Path]rs   r   rW   r   rt   r   ru   r   rv   r   rQ   r   r^   r   r   r   )ry   r   rj   r   rt   r   rz   r
   r{   r   r   ztuple[Path, Sequence[str]])rt   r   r   zbool | None)r   r   r   
list[None])r   r   r   r   )r   z list[dict[str, type[BaseModel]]])r_   r   r   r   )r   r   r_   r   r   r   )__name__
__module____qualname____doc__r%   propertyr   r/   r2   r   rO   rR   rT   ra   re   rh   rk   rp   rw   r|   r~   r   r   r   r   r    r&   r$   r   r   #   s	    	 & 	  
&F3O5
 
 	

 
 
 
 $
 
 
 !
 #
 
 

<F"F+6F	FY"Y48Y	Y"48FJ	 >B 	
 
 	

 
 
 
 
 4
 !
 

0  	
    
$*FG80<Hr&   r   c                2    t        d| | |      S g |      S )Nr4   )project_namer!   r   )r   r1   s     r$   get_plugin_managerr     s/     !". 46 r&   r   )NT)r!   zSequence[str | Path] | Noner   r   )*r   
__future__r   r<   importlib.utilr7   rF   argparser   collections.abcr   loggingr   pathlibr   typingr   r   pydanticr	   ocrmypdf.builtin_pluginsr4   r
   r   r   ocrmypdf._optionsr   ocrmypdf._progressbarr   ocrmypdf.helpersr   ocrmypdf.pluginspecr   PILr   ocrmypdf._jobcontextr   ocrmypdf.pdfinfor   r   r   __all__r   r&   r$   <module>r      s    < "    
 # $        5 5 ( - ' )0(lH lH` ;?( #$8
9r&   