
    Ng>                     b    d Z ddlZddlmZ ddlmZ ddlm	Z	 d Z
 G d dej                  ZdS ))PdfAttachment    N)PdfiumErrorc                     t          | t                    r| dz                       d          S t          dt	          |           j         d          )N zutf-8zKey must be str, but z was given.)
isinstancestrencode	TypeErrortype__name__)keys    Y/var/www/html/ai-engine/env/lib/python3.11/site-packages/pypdfium2/_helpers/attachment.py_encode_keyr      sP    #s Qf$$W---OS		0BOOOPPP    c                   B    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
S )r   aR  
    Attachment helper class.
    See PDF 1.7, Section 7.11 "File Specifications".
    
    Attributes:
        raw (FPDF_ATTACHMENT):
            The underlying PDFium attachment handle.
        pdf (PdfDocument):
            Reference to the document this attachment belongs to. Must remain valid as long as the attachment is used.
    c                 $    ||c| _         | _        d S )N)rawpdf)selfr   r   s      r   __init__zPdfAttachment.__init__&   s     #$(((r   c                 .   t          j        | dd          }t          j        |          }t          j        |t          j        t           j                            }t          j        | ||           |j        d|dz
                               d          S )zC
        Returns:
            str: Name of the attachment.
        Nr      	utf-16-le)	pdfium_cFPDFAttachment_GetNamectypescreate_string_buffercastPOINTER
FPDF_WCHARr   decode)r   n_bytesbuffer
buffer_ptrs       r   get_namezPdfAttachment.get_name*   s~    
 1$a@@,W55[8K)L)LMM
'j'BBBz*719*%,,[999r   c                 ~   t          j                    }t          j        | dd|           |j        }|dk    rt          d| d          t          j        |          }t          j                    }t          j        | |||          }|j        }|st          d          ||k     rt          d| d| d          |S )zs
        Returns:
            ctypes.Array: The attachment's file data (as :class:`~ctypes.c_char` array).
        Nr   z,Failed to extract attachment (buffer length z).z,Failed to extract attachment (error status).z'Failed to extract attachment (expected z bytes, but got )r   c_ulongr   FPDFAttachment_GetFilevaluer   r   )r   r"   r#   
out_buflenoks        r   get_datazPdfAttachment.get_data6   s     .""'dAw???-a<<XWXXXYYY,W55^%%
,T67JOO%
 	NLMMMZoooakooopppr   c                 z    t          j        | | j        |t          |                    }|st	          d          dS )a  
        Set the attachment's file data.
        If this function is called on an existing attachment, it will be changed to point at the new data,
        but the previous data will not be removed from the file (as of PDFium 5418).
        
        Parameters:
            data (bytes | ctypes.Array):
                New file data for the attachment. May be any data type that can be implicitly converted to :class:`~ctypes.c_void_p`.
        zFailed to set attachment data.N)r   FPDFAttachment_SetFiler   lenr   )r   datar+   s      r   set_datazPdfAttachment.set_dataN   sF     ,T48T3t99MM 	@>???	@ 	@r   c                 F    t          j        | t          |                    S )z
        Parameters:
            key (str):
                A key to look for in the attachment's params dictionary.
        Returns:
            bool: True if *key* is contained in the params dictionary, False otherwise.
        )r   FPDFAttachment_HasKeyr   r   r   s     r   has_keyzPdfAttachment.has_key]   s     -dK4D4DEEEr   c                 F    t          j        | t          |                    S )zx
        Returns:
            int: Type of the value of *key* in the params dictionary (:attr:`FPDF_OBJECT_*`).
        )r   FPDFAttachment_GetValueTyper   r4   s     r   get_value_typezPdfAttachment.get_value_typeh   s    
 3D+c:J:JKKKr   c                    t          |          }t          j        | |dd          }|dk    rt          d| d          t	          j        |          }t	          j        |t	          j        t          j                            }t          j        | |||           |j	        d|dz
           
                    d          S )z
        Returns:
            str: The value of *key* in the params dictionary, if it is a string or name.
            Otherwise, an empty string will be returned. On other failures, an exception will be raised.
        Nr   zFailed to get value of key ''.r   r   )r   r   FPDFAttachment_GetStringValuer   r   r   r   r   r    r   r!   )r   r   enc_keyr"   r#   r$   s         r   get_str_valuezPdfAttachment.get_str_valuep   s     c""8waPPa<<DSDDDEEE,W55[8K)L)LMM
.tWj'RRRz*719*%,,[999r   c                     |dz                        d          }t          j        |t          j                  }t          j        | t          |          |          }|st          d| d| d          dS )z
        Set the attribute specified by *key* to the string *value*.
        
        Parameters:
            value (str): New string value for the attribute.
        r   r   z Failed to set attachment param 'z' to 'r:   N)r	   r   r   r   FPDF_WIDESTRINGFPDFAttachment_SetStringValuer   r   )r   r   r)   	enc_valueenc_value_ptrr+   s         r   set_str_valuezPdfAttachment.set_str_value   s     V^++K88	Ix/GHH3D+c:J:JMZZ 	WUUUEUUUVVV	W 	Wr   N)r   
__module____qualname____doc__r   r%   r,   r1   r5   r8   r=   rC    r   r   r   r      s        	 	$& & &	: 	: 	:  0@ @ @F F FL L L: : :&W W W W Wr   r   )__all__r   pypdfium2.rawr   r   pypdfium2.internalinternalpdfium_ipypdfium2._helpers.miscr   r   AutoCastabler   rG   r   r   <module>rO      s                  % % % % % % / / / / / /Q Q Q{W {W {W {W {WX* {W {W {W {W {Wr   