o
    e#                     @  s  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Zddlm	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mZmZ ddlZddlZdd
lmZ eeZ eej!edk rne"ddZ#neej!edk re"ej$jdZ#n
e"ej$jej%j&dZ#edee dZ'G dd dee' Z(G dd de)Z*d;ddZ+d<ddZ,d=d$d%Z-d>d(d)Z.d?d+d,Z/d@d-d.Z0dAd0d1Z1dBd3d4Z2d5d6 Z3d7d8 Z4d9d: Z5dS )CzSupport functions.    )annotationsN)Iterable)suppress)StringIO)iscloseisfinite)Path)AnyGenericSequenceSupportsFloatSupportsRoundTypeVar)Versionz0.4.0T)Zwithout_pdfwz0.4.3)engine)r   ZrotationT)boundc                   @  s   e Zd ZU dZded< ded< dZd,ddZdZd-ddZd.ddZ	e
dd Zed/ddZed/ddZ	d0d1ddZd2d d!Zd3d$d%Zd&d' Zd(d) Zd*d+ ZdS )4
ResolutionzThe 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                 C  s   || _ || _d S Nr   )selfr   r    r   @D:\Projects\ConvertPro\env\Lib\site-packages\ocrmypdf/helpers.py__init__3   s   
zResolution.__init__gMb`?ndigitsintreturnc                 C  s   t t| j|t| j|S r   )r   roundr   r   )r   r   r   r   r   r   ;   s   zResolution.roundResolution[int]c                 C  s   t tt| jtt| jS r   )r   r   r   r   r   r   r   r   r   to_int>   s   zResolution.to_intc                 C  s   t ||| jdS )N)Zrel_tol)r   CONVERSION_ERROR)clsabr   r   r   _iscloseA      zResolution._iscloseboolc                 C  s   |  | j| jS r   )r'   r   r   r!   r   r   r   	is_squareE   r(   zResolution.is_squarec                 C  s0   t | jtrt | jtrt| jot| jS dS )NT)
isinstancer   r   r   r   r!   r   r   r   	is_finiteI   s   zResolution.is_finiteNvalsIterable[Any]yvalsIterable[Any] | Nonec                 C  sh   |d urt t| jg|R  t| jg|R  S | j| j}}|D ]\}}t||}t||}q t ||S r   )r   maxr   r   )r   r-   r/   Zmax_xZmax_yr   r   r   r   r   take_maxO   s   &

zResolution.take_maxResolution[T]c                 C  s   t | j| jS r   )r   r   r   r!   r   r   r   	flip_axisZ   s   zResolution.flip_axisidxint | slicec                 C  s   | j | jf| S r   r   )r   r5   r   r   r   __getitem__]   s   zResolution.__getitem__c                 C  s   | j dd| jdS )Nfr   r   r!   r   r   r   __str__`      zResolution.__str__c                 C  s   d| j  d| j dS )NzResolution(r   z dpi)r   r!   r   r   r   __repr__c   r:   zResolution.__repr__c                 C  sL   t |trt|dkrt| }t |tstS | | j|jo%| | j|jS )N   )r+   tuplelenr   NotImplementedr'   r   r   )r   otherr   r   r   __eq__f   s
   
 zResolution.__eq__)r   r   r   r   )r   r   r   r   )r   r    )r   r)   r   )r-   r.   r/   r0   r   r   )r   r3   )r5   r6   r   r   )__name__
__module____qualname____doc____annotations__	__slots__r   r#   r   r"   classmethodr'   propertyr*   r,   r2   r4   r7   r9   r;   rA   r   r   r   r   r   '   s,   
 





r   c                   @  s   e Zd ZdZdS )
NeverRaisez!An exception that is never raisedN)rB   rC   rD   rE   r   r   r   r   rJ   n   s    rJ   
input_fileos.PathLikesoft_link_namec                 C  s   t | } t |}| |krtd dS t j|r-t j|s(t| dt | t j	| s:t
d|  t jdkrGt| | dS td| | t t j| | dS )aq  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)rK   rM   r   r   r   safe_symlinkr   s$   
	


r`   file1file2c                 C  s    t jdkr	| |kS t j| |S )NrN   )rO   rZ   rS   samefile)ra   rb   r   r   r   rc      s   
rc   thingr	   r   r)   c                 C  s   t | to
t | t S )z1Is this is an iterable type, other than a string?)r+   r   str)rd   r   r   r   is_iterable_notstr   s   rf   seqr   c                 C  s    t dd t| | dd D S )z*Does this sequence increase monotonically?c                 s  s    | ]	\}}||kV  qd S r   r   ).0r%   r&   r   r   r   	<genexpr>   s    zmonotonic.<locals>.<genexpr>   N)allzip)rg   r   r   r   	monotonic   s    rm   r   c                 C  s   t tjt| dd S )z?Get one-based page number implied by filename (000002.pdf -> 2)r      )r   rO   rS   basenamerP   )rK   r   r   r   page_number   s   rp   c                   C  s,   zt  W S  ty   Y nw td dS )z%Returns number of CPUs in the system.zHCould not get CPU count. Assuming one (1) CPU. Use -j N to set manually.rj   )multiprocessing	cpu_countNotImplementedErrorwarningswarnr   r   r   r   available_cpu_count   s   
rv   	test_filec              
   C  s   z[t | }| r|jdd}| r(| r(tjt|tjtjtj	v dW S z|
d}W n ty:   Y W dS w |  tt |  W d   W dS 1 sTw   Y  W dS  ttfy| } zt| tt| W Y d}~dS d}~ww )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)Zeffective_idswbNT)r   
is_symlinkresolverX   is_filerO   accessrP   W_OKsupports_effective_idsopenOSErrorcloser   rW   RuntimeErrorrQ   r]   errorre   )rw   pfper   r   r   is_file_writable   s8   




r   r   c              
   C  s  zt | }W n t jy  } zt| W Y d}~dS d}~ww |` | }d}|D ]}d| v r<t| d}q,d|v rAq,t| d}q,t }d}z|	| W n t
t jfya   Y nw | }|rmt| |rz|sz	 W d   dS 	 W d   dS 1 sw   Y  dS )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.
    NFTr   zG/DecodeParms: operation for dictionary attempted on object of type null )pikepdfr   ZPdfErrorrQ   r   checklowerrR   r   Zcheck_linearizationr   ZForeignObjectErrorgetvalue)rK   Zpdfr   messagessuccessmsgsioZlinearize_msgsr   r   r   	check_pdf   sF   



$r   c                 C  s   t |t| |S )zBClamps the value of ``n`` to between ``smallest`` and ``largest``.)r1   min)nZsmallestZlargestr   r   r   clamp  r(   r   c                 C  s*   | j dd D ]}| | |  qdS )z9Remove all log handlers, usually used in a child process.N)handlersremoveHandlerr   )loggerhandlerr   r   r   remove_all_log_handlers  s   

r   c                   C  s   d S r   r   r   r   r   r   pikepdf_enable_mmap  s   
r   )rK   rL   rM   rL   )ra   rL   rb   rL   )rd   r	   r   r)   )rg   r   r   r)   )rK   rL   r   r   )r   r   )rw   rL   r   r)   )rK   r   r   r)   )6rE   
__future__r   loggingrq   rO   r[   rt   collections.abcr   
contextlibr   ior   mathr   r   pathlibr   typingr	   r
   r   r   r   r   Zimg2pdfr   Zpackaging.versionr   	getLoggerrB   rQ   __version__dictZIMG2PDF_KWARGSZEngineZRotationZifvalidr   r   	ExceptionrJ   r`   rc   rf   rm   rp   rv   r   r   r   r   r   r   r   r   r   <module>   sL    
G

)





#.