
    TAi,                    *   S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKJ	r	J
r
Jr  SSKJr  SSKJr  SSKJr  SSKJrJr  SS	KJr  SS
KJr  SSKJrJrJr  SSKrSSKrSSKJ r   \RB                  " \"5      r#\$" \RJ                  R<                  \RL                  RN                  S9r(\" S\)\*\5      r+ " S S\\+   5      r,\ " SS9 " S S\-5      5       r.S"S jr/S#S jr0S$S jr1S%S jr2S&S jr3S'S jr4S(S jr5S)S jr6S*S jr7S+S jr8S,S jr9S-S  jr:S-S! jr;g).zSupport functions.    )annotationsN)CallableIterableSequence)suppress)Decimal)StringIO)iscloseisfinite)Path)harmonic_mean)AnyGenericTypeVar)
deprecated)enginerotationTc                  
   \ rS rSr% SrS\S'   S\S'   SrSS jrSrSS	 jr	SS
 jr
\S 5       r\SS j5       r\SS j5       rSS jr        SS jr S     SS jjr S     SS jjrS S jrS!S jrS rS rS rSrg)"
Resolution&   zThe number of pixels per inch in each 2D direction.

Resolution objects are considered "equal" for == purposes if they are
equal to a reasonable tolerance.
r   xyr   r   c                    Xl         X l        g)zConstruct a Resolution object.Nr   )selfr   r   s      L/var/www/html/land-ocr/venv/lib/python3.13/site-packages/ocrmypdf/helpers.py__init__Resolution.__init__2   s        gMb`?c                j    [        [        U R                  U5      [        U R                  U5      5      $ )z)Round to ndigits after the decimal point.)r   roundr   r   )r   ndigitss     r   r"   Resolution.round;   s%    %0%2HIIr    c                    [        [        [        U R                  5      5      [        [        U R                  5      5      5      $ )zRound to nearest integer.)r   intr"   r   r   r   s    r   to_intResolution.to_int?   s)    #eDFFm,c%-.@AAr    c                *    [        XU R                  S9$ )N)rel_tol)r
   CONVERSION_ERROR)clsabs      r   _iscloseResolution._iscloseC   s    qS%9%9::r    c                N    U R                  U R                  U R                  5      $ )z*True if the resolution is square (x == y).)r0   r   r   r'   s    r   	is_squareResolution.is_squareG   s     }}TVVTVV,,r    c                d    [        U R                  5      =(       a    [        U R                  5      $ )z(True if both x and y are finite numbers.)r   r   r   r'   s    r   	is_finiteResolution.is_finiteL   s!     4HTVV$44r    c                h    [        [        U R                  5      [        U R                  5      /5      $ )a  Return the harmonic mean of x and y as a 1D approximation.

In most cases, Resolution is 2D, but typically it is "square" (x == y) and
can be approximated as a single number. When not square, the harmonic mean
is used to approximate the 2D resolution as a single number.
)r   floatr   r   r'   s    r   	to_scalarResolution.to_scalarQ   s$     eDFFmU466];<<r    c                    Ub.  [        U" U R                  /UQ76 U" U R                  /UQ76 5      $ U R                  U R                  pTU H  u  pgU" Xd5      nU" Xu5      nM     [        XE5      $ zEReturn a new Resolution object with the maximum resolution of inputs.)r   r   r   )r   valsyvalscmpcmp_xcmp_yr   r   s           r   _take_minmaxResolution._take_minmaxZ   sm     c$&&040#dff2Eu2EFFvvtvvuDAMEME  %''r    Nc                .    U R                  X[        5      $ r=   )rC   maxr   r>   r?   s      r   take_maxResolution.take_maxf          c22r    c                .    U R                  X[        5      $ )zEReturn a new Resolution object with the minimum resolution of inputs.)rC   minrG   s      r   take_minResolution.take_minl   rJ   r    c                B    [        U R                  U R                  5      $ )z4Return a new Resolution object with x and y swapped.)r   r   r   r'   s    r   	flip_axisResolution.flip_axisr   s    $&&$&&))r    c                8    U R                   U R                  4U   $ )zSupport [0] and [1] indexing.r   )r   idxs     r   __getitem__Resolution.__getitem__v   s    $$r    c                <    U R                   S SU R                  S 3$ )z1Return a string representation of the resolution.f   ×r   r'   s    r   __str__Resolution.__str__z   s    &&2dffQZ((r    c                @    SU R                   < SU R                  < S3$ )z"Return a repr() of the resolution.zResolution(z, )r   r'   s    r   __repr__Resolution.__repr__~   s    TVVJb
!44r    c                6   [        U[        5      (       a  [        U5      S:X  a  [        U6 n[        U[        5      (       d  [        $ U R                  U R                  UR                  5      =(       a&    U R                  U R                  UR                  5      $ )z=Return True if the resolution is equal to another resolution.   )
isinstancetuplelenr   NotImplementedr0   r   r   )r   others     r   __eq__Resolution.__eq__   sg    eU##E
a&E%,,!!}}TVVUWW-P$--2PPr    )r   r   r   r   )r#   r&   returnr   )rh   zResolution[int]rh   bool)rh   r9   )r>   Iterable[Any]r?   Iterable[Any] | Noner@   r   rh   r   N)r>   rk   r?   rl   rh   r   )rh   zResolution[T])rS   zint | slicerh   r   )__name__
__module____qualname____firstlineno____doc____annotations__	__slots__r   r,   r"   r(   classmethodr0   propertyr3   r6   r:   rC   rH   rM   rP   rT   rY   r]   rf   __static_attributes__ r    r   r   r   &   s     	DDI JB ; ; - - 5 5=
(!
(*>
(EM
(	
( BF3!3*>3	3 BF3!3*>3	3*%)5Qr    r   z15.4.0)deprecated_inc                      \ rS rSrSrSrg)
NeverRaise   z"An exception that is never raised.rx   N)rn   ro   rp   rq   rr   rw   rx   r    r   r{   r{      s    ,r    r{   c                   [         R                  " U 5      n [         R                  " U5      nX:X  a  [        R                  S5        g[         R                  R                  U5      (       aH  [         R                  R                  U5      (       d  [        U S35      e[         R                  " U5        [         R                  R                  U 5      (       d  [        SU  35      e[         R                  S:X  a  [        R                  " X5        g[        R                  SX5        [         R                  " [         R                  R!                  U 5      U5        g)a]  Create a symbolic link at ``soft_link_name``, which references ``input_file``.

Think of this as copying ``input_file`` to ``soft_link_name`` with less overhead.

Use symlinks safely. Self-linking loops are prevented. On Windows, file copy is
used since symlinks may require administrator privileges. An existing link at the
destination is removed.
z]No symbolic link created. You are using the original data directory as the working directory.Nz exists and is not a linkz%trying to create a broken symlink to ntzos.symlink(%s, %s))osfspathlogwarningpathlexistsislinkFileExistsErrorunlinkexistsFileNotFoundErrornameshutilcopyfiledebugsymlinkabspath)
input_filesoft_link_names     r   safe_symlinkr      s     :&JYY~.N #(	
 	 
ww~&&ww~~n--!^$44M"NOO
		.!77>>*%%"G
| TUU	ww$
3II"J? JJrwwz*N;r    c                p    [         R                  S:X  a  X:H  $ [         R                  R                  X5      $ )zpReturn True if two files are the same file.

Attempts to account for different relative paths to the same file.
r~   )r   r   r   samefile)file1file2s     r   r   r      s,    
 
ww$~ww--r    c                Z    [        U [        5      =(       a    [        U [        5      (       + $ )z1Is this is an iterable type, other than a string?)ra   r   str)things    r   is_iterable_notstrr      s    eX&Ez%/E+EEr    c           	     >    [        S [        X SS 5       5       5      $ )z*Does this sequence increase monotonically?c              3  .   #    U  H  u  pX!:  v   M     g 7frm   rx   ).0r.   r/   s      r   	<genexpr>monotonic.<locals>.<genexpr>   s     3!2qu!2s      N)allzip)seqs    r   	monotonicr      s    3Sab'!2333r    c                    [        [        R                  R                  [        R                  " U 5      5      SS 5      $ )z@Get one-based page number implied by filename (000002.pdf -> 2).r      )r&   r   r   basenamer   )r   s    r   page_numberr      s,    rww		* 56q;<<r    c                 |     [         R                  " 5       $ ! [         a     Of = f[        R                  " S5        g)z%Returns number of CPUs in the system.zHCould not get CPU count. Assuming one (1) CPU. Use -j N to set manually.r   )multiprocessing	cpu_countNotImplementedErrorwarningswarnrx   r    r   available_cpu_countr      s<    ((** MMR s    
$$c                $    [        U 5      nUR                  5       (       a  UR                  SS9nUR                  5       (       a  UR	                  5       (       d$  UR                  [        R                  5      (       aW  [        R                  " [        R                  " U5      [        R                  [        R                  [        R                  ;   S9$  UR                  S5      nUR                  5         [        [        5         UR!                  5         SSS5        g! , (       d  f       g= f! [         a     gf = f! [        ["        4 a=  n[$        R'                  U5        [$        R)                  [+        U5      5         SnAgSnAff = f)zIntentionally racy test if target is writable.

We intend to write to the output file if and only if we succeed and
can replace it atomically. Before doing the OCR work, make sure
the location is writable.
F)strict)effective_idswbNT)r   
is_symlinkresolver   is_filer   r   devnullaccessr   W_OKsupports_effective_idsopencloser   OSErrorr   RuntimeErrorr   r   errorr   )	test_filepfpes       r   is_file_writabler      s   O<<>>			'A 88::199;;!**RZZ*@*@99		!!yyB,E,EE 	B HHJ'"
 # #"  		 \" 		!		#a&sZ   CE D2 (E D!E !
D/+E /E 2
D?<E >D??E F3F

Fc                2    [         R                  " U 5      nU   [        R                  " 5          [        R                  " SSS9  UR                  5       nSSS5        SnW HO  nSUR                  5       ;   a  [        R                  U5        SnM0  SU;   a  M8  [        R                  U5        SnMQ     [        5       nS	n UR                  U5        UR                  5       nU(       a  [        R                  U5        U(       a  U(       d
   SSS5        g SSS5        g! , (       d  f       N= f! [        [         R                  4 a     NOf = f! , (       d  f       g= f! [         R                   a  n[        R                  U5         SnAgSnAff = f)
zCheck if a PDF complies with the PDF specification.

Checks for proper formatting and proper linearization. Uses pikepdf (which in
turn, uses QPDF) to perform the checks.
ignorezpikepdf.*JBIG2.*)messageNTr   FzG/DecodeParms: operation for dictionary attempted on object of type null )pikepdfr   r   catch_warningsfilterwarningscheck_pdf_syntaxlowerr   r   r   r	   check_linearizationgetvaluer   ForeignObjectErrorPdfError)r   pdfmessagessuccessmsgsiolinearize_msgsr   s           r   	check_pdfr     sK   'll:&
 ((*'':MN//1 + Gciik)IIcN#G#&)* KK$#G   *CN	0 '', "%!KK/~A SB C S**. !'"<"<= 1 S	  		!sd   E# E&D!A+ED2;EE!
D/	+E2EEEE
E #F7FFc                ,    [        U[        X5      5      $ )zBClamps the value of ``n`` to between ``smallest`` and ``largest``.)rF   rL   )nsmallestlargests      r   clampr   2  s    xQ))r    c                r    U R                   SS  H$  nU R                  U5        UR                  5         M&     g)a@  Remove all log handlers, usually used in a child process.

The child process inherits the log handlers from the parent process when
a fork occurs. Typically we want to remove all log handlers in the child
process so that the child process can set up a single queue handler to
forward log messages to the parent process.
N)handlersremoveHandlerr   )loggerhandlers     r   remove_all_log_handlersr   7  s.     ??1%W% &r    c                     [         R                  R                  S5        [        R	                  S[         R                  R                  5       (       a  SOS-   5        g! [         a    [        R	                  S5         gf = f)zEnable pikepdf memory mapping.Tzpikepdf mmap enableddisabledzpikepdf mmap not availableN)r   _coreset_access_default_mmapr   r   get_access_default_mmapAttributeErrorrx   r    r   pikepdf_enable_mmapr   D  sh    0--d3		 ==88:: 		
  0		./0s   AA B Bc                 4    [        S5      R                  5       $ )z<Returns True if we seem to be running in a Docker container.z/.dockerenv)r   r   rx   r    r   running_in_dockerr   T  s    %%''r    c                 `     [        S5      R                  5       n SU ;   $ ! [         a     gf = f)z:Returns True if we seem to be running in a Snap container.z/proc/self/cgroupzsnap.ocrmypdfF)r   	read_textr   )cgroup_texts    r   running_in_snapr   Y  s8    ./99;+-- s     
--)r   os.PathLiker   r   rh   None)r   r   r   r   rh   rj   )r   r   rh   rj   )r   r   rh   rj   )r   r   rh   r&   )rh   r&   )r   r   rh   rj   )r   r   rh   rj   )r   r   r   r   r   r   rh   r   )r   zlogging.Loggerrh   r   )rh   r   ri   )<rr   
__future__r   loggingr   r   r   r   collections.abcr   r   r   
contextlibr   decimalr   ior	   mathr
   r   pathlibr   
statisticsr   typingr   r   r   img2pdfr   deprecationr   	getLoggerrn   r   dictEngineRotationifvalidIMG2PDF_KWARGSr9   r&   r   r   	Exceptionr{   r   r   r   r   r   r   r   r   r   r   r   r   r   rx   r    r   <module>r     s    "   	   8 8    "  $    "!W^^33g>N>N>V>VW CW%bQ bQJ (#- - $-&<R.F
4
=
	 F-`*

0 (
r    