o
    —¨ÊhV<  ã                   @  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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 dd	lmZ dd
lmZmZ dZeddgƒZeddgƒZej d¡r_dd„ ZnejZej Z!e"ej# $dd¡ƒZ%eddƒZ&e
e'feffdd„ƒZ(e!fdd„Z)dd„ Z*G dd„ dƒZ+e+ƒ Z,e,j-Z-e,j.Z.e,j/Z/e,j0Z0dd „ Z1d!d"„ Z2d#d$„ Z3d%d&„ Z4d'd(„ Z5d)d*„ Z6e2ƒ  e5ƒ  e3ƒ  e6ƒ  e, 7d+¡ e8ƒ Z9e9fd,d-„Z:e9fd.d/„Z;e;ƒ  ed0ƒD ]\Z<Z=e/e<j>ge=¢R Ž  qÝd3d1d2„Z?dS )4zSerialization utilities.é    )ÚannotationsN)Ú
namedtuple)Úcontextmanager©ÚBytesIOé   )ÚContentDisallowedÚDecodeErrorÚEncodeErrorÚSerializerNotInstalledÚreraise)Úentrypoints)Úbytes_to_strÚstr_to_bytes)ÚpickleÚloadsÚdumpsÚregisterÚ
unregisterÚbinaryz
ascii-8bitúapplication/datazapplication/textÚjavac                 C  s   t  |¡| ƒd S )Nr   )ÚcodecsÚ
getdecoder)ÚtÚcoding© r   úE/var/www/html/env/lib/python3.10/site-packages/kombu/serialization.pyÚ_decode   s   r   ÚPICKLE_PROTOCOLé   Úcodec)Úcontent_typeÚcontent_encodingÚencoderc              
   c  s\   � zd V  W d S  |y   ‚  |y- } zt | | |ƒt ¡ d ƒ W Y d }~d S d }~ww )Né   )r   ÚsysÚexc_info)ÚwrapperÚincludeÚexcludeÚexcr   r   r   Ú_reraise_errors%   s   €&€ÿr,   c                 C  s   |t | ƒƒS ©Nr   )ÚsÚloadr   r   r   Úpickle_loads0   s   r0   c                 C  s   | r
| › d|› d�S |S )Nz (ú)r   )ÚfirstÚsecondr   r   r   Úparenthesize_alias5   s   r4   c                   @  sf   e Zd ZdZdd„ Z	ddd„Zdd„ Zd	d
„ Zdd„ Zdd„ Z	ddd„Z
ddefdd„Zdd„ ZdS )ÚSerializerRegistryz2The registry keeps track of serialization methods.c                 C  s6   i | _ i | _d | _d | _d | _tƒ | _i | _i | _d S r-   )	Ú	_encodersÚ	_decodersÚ_default_encodeÚ_default_content_typeÚ_default_content_encodingÚsetÚ_disabled_content_typesÚtype_to_nameÚname_to_type)Úselfr   r   r   Ú__init__<   s   
zSerializerRegistry.__init__úutf-8c                 C  s<   |rt |||ƒ| j|< |r|| j|< || j|< || j|< dS )aü  Register a new encoder/decoder.

        Arguments:
        ---------
            name (str): A convenience name for the serialization method.

            encoder (callable): A method that will be passed a python data
                structure and should return a string representing the
                serialized data.  If :const:`None`, then only a decoder
                will be registered. Encoding will not be possible.

            decoder (Callable): A method that will be passed a string
                representing serialized data and should return a python
                data structure.  If :const:`None`, then only an encoder
                will be registered.  Decoding will not be possible.

            content_type (str): The mime-type describing the serialized
                structure.

            content_encoding (str): The content encoding (character set) that
                the `decoder` method will be returning. Will usually be
                `utf-8`, `us-ascii`, or `binary`.
        N)r!   r6   r7   r=   r>   )r?   Únamer$   Údecoderr"   r#   r   r   r   r   F   s   
ÿ

zSerializerRegistry.registerc                 C  ó"   d|vr	| j | }| j |¡ d S ©Nú/)r>   r<   Údiscard©r?   rB   r   r   r   Úenableh   ó   
zSerializerRegistry.enablec                 C  rD   rE   )r>   r<   ÚaddrH   r   r   r   Údisablem   rJ   zSerializerRegistry.disablec                 C  sf   z$| j | }| j |d¡ | j |d¡ | j |d¡ | j  |d¡ W dS  ty2   td|› �ƒ‚w )a  Unregister registered encoder/decoder.

        Arguments:
        ---------
            name (str): Registered serialization method name.

        Raises
        ------
            SerializerNotInstalled: If a serializer by that name
                cannot be found.
        Nú!No encoder/decoder installed for )r>   r7   Úpopr6   r=   ÚKeyErrorr   )r?   rB   r"   r   r   r   r   r   s   
ÿÿzSerializerRegistry.unregisterc                 C  s:   z| j | \| _| _| _W dS  ty   td|› �ƒ‚w )aÒ  Set the default serialization method used by this library.

        Arguments:
        ---------
            name (str): The name of the registered serialization method.
                For example, `json` (default), `pickle`, `yaml`, `msgpack`,
                or any custom methods registered using :meth:`register`.

        Raises
        ------
            SerializerNotInstalled: If the serialization method
                requested is not available.
        úNo encoder installed for N)r6   r9   r:   r8   rO   r   rH   r   r   r   Ú_set_default_serializerˆ   s   
ÿ
ÿÿz*SerializerRegistry._set_default_serializerNc                 C  sö   |dkrt |ƒS |r| j |¡std|› �ƒ‚|s#t|tƒr#dd|fS |sJt|tƒrJttdd�� | 	d¡}W d  ƒ n1 s@w   Y  d	d|fS |rU| j| \}}}n	| j
}| j}| j}ttƒ� ||ƒ}W d  ƒ n1 sqw   Y  |||fS )
aæ  Encode data.

        Serialize a data structure into a string suitable for sending
        as an AMQP message body.

        Arguments:
        ---------
            data (List, Dict, str): The message data to send.

            serializer (str): An optional string representing
                the serialization method you want the data marshalled
                into. (For example, `json`, `raw`, or `pickle`).

                If :const:`None` (default), then json will be used, unless
                `data` is a :class:`str` or :class:`unicode` object. In this
                latter case, no serialization occurs as it would be
                unnecessary.

                Note that if `serializer` is specified, then that
                serialization method will be used even if a :class:`str`
                or :class:`unicode` object is passed in.

        Returns
        -------
            Tuple[str, str, str]: A three-item tuple containing the
            content type (e.g., `application/json`), content encoding, (e.g.,
            `utf-8`) and a string containing the serialized data.

        Raises
        ------
            SerializerNotInstalled: If the serialization method
                requested is not available.
        ÚrawrP   r   r   r   ©r*   rA   Nz
text/plain)Ú
raw_encoder6   Úgetr   Ú
isinstanceÚbytesÚstrr,   r
   Úencoder8   r9   r:   )r?   ÚdataÚ
serializerÚpayloadr"   r#   r$   r   r   r   r   �   s.   "ÿ
ÿ

ÿ

ÿ
zSerializerRegistry.dumpsFc                 C  sè   |rt |ƒnd}|dur||vr||vr|  |d¡‚n|| jv r(|s(|  |d¡‚|p+d ¡ }|rr| j |¡}|rPttƒ� ||ƒW  d  ƒ S 1 sKw   Y  |tvrrt	|t
ƒsrttƒ� t||ƒW  d  ƒ S 1 smw   Y  |S )aÂ  Decode serialized data.

        Deserialize a data stream as serialized using `dumps`
        based on `content_type`.

        Arguments:
        ---------
            data (bytes, buffer, str): The message data to deserialize.

            content_type (str): The content-type of the data.
                (e.g., `application/json`).

            content_encoding (str): The content-encoding of the data.
                (e.g., `utf-8`, `binary`, or `us-ascii`).

            accept (Set): List of content-types to accept.

        Raises
        ------
            ContentDisallowed: If the content-type is not accepted.

        Returns
        -------
            Any: The unserialized data.
        r   NÚ	untrustedÚdisabledrA   )r   Ú_for_untrusted_contentr<   Úlowerr7   rU   r,   r	   ÚSKIP_DECODErV   rX   r   )r?   rZ   r"   r#   ÚacceptÚforceÚ_trusted_contentÚdecoder   r   r   r   ß   s0   ÿ€
 ÿÿ
 ÿzSerializerRegistry.loadsc              	   C  s    t d |t| j ||¡|ƒ¡ƒS )Nz-Refusing to deserialize {} content of type {})r   Úformatr4   r=   rU   )r?   ÚctypeÚwhyr   r   r   r_     s   þÿz)SerializerRegistry._for_untrusted_content)rA   r-   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r@   r   rI   rL   r   rQ   r   ÚTRUSTED_CONTENTr   r_   r   r   r   r   r5   9   s    
ÿ"
C
ÿ1r5   c                 C  s\   d}| }t |tƒr'd}ttdd�� | |¡}W d  ƒ n1 s!w   Y  nd}|||fS )zSpecial case serializer.r   rA   r   rS   Nr   )rV   rX   r,   r
   rY   )rZ   r"   r\   r#   r   r   r   rT   !  s   
ÿ€
rT   c                  C  s(   ddl m}  tjd| j| jddd� dS )z2Register a encoder/decoder for JSON serialization.r   )Újsonrn   zapplication/jsonrA   ©r"   r#   N)Úkombu.utilsrn   Úregistryr   r   r   )Ú_jsonr   r   r   Úregister_json.  s
   
þrs   c               	   C  sT   zddl } tjd| j| jddd� W dS  ty)   dd„ }t dd|d¡ Y dS w )	z¸Register a encoder/decoder for YAML serialization.

    It is slower than JSON, but allows for more data types
    to be serialized. Useful if you need to send data such as dates

    r   NÚyamlzapplication/x-yamlrA   ro   c                  _  ó   t dƒ‚)zŒRaise SerializerNotInstalled.

            Used in case a client receives a yaml message, but yaml
            isn't installed.
            z9No decoder installed for YAML. Install the PyYAML library©r   ©ÚargsÚkwargsr   r   r   Únot_availableE  s   ÿz$register_yaml.<locals>.not_available)rt   rq   r   Ú	safe_dumpÚ	safe_loadÚImportError)rt   rz   r   r   r   Úregister_yaml7  s   þör~   c                 C  s   t t| ƒƒS r-   )r0   r   ©r.   r   r   r   ÚunpickleP  ó   r€   c                  C  s&   t jfdd„} tjd| tddd� dS )zpRegister pickle serializer.

    The fastest serialization method, but restricts
    you to python clients.
    c                 S  s   || t d�S )N)Úprotocol)Úpickle_protocol)ÚobjÚdumperr   r   r   Úpickle_dumpsZ  r�   z%register_pickle.<locals>.pickle_dumpsr   zapplication/x-python-serializer   ro   N)r   r   rq   r   r€   )r†   r   r   r   Úregister_pickleT  s
   

þr‡   c               	     s˜   d } }z(ddl }|jdkr#ddl m‰ m‰ ‡ fdd„} ‡fdd„}nd	d
„ }| } }W n ttfy?   dd„ }| } }Y nw tjd| |ddd� dS )zZRegister msgpack serializer.

    See Also
    --------
        https://msgpack.org/.
    Nr   )r   r    ©ÚpackbÚunpackbc                   ó   ˆ | dd�S )NT)Úuse_bin_typer   r   )r‰   r   r   Úpacko  r�   zregister_msgpack.<locals>.packc                   r‹   )NF)rR   r   r   )rŠ   r   r   Úunpackr  r�   z register_msgpack.<locals>.unpackc                  _  ru   )Nz(msgpack requires msgpack-python >= 0.4.0rv   rw   r   r   r   Úversion_mismatchu  ó   ÿz*register_msgpack.<locals>.version_mismatchc                  _  ru   )NzKNo decoder installed for msgpack. Please install the msgpack-python libraryrv   rw   r   r   r   rz   z  r�   z'register_msgpack.<locals>.not_availableÚmsgpackzapplication/x-msgpackr   ro   )r‘   Úversionr‰   rŠ   r}   Ú
ValueErrorrq   r   )r�   rŽ   r‘   r�   rz   r   rˆ   r   Úregister_msgpackb  s&   
€û
ýr”   rn   c              	   C  sN   | t u rg d¢n| } | dur#| D ]}zt |¡ W q ty"   Y qw dS dS )zöEnable serializers that are considered to be unsafe.

    Note:
    ----
        Will enable ``pickle``, ``yaml`` and ``msgpack`` by default, but you
        can also specify a list of serializers (by name or content type)
        to enable.
    )r   rt   r‘   N)ÚNOTSETrq   rI   rO   )ÚchoicesÚchoicer   r   r   Úenable_insecure_serializers’  s   	ÿür˜   c                 C  sL   | t u rdgn| } tjD ]}t |¡ q| dur"| D ]	}t |¡ qdS dS )aJ  Disable untrusted serializers.

    Will disable all serializers except ``json``
    or you can specify a list of deserializers to allow.

    Note:
    ----
        Producers will still be able to serialize data
        in these formats, but consumers will not accept
        incoming data using the untrusted content types.
    rn   N)r•   rq   r7   rL   rI   )ÚallowedrB   r   r   r   Údisable_insecure_serializers¤  s   
þrš   zkombu.serializersc              
     s\   ˆ st jnˆ ‰ | dur,z
‡ fdd„| D ƒW S  ty+ } z
td|jd › �ƒ‚d}~ww | S )z¿Replace aliases of content_types with full names from registry.

    Raises
    ------
        SerializerNotInstalled: If the serialization method
            requested is not available.
    Nc                   s    h | ]}d |v r
|nˆ | ’qS )rF   r   )Ú.0Ún©r>   r   r   Ú	<setcomp>Ë  s     z)prepare_accept_content.<locals>.<setcomp>rM   r   )rq   r>   rO   r   rx   )Úcontent_typesr>   Úer   r�   r   Úprepare_accept_contentÀ  s   ÿ€ÿr¡   r-   )@rl   Ú
__future__r   r   Úosr   r&   Úcollectionsr   Ú
contextlibr   Úior   Ú
exceptionsr   r	   r
   r   r   Úutils.compatr   Úutils.encodingr   r   Ú__all__Ú	frozensetra   rm   ÚplatformÚ
startswithr   re   r/   Úpickle_loadÚintÚenvironrU   rƒ   r!   Ú	Exceptionr,   r0   r4   r5   rq   r   r   r   r   rT   rs   r~   r€   r‡   r”   rQ   Úobjectr•   r˜   rš   Úeprx   rB   r¡   r   r   r   r   Ú<module>   sf    

ÿ
 b	%
