o
    uhn2                     @  s   d dl mZ d dlmZ d dlmZ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 d d	lmZmZmZmZmZmZmZmZmZmZ er^d d
lZd dlmZ d dl m!Z! G dd dZ"d
S )    )annotations)cached_property)TYPE_CHECKINGcast)format_iso8824_dateparse_iso8824_date)CatalogAttributes)"FileSpecificationDictionaryEntries)PageAttributes)PdfReadError)
ArrayObjectByteStringObjectDecodedStreamObjectDictionaryObject
NameObject
NullObjectNumberObjectStreamObjectTextStringObjectis_null_or_noneN)	Generator)	PdfWriterc                   @  s  e Zd ZdZdJdd	ZedKd
dZedLddZedMddZ	e	j
dNddZ	edMddZej
dNddZedKddZej
dOddZedPd d!ZedQd"d#ZedQd$d%ZedMd&d'Zej
dRd)d'ZedSd+d,Zej
dTd-d,ZedUd/d0Zej
dVd2d0ZedWd4d5Zej
dXd6d5ZedWd7d8Zej
dXd9d8ZedYd;d<Zej
dZd>d<ZdKd?d@Zed[dDdEZed\dGdHZdIS )]EmbeddedFilez
    Container holding the information on an embedded file.

    Attributes are evaluated lazily if possible.

    Further information on embedded files can be found in section 7.11 of the PDF 2.0 specification.
    namestr
pdf_objectr   returnNonec                 C  s   || _ || _dS )z
        Args:
            name: The (primary) name as provided in the name tree.
            pdf_object: The corresponding PDF object to allow retrieving further data.
        N)_namer   )selfr   r    r    J/var/www/html/bot/env/lib/python3.10/site-packages/pypdf/generic/_files.py__init__'   s   
zEmbeddedFile.__init__c                 C  s   | j S )zEThe (primary) name of the embedded file as provided in the name tree.)r   r   r    r    r!   r   0   s   zEmbeddedFile.namewriterr   contentstr | bytesc                 C  s>  t |tr
|d}t }|| |ttjtdi t	 }|td|
|i ddlm} t	 }|ttjtdttj||ttj|i tj|jvr`|
t	 |jttj< tt	|jtj }d|vrt	ttjt i}	|
|	|td< ntt	|d }	tt|	tj }
|
|||g | ||dS )	aO  
        Create a new embedded file and add it to the PdfWriter.

        Args:
            writer: The PdfWriter instance to add the embedded file to.
            name: The filename to display.
            content: The data in the file.

        Returns:
            EmbeddedFile instance for the newly created embedded file.
        latin-1z/EmbeddedFilez/Fr   )create_string_objectz	/Filespec/EmbeddedFilesr   r   )
isinstancer   encoder   set_dataupdater   PGTYPEr   _add_objectpypdf.genericr(   r	   FEFCANAMES_root_objectr   r   extend)clsr$   r   r%   
file_entryef_entryr(   filespec
names_dictembedded_files_names_dictionarynames_arrayr    r    r!   _create_new5   s6   



	zEmbeddedFile._create_new
str | Nonec                 C  sD   t jt jfD ]}|| jv r| j|  }t|stt|  S qdS )z3Retrieve the alternative name (file specification).N)r	   UFr3   r   
get_objectr   r   r   )r   keyvaluer    r    r!   alternative_namen   s   
zEmbeddedFile.alternative_namerE   TextStringObject | Nonec                 C  s   |du r&t j| jv rt | jtt j< t j| jv r$t | jtt j< dS dS t j| jv r4|| jtt j< t j| jv rD|| jtt j< dS dS )z.Set the alternative name (file specification).N)r	   rB   r   r   r   r3   r   rE   r    r    r!   rF   z   s   c                 C  s   | j tj}t|rdS |S )zRetrieve the description.N)r   getr	   DESCr   rH   r    r    r!   description   s   zEmbeddedFile.descriptionc                 C  s2   |du rt  | jttj< dS || jttj< dS )zSet the description.N)r   r   r   r	   rJ   rH   r    r    r!   rK      s   c                 C  s   | j ddS )zJRetrieve the relationship of the referring document to this embedded file./AFRelationshipz/Unspecified)r   rI   r#   r    r    r!   associated_file_relationship   s   z)EmbeddedFile.associated_file_relationshipr   c                 C  s   || j td< dS )zESet the relationship of the referring document to this embedded file.rL   N)r   r   rH   r    r    r!   rM      s   r   c                 C  sh   d| j vrtd| j  tt| j d }tjtjfD ]}||v r,tt||    S qtd| )z)Retrieve the actual embedded file stream.z/EFz/EF entry not found: z'No /(U)F key found in file dictionary: )	r   r   r   r   r	   rB   r3   r   rC   )r   efrD   r    r    r!   _embedded_file   s   
zEmbeddedFile._embedded_filec                 C  s   | j dt  S )z&Retrieve the file-specific parameters./Params)rO   rI   r   rC   r#   r    r    r!   _params   s   zEmbeddedFile._paramsc                 C  s*   | j }d|vrt |td< tt|d S )z3Ensure the /Params dictionary exists and return it.rP   )rO   r   r   r   )r   embedded_filer    r    r!   _ensure_params   s   zEmbeddedFile._ensure_paramsc                 C     | j d}t|rdS |S )zERetrieve the subtype. This is a MIME media type, prefixed by a slash./SubtypeN)rO   rI   r   rH   r    r    r!   subtype      zEmbeddedFile.subtypeNameObject | Nonec                 C  0   | j }|du rt |td< dS ||td< dS )zGSet the subtype. This should be a MIME media type, prefixed by a slash.NrU   )rO   r   r   )r   rE   rR   r    r    r!   rV         bytesc                 C  s
   | j  S )z!Retrieve the actual file content.)rO   get_datar#   r    r    r!   r%      s   
zEmbeddedFile.contentc                 C  s$   t |tr
|d}| j| dS )zSet the file content.r'   N)r+   r   r,   rO   r-   rH   r    r    r!   r%      s   


int | Nonec                 C  rT   )z4Retrieve the size of the uncompressed file in bytes./SizeNrQ   rI   r   rH   r    r    r!   size   rW   zEmbeddedFile.sizeNumberObject | Nonec                 C  rY   )z/Set the size of the uncompressed file in bytes.Nr^   rS   r   r   r   rE   paramsr    r    r!   r`      rZ   datetime.datetime | Nonec                 C     t | jdS )z$Retrieve the file creation datetime./CreationDater   rQ   rI   r#   r    r    r!   creation_date      zEmbeddedFile.creation_datec                 C  <   | j }|du rt |td< dS t|}t||td< dS )zSet the file creation datetime.Nrg   rS   r   r   r   r   r   rE   rd   date_strr    r    r!   ri      
   c                 C  rf   )z4Retrieve the datetime of the last file modification./ModDaterh   r#   r    r    r!   modification_date   rj   zEmbeddedFile.modification_datec                 C  rk   )z/Set the datetime of the last file modification.Nrp   rl   rm   r    r    r!   rq      ro   bytes | Nonec                 C  rT   )z5Retrieve the MD5 checksum of the (uncompressed) file.	/CheckSumNr_   rH   r    r    r!   checksum  rW   zEmbeddedFile.checksumByteStringObject | Nonec                 C  rY   )z0Set the MD5 checksum of the (uncompressed) file.Nrs   rb   rc   r    r    r!   rt     rZ   c                 C  s   d| j j d| jdS )N<z name=>)	__class____name__r   r#   r    r    r!   __repr__  s   zEmbeddedFile.__repr__namesr   Generator[EmbeddedFile]c                 c  sH    t |D ]\}}t|ts!| }||d   }t||dV  qdS )z
        Convert the given name tree into class instances.

        Args:
            names: The name tree to load the data from.

        Returns:
            Iterable of class instances for the files found.
           r*   N)	enumerater+   r   rC   r   )r9   r{   ir   file_dictionarydirect_namer    r    r!   _load_from_names  s   
zEmbeddedFile._load_from_namescatalogc                 c  s    zt tt t|d d }W n
 ty   Y dS w d|v r?t t|d  D ]}| }d|v r>| t t|d E dH  q'd|v rR| t t|d E dH  dS dS )aI  
        Load the embedded files for the given document catalog.

        This method and its signature are considered internal API and thus not exposed publicly for now.

        Args:
            catalog: The document catalog to load from.

        Returns:
            Iterable of class instances for the files found.
        z/Namesr)   Nz/Kids)r   r   KeyErrorr   rC   r   )r9   r   	containerkidr    r    r!   _load-  s$   zEmbeddedFile._loadN)r   r   r   r   r   r   )r   r   )r$   r   r   r   r%   r&   r   r   )r   rA   )rE   rG   r   r   )rE   r   r   r   )r   r   )r   r   )rE   rX   r   r   )r   r[   )rE   r&   r   r   )r   r]   )rE   ra   r   r   )r   re   )rE   re   r   r   )r   rr   )rE   ru   r   r   )r{   r   r   r|   )r   r   r   r|   )ry   
__module____qualname____doc__r"   propertyr   classmethodr@   rF   setterrK   rM   rO   rQ   r   rS   rV   r%   r`   ri   rq   rt   rz   r   r   r    r    r    r!   r      sl    
	8
		
r   )#
__future__r   	functoolsr   typingr   r   pypdf._utilsr   r   pypdf.constantsr   r5   r	   r
   r/   pypdf.errorsr   r2   r   r   r   r   r   r   r   r   r   r   datetimecollections.abcr   pypdf._writerr   r   r    r    r    r!   <module>   s    0