
    j5j-                    ~   d Z ddlm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ZddlmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZmZmZmZ ddlmZmZmZmZ ddl m!Z!  ejD                  e#      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,ddZ-ddZ.	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ/y) z/Validate a work order from API or command line.    )annotationsN)Sequence)Path)copyfileobj)DEFAULT_ROTATE_PAGES_THRESHOLD)unpaper)
OcrOptionsProcessingMode)OcrmypdfPluginManager)BadArgsErrorInputFileErrorMissingDependencyErrorOutputFileAccessError)is_file_writablerunning_in_dockerrunning_in_snapsafe_symlink)check_external_programc                 V    t         j                  dk  rt        j                  d       y y )Nl        zYou are running OCRmyPDF in a 32-bit (x86) Python interpreter. This is not supported. 32-bit does not have enough address space to process large files. Please use a 64-bit (x86-64) version of Python.)sysmaxsizelogwarning     J/var/www/html/qr/venv/lib/python3.12/site-packages/ocrmypdf/_validation.pycheck_platformr   '   s$    
{{e>	
 r   c                   ddh}|t        | j                        z  }|rt        ddj                  |       d      |sy t        | j                        t        |      z
  }|r)dj                  d |D              }d| d	}t	        |      y )
Nequosdz^The following languages are for Tesseract's internal use and should not be issued explicitly: , z-
Remove them from the -l/--language argument.
c              3      K   | ]  }|  y wNr   ).0langs     r   	<genexpr>z*check_options_languages.<locals>.<genexpr>D   s     AtdAs   zOOCR engine does not have language data for the following requested languages: 
a  
Please install the appropriate language data for your OCR engine.

See the online documentation for instructions:
    https://ocrmypdf.readthedocs.io/en/latest/languages.html

Note: most languages are identified by a 3-letter ISO 639-2 Code.
For example, English is 'eng', German is 'deu', and Spanish is 'spa'.
Simplified Chinese is 'chi_sim' and Traditional Chinese is 'chi_tra'.
)set	languagesr   joinr   )optionsocr_engine_languagesDENIED_LANGUAGESblockedmissing_languages	lang_textmsgs          r   check_options_languagesr2   1   s     u~W%6%6!77G0yy!" #;;
 	
  G--.5I1JJIIA/@AA	&k 	 	 %S))! r   c                H   | j                   dk(  rV| j                  dk(  rt        d      | j                  t        j                  k(  rt        d      | j                  dz   | _         | j                   | j
                  k(  s| j                   | j                  k(  rt        d      y )N -z5--sidecar filename needed when output file is stdout.z?--sidecar filename needed when output file is /dev/null or NUL.z.txtz@--sidecar file must be different from the input and output files)sidecaroutput_filer   osdevnull
input_filer+   s    r   check_options_sidecarr<   V   s    $#%VWW  BJJ.Q  "--6',,,7CVCV0VN
 	
 1Wr   c                   | j                   rd| _        | j                  r| j                  st        d      | j                  t
        k7  r| j                  st        d      | j                  rt        ddt        j                  dd       y y )NTz&--clean is required for --unpaper-argsz7--rotate-pages is required for --rotate-pages-thresholdr   z6.1z--clean, --clean-final)programpackageversion_checkerneed_versionrequired_for)
clean_finalcleanunpaper_argsr   rotate_pages_thresholdr   rotate_pagesr   r   versionr;   s    r   check_options_preprocessingrI   e   sx    GMMCDD&&*HH$$TUU}}#OO1	
 r   c           	     t   | j                   t        j                  k7  ry| j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  d}t        d |j                         D              }|rt        ddj                  |             y)zReject options that cannot apply in strip mode.

    ``--mode strip`` removes the OCR text layer in place without rasterizing or
    running OCR, so image-processing and OCR-output options have no effect.
    N)z--deskewz--cleanz--clean-finalz--remove-backgroundz--rotate-pagesz--oversamplez--remove-vectorsz	--sidecarc              3  ,   K   | ]  \  }}|s	|  y wr$   r   )r%   namevalues      r   r'   z&check_options_strip.<locals>.<genexpr>   s     H;4%$Hs   
z--mode strip removes the OCR text layer without rasterizing or running OCR, so these options have no effect and are not allowed: r!   )moder
   
strip_textdeskewrD   rC   remove_backgroundrG   
oversampleremove_vectorsr6   sorteditemsr   r*   )r+   incompatibleuseds      r   check_options_striprX   y   s     ||~000NN== ,,&88!..**#22__	L H,*<*<*>HHDQyy!
 	
 r   c                Z    t                t        |        t        |        t        |        y r$   )r   rX   r<   rI   r;   s    r   _check_plugin_invariant_optionsrZ      s      '"(r   c                    |j                  |        |j                  |       j                  |       }t        | |       ddlm}  ||      }|j                  |        y )Nr;   r   )ValidationCoordinator)check_optionsget_ocr_enginer)   r2    ocrmypdf._validation_coordinatorr\   validate_all_options)r+   plugin_managerr,   r\   coordinators        r   _check_plugin_optionsrc      sd        1 *888ISS G%9: G'7K$$W-r   c                2    t        |        t        | |       y)aZ  Check options for validity and consistency.

    This function coordinates validation across the entire system:
    1. Core validation (platform, files, preprocessing)
    2. Plugin external dependency validation
    3. Plugin-specific validation (handled by plugin models)
    4. Cross-cutting validation (handled by validation coordinator)
    N)rZ   rc   )r+   ra   s     r   r]   r]      s     $G,'>2r   c                "   | j                   dk(  rWt        j                  d       |dz  }t        |d      5 }t	        t
        j                  j                  |       d d d        |dfS t        | j                   d      rn| j                   j                         st        d      t        j                  d       |dz  }t        |d      5 }t	        | j                   |       d d d        |dfS 	 |d	z  }t        | j                   |       |t        j                  | j                         fS # 1 sw Y   |dfS xY w# 1 sw Y   |dfS xY w# t        $ r?}d
| j                    }t               r|dz  }nt!               r|dz  }t        |      |d }~ww xY w)Nr5   z reading file from standard inputstdinwbreadablez!Input file stream is not readablezreading file from input streamstreamoriginzFile not found - z
Docker cannot access your working directory unless you explicitly share it with the Docker container and set up permissions correctly.
You may find it easier to use stdin/stdout:
	docker run -i --rm jbarlow83/ocrmypdf - - <input.pdf >output.pdf
z
Snap applications cannot access files outside of your home directory unless you explicitly allow it. You may find it easier to use stdin/stdout:
	snap run ocrmypdf - - <input.pdf >output.pdf
)r:   r   infoopenr   r   rf   bufferhasattrrh   r   r   r8   fspathFileNotFoundErrorr   r   )r+   work_foldertargetstream_bufferer1   s         r   create_input_fileru      s   S 34w&&$ 	9=		((-8	9w	##Z	0!!**, !DEE12x'&$ 	;=**M:	;x	- 8+F++V4299W%7%7888	9w	;x ! 	-%g&8&8%9:C " !" !%1,+	-s/   %D(D7,;E (D47E	F:F		Fc                8   | j                   dk(  r)t        j                  j                         rtt	        d      t        | j                   d      r%| j                   j                         s9t        d      t        | j                         st        d| j                    d      | j                  rmt        | j                   d      sV| j                   dk7  rFt        t        | j                               j                         rt        d| j                    d      y y y y )	Nr5   ztOutput was set to stdout '-' but it looks like stdout is connected to a terminal.  Please redirect stdout to a file.writablezOutput stream is not writablezOutput file location (z) is not a writable file.zOutput file already exists: z6
To overwrite it, omit the --no-overwrite / -n option.)r7   r   stdoutisattyr   rn   rw   r   r   no_overwriter   strexistsr;   s    r   check_requested_output_filer}      s   c!:: 
 
$$j	1""++-'(GHHg112#$W%8%8$99RS
 	

 	++Z83&W(()*113#*7+>+>*? @D D
 	
 4 ' 9 	r   c                R   |g }	 t        |      j                         j                  }t        |      j                         j                  }t	        j
                  |      5 }||t        |j                        z  z   }	d d d        ||z  }
||	z   z  }|dk  s|dk  ry g }h d}|D ]5  }t        | |d      s|j                  d|j                  dd       d       7 | j                  r|j                  d	       |j                  |       | j                  j                  d
      r|j                  d       | j                  r|j                  d       |rddj!                  |      z   dz   }nd}t"        j%                  d|
dd|        y # t        $ r Y y w xY w# 1 sw Y   !xY w)Ng?ia  >   rP   rR   rC   rQ   Fz--_r5   z! was issued, causing transcoding.z>--force-ocr (or --mode force) was issued, causing transcoding.pdfaz8PDF/A conversion was enabled. (Try `--output-type pdf`.)zPlugins were used.z#Possible reasons for this include:
r"   z@No reason for this increase is known.  Please report this issue.zThe output file size is z.2fu   × larger than the input file.
)r   statst_sizerp   pikepdfrl   lenpagesgetattrappendreplace	force_ocrextendoutput_type
startswithpluginsr*   r   r   )r+   r:   r7   optimize_messagesfile_overheadpage_overheadoutput_size
input_sizepreasonable_overheadratioreasonable_ratioreasonsimage_preprocargexplanations                   r   report_output_file_sizer     s     ;',,.66*%**,44
 
k	" Ka ,mc!''l.JJK *$E"j3F&FG$*u"4GM  7C'NNS[[c*++LM WXNN$%%%f-QR+,<tyy?QQTXXXKK
"5+-M-	O  K Ks   AF !F	FFF&)returnNone)r+   r	   r,   z	list[str]r   r   )r+   r	   r   r   )r+   r	   ra   r   r   r   )r+   r	   rq   r   r   ztuple[Path, str])Ni  i  )r+   r	   r:   r   r7   r   r   zSequence[str] | Noner   intr   r   r   r   )0__doc__
__future__r   loggingr8   r   collections.abcr   pathlibr   shutilr   r   ocrmypdf._defaultsr   ocrmypdf._execr   ocrmypdf._optionsr	   r
   ocrmypdf._plugin_managerr   ocrmypdf.exceptionsr   r   r   r   ocrmypdf.helpersr   r   r   r   ocrmypdf.subprocessr   	getLogger__name__r   r   r2   r<   rI   rX   rZ   rc   r]   ru   r}   r   r   r   r   <module>r      s  
 6 "  	 
 $    = " 8 :   7g!
"*"*/8"*	"*J

(
6)..)>.	.&
3*-Z
@ /3777 7 ,	7
 7 7 
7r   