o
    jwQ                     @   s(  U d Z dZd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
mZ ddlmZ ddlmZ dd	lmZ dd
lmZmZmZmZmZ ejdd dkrYddlmZ nddlmZ ejdkrkddlmZ nddlmZ ddlmZmZmZ eee e e f ee e e f ee e e f f Z!ee"d< ee e e e e e f Z#ee"d< ee Z$ee% Z&ee'e$f Z(dee' dee
 fddZ)de
de'fddZ*de'de'de'fddZ+dZ,d -e,Z.d!e. d" Z/dnd#e$d$ee0 de%fd%d&Z1d#e&de%fd'd(Z2d#e$de3fd)d*Z4d+e%de3fd,d-Z5d#e$ddfd.d/Z6d#e$d0ee% de%fd1d2Z7d#e&d3e0de%fd4d5Z8d#e$de%fd6d7Z9d8e!d9e!de!fd:d;Z:d#e$ddfd<d=Z;ed9e'de0fd>d?Z<ed9e%de%fd@d?Z<ed9e0de0fdAd?Z<d9ee0e'e%f dee0e%f fdBd?Z<dodDe'dEe0ddfdFdGZ=dDe'ddfdHdIZ>dJe'dKe'dLe'ddfdMdNZ?dJe'dKe'dLe'ddfdOdPZ@dQe'dLe'ddfdRdSZAdQe'dLe'ddfdTdUZBdVe'dWe'dXeddfdYdZZCdDe'd[e'ddfd\d]ZD	^dpd_e'd`eEe'ef daeEe'e'f dbe3ddf
dcddZFdee0de'fdfdgZGG dhdi diZHe	G djdk dkZIejJG dldm dmZKdS )qz"Utility functions for PDF library.zMathieu Fenniakzbiziqe@mathieu.fenniak.net    N)	dataclass)datetimetimezone)DEFAULT_BUFFER_SIZE)SEEK_CUR)Pattern)IOAnyOptionalUnionoverload   )   
   )	TypeAlias)r      )Self   )STREAM_TRUNCATED_PREMATURELYDeprecationErrorPdfStreamErrorTransformationMatrixTypeCompressedTransformationMatrixtextreturnc              	   C   s   | }| sd S | d   rd|  } | dr| d7 } | dddddd	} t| d| d
}|dkrB|t| d krB| d7 } dD ]#}zt| |}W n	 tyW   Y qDw | drd|jt	j
d}|  S td| )Nr   zD:)Zz0000r   +r   ' -   00)zD:%YzD:%Y%mzD:%Y%m%dz
D:%Y%m%d%HzD:%Y%m%d%H%MD:%Y%m%d%H%M%SzD:%Y%m%d%H%M%S%zz+0000)tzinfozCan not convert date: )isdigitendswithreplacemaxfindlenr   strptime
ValueErrorr   utc)r   orgtextifd r3   R/var/www/html/fyndo/pharma/fyndo/venv/lib/python3.10/site-packages/pypdf/_utils.pyparse_iso8824_dateO   s*   
	
r5   dtc                 C   s~   |  d}| jdur=|  }|dusJ t| }tt|d\}}|d }|dkr-dnd}|| |dd	|dd	7 }|S )
a  
    Convert a datetime object to PDF date string format.

    Converts datetime to the PDF date format D:YYYYMMDDHHmmSSOHH'mm
    as specified in the PDF Reference.

    Args:
        dt: A datetime object to convert.

    Returns:
        A date string in PDF format.
    r$   Ni  <   r   r   r!   02dr   )strftimer%   	utcoffsetinttotal_secondsdivmodabs)r6   date_stroffsetr<   hours	remainderminutessignr3   r3   r4   format_iso8824_dateo   s   

rE   header1header2c                 C   sf   d}g }| |v r| ||  ||v r| || t|dkr-td| d|d|t| S )N)z%PDF-1.3z%PDF-1.4z%PDF-1.5z%PDF-1.6z%PDF-1.7z%PDF-2.0r   zNeither z nor z are proper headers)appendindexr+   r-   r)   )rF   rG   versionspdf_header_indicesr3   r3   r4   _get_max_pdf_version_header   s   rL   )       	   
                 [   ]streammaxcharsc                 C   s>   d}	 |  d}| s|s	 |S ||7 }t||kr	 |S q)a<  
    Read non-whitespace characters and return them.

    Stops upon encountering whitespace or when maxchars is reached.

    Args:
        stream: The data stream from which was read.
        maxchars: The maximum number of bytes returned; by default unlimited.

    Returns:
        The data which was read.

    rS   Tr   )readisspacer+   )rV   rW   txttokr3   r3   r4   read_until_whitespace   s   
r\   c                 C   s(   |  d}|tv r|  d}|tv s	|S )z
    Find and read the next non-whitespace character (ignores whitespace).

    Args:
        stream: The data stream from which was read.

    Returns:
        The data which was read.

    r   rX   WHITESPACESrV   r[   r3   r3   r4   read_non_whitespace   s
   

r`   c                 C   s8   |  d}d}|tv r|d7 }|  d}|tv s|dkS )a  
    Similar to read_non_whitespace, but return a boolean if at least one
    whitespace character was read.

    Args:
        stream: The data stream from which was read.

    Returns:
        True if one or more whitespace was skipped, otherwise return False.

    r   r   r]   )rV   r[   cntr3   r3   r4   skip_over_whitespace   s   

rb   valuec                 C   s   t dd | D S )z
    Check if the given value consists of whitespace characters only.

    Args:
        value: The bytes to check.

    Returns:
        True if the value only has whitespace characters, otherwise return False.

    c                 s   s    | ]}|t v V  qd S N)WHITESPACES_AS_BYTES).0br3   r3   r4   	<genexpr>   s    z+check_if_whitespace_only.<locals>.<genexpr>)all)rc   r3   r3   r4   check_if_whitespace_only   s   rj   c                 C   sT   |  d}| dd |dkr&|dvr(|  d}|dkr td|dvsd S d S d S )Nr      %)rO   rQ   rS   zFile ended unexpectedly.)rX   seekr   r_   r3   r3   r4   skip_over_comment   s   

rn   regexc                 C   s   g }d}d}d}	 |  |}|sd|S || }||}|durHt|}	||	 |  }
| |
| t| d || d|d|
 S || |t|7 }|dd }|dk ra|dK }q	)	z
    Read until the regular expression pattern matched (ignore the match).
    Treats EOF on the underlying stream as the end of the token to be matched.

    Args:
        regex: re.Pattern

    Returns:
        The read bytes.

    r   rS      TNr   ii    )rX   joinsearchr+   startrm   rH   )rV   ro   parts	total_lentail
chunk_sizer[   bufmoverlapactual_startr3   r3   r4   read_until_regex   s,   




r|   to_readc                 C   s>   |   |k r
td| | t | |}| | t |S )a  
    Given a stream at position X, read a block of size to_read ending at position X.

    This changes the stream's position to the beginning of where the block was
    read.

    Args:
        stream:
        to_read:

    Returns:
        The data which was read.

    z!Could not read malformed PDF file)tellr   rm   r   rX   )rV   r}   rX   r3   r3   r4   read_block_backwards  s   
r   c                 C   s  g }d}|   dkrtt	 tt|   }|dkrndt| |}t|d }|sF|dkr@|| dvr@|d8 }|dkr@|| dvs2|dkrFd}|rl|||d d  |dkrk|| dv rk|d8 }|dkrk|| dv s]n|| |dkr~| |d t	 nqd
|ddd S )	a  
    Given a byte stream with current position X, return the previous line.

    All characters between the first CR/LF byte found before X
    (or, the start of the file, if no such byte is found) and position X
    After this call, the stream will be positioned one byte after the
    first non-CRLF character found beyond the first CR/LF byte before X,
    or, if no such byte is found, at the beginning of the stream.

    Args:
        stream: StreamType:

    Returns:
        The data which was read.

    Fr   Tr   s   
NrS   rk   )r~   r   r   minr   r   r+   rH   rm   r   rq   )rV   line_content
found_crlfr}   blockidxr3   r3   r4   read_previous_line7  s8   

"r   arg   c                    s   t  fdd| D S )Nc                 3   s*    | ] t  fd dt D V  qdS )c                 3   s(    | ]}t d d t |D V  qdS )c                 s   s$    | ]\}}t |t | V  qd S rd   )float)rf   r0   jr3   r3   r4   rh   u  s   " z6matrix_multiply.<locals>.<genexpr>.<genexpr>.<genexpr>N)sumzip)rf   colrowr3   r4   rh   u  s   & z,matrix_multiply.<locals>.<genexpr>.<genexpr>N)tupler   )rf   rg   r   r4   rh   t  s
    
z"matrix_multiply.<locals>.<genexpr>)r   )r   rg   r3   r   r4   matrix_multiplyq  s   r   c                 C   sx   d}|  | d tdd}|| | |d || | W d   n1 s.w   Y  |  | d dS )z5Create text file showing current location in context.i  r   zpypdf_pdfLocation.txtwbs   HEREN)rm   openwriterX   )rV   radius	output_fhr3   r3   r4   mark_locationz  s   
r   c                 C      d S rd   r3   r   r3   r3   r4   ord_     r   c                 C   r   rd   r3   r   r3   r3   r4   r     r   c                 C   r   rd   r3   r   r3   r3   r4   r     r   c                 C   s   t | tr	t| S | S rd   )
isinstancestrordr   r3   r3   r4   r     s   
r   msg
stacklevelc                 C   s   t j| t|d d S )N)r   )warningswarnDeprecationWarning)r   r   r3   r3   r4   	deprecate  s   r   c                 C   s   t | rd   )r   )r   r3   r3   r4   deprecation  s   r   old_namenew_name
removed_inc                 C   s    t |  d| d| dd dS )zFIssue a warning that a feature will be removed, but has a replacement., is deprecated and will be removed in pypdf . Use 	 instead.   Nr   r   r   r   r3   r3   r4   deprecate_with_replacement  s   r   c                 C   s   t |  d| d| d dS )zMRaise an exception that a feature was already removed, but has a replacement.( is deprecated and was removed in pypdf r   r   Nr   r   r3   r3   r4   deprecation_with_replacement  s   r   namec                 C   s   t |  d| dd dS )zCIssue a warning that a feature will be removed without replacement.r   .r   Nr   r   r   r3   r3   r4   deprecate_no_replacement  s   r   c                 C   s   t |  d| d dS )zJRaise an exception that a feature was already removed without replacement.r   r   Nr   r   r3   r3   r4   deprecation_no_replacement  s   r   messagesourcevaluesc                K   s   t || | dS )z
    Use this instead of logger.error directly.

    That allows people to overwrite it more easily.

    See the docs on when to use which:
    https://pypdf.readthedocs.io/en/latest/user/suppress-warnings.html
    N)logging	getLoggererror)r   r   r   r3   r3   r4   logger_error  s   	r   srcc                 C   s   t ||  dS )a  
    Use this instead of logger.warning directly.

    That allows people to overwrite it more easily.

    ## Exception, warnings.warn, logger_warning
    - Exceptions should be used if the user should write code that deals with
      an error case, e.g. the PDF being completely broken.
    - warnings.warn should be used if the user needs to fix their code, e.g.
      DeprecationWarnings
    - logger_warning should be used if the user needs to know that an issue was
      handled by pypdf, e.g. a non-compliant PDF being read in a way that
      pypdf could apply a robustness fix to still read it. This applies mainly
      to strict=False mode.
    N)r   r   warning)r   r   r3   r3   r4   logger_warning  s   r   F	func_namekwargsaliasesfailc                 C   s   |  D ]@\}}||v rD|rt| d| d||v r/t|  d| d| d| d| d
||||< tj| d| dtdd	 qd
S )z
    Helper function to deprecate arguments.

    Args:
        func_name: Name of the function to be deprecated
        kwargs:
        aliases:
        fail:

    z# is deprecated as an argument. Use z insteadz received both z and z as an argument. z is deprecated. Use r   r   )r   categoryr   N)itemsr   	TypeErrorpopr   r   r   )r   r   r   r   old_termnew_termr3   r3   r4   rename_kwargs  s,   r   bytesc                 C   sR   | dk r	|  dS | dk r| d ddS | dk r!| d ddS | d ddS )	Ni  z Bytei@B z.1fz kBi ʚ;z MBz GBr3   )r   r3   r3   r4   _human_readable_bytes  s   
r   c                   @   s:   e Zd ZdZd
dddZd
defddZdefdd	ZdS )classpropertyz
    Decorator that converts a method with a single cls argument into a property
    that can be accessed directly from the class.
    Nr   c                 C   s
   || _ d S rd   fgetselfmethodr3   r3   r4   __init__1     
zclassproperty.__init__c                 C   s
   |  |S rd   r   )r   instanceclsr3   r3   r4   __get__4  r   zclassproperty.__get__c                 C   s
   || _ | S rd   r   r   r3   r3   r4   getter7  s   zclassproperty.getterrd   )r   N)	__name__
__module____qualname____doc__r   r	   r   r   r   r3   r3   r3   r4   r   +  s
    r   c                   @   sd   e Zd ZU ddlmZ dZeed< 	 dZe	ed< 	 dZ
ee ed< 	 d	efd
dZd	efddZdS )Filer   )IndirectObjectr    r   rS   dataNindirect_referencer   c                 C   s&   | j j d| j dtt| j dS )Nz(name=z, data: ))	__class__r   r   r   r+   r   r   r3   r3   r4   __str__M  s   &zFile.__str__c                 C   s"   |   d d dt| j d S )Nrk   z, hash: r   )r   hashr   r   r3   r3   r4   __repr__P  s   "zFile.__repr__)r   r   r   genericr   r   r   __annotations__r   r   r   r
   r   r   r3   r3   r3   r4   r   <  s   
 r   c                   @   sx   e Zd ZedZdeddfddZdedee	e
ef  fddZd	edefd
dZde
fddZd	edefddZdS )Versionz^(\d+)(.*)$version_strr   Nc                 C   s   || _ | || _d S rd   )r   _parse_version
components)r   r   r3   r3   r4   r   X  s   zVersion.__init__c                 C   sn   | d}g }|D ]+}tj|}|s|d|f q	|d}|d}|d u r+d}|t||f q	|S )Nr   r   r   r   )splitr   COMPONENT_PATTERNmatchrH   groupr;   )r   r   r   parsed_components	componentr   integer_prefixsuffixr3   r3   r4   r   \  s   


zVersion._parse_versionotherc                 C   s   t |tsdS | j|jkS )NF)r   r   r   )r   r   r3   r3   r4   __eq__k  s   
zVersion.__eq__c                 C   s   t | jt| jfS rd   )r   r   r   r   r   r3   r3   r4   __hash__p  s   zVersion.__hash__c                 C   s   t |tstdt| t| j|jD ](\}}|\}}|\}}||k r( dS ||kr/ dS ||k r6 dS ||kr= dS qt| jt|jk S )Nz#Version cannot be compared against TF)r   r   r-   typer   r   r+   )r   r   self_componentother_component
self_valueself_suffixother_valueother_suffixr3   r3   r4   __lt__t  s   
zVersion.__lt__)r   r   r   recompiler   r   r   listr   r;   r   objectboolr   r   r	   r   r3   r3   r3   r4   r   T  s    
r   rd   )r   )F)Lr   
__author____author_email__	functoolsr   r   sysr   dataclassesr   r   r   ior   osr   r   typingr   r	   r
   r   r   version_infor   typing_extensionsr   errorsr   r   r   r   r   r   r   r   
StreamTyper   BinaryStreamTyper   StrByteTyper5   rE   rL   r^   rq   re   WHITESPACES_AS_REGEXPr;   r\   r`   r  rb   rj   rn   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   dictr   r   r   r   total_orderingr   r3   r3   r3   r4   <module>   s   
& 

(:
	$


#-