o
    ?Zc<                  	   @   s  d 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rYd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*j.Z.dd Z/d d! Z0d"d# Z1d$d% Z2d&d' Z3d(d) Z4e0  e3  e1  e4  e*5d* e0e3e1e4e0e1e3e4d+Z6e7 Z8e8fd,d-Z9e8fd.d/Z:e:  ed0D ]\Z;Z<e-e;j=ge<R   qd3d1d2Z>dS )4zSerialization utilities.    N)
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   I/var/www/chikooza/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_loads.   s   r/   c                 C   s   | r
|  d| dS |S )Nz ()r   )firstsecondr   r   r   parenthesize_alias3   s   r3   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    r5   r6   r<   r=   )r>   namer#   decoderr!   r"   r   r   r   r   D   s   


zSerializerRegistry.registerc                 C   "   d|vr	| j | }| j| d S N/)r=   r;   discardr>   rA   r   r   r   enablee      
zSerializerRegistry.enablec                 C   rC   rD   )r=   r;   addrG   r   r   r   disablej   rI   zSerializerRegistry.disablec                 C   sf   z$| j | }| j|d | j|d | j|d | j |d W dS  ty2   td| w )zUnregister 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=   r6   popr5   r<   KeyErrorr
   )r>   rA   r!   r   r   r   r   o   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)r5   r8   r9   r7   rN   r
   rG   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.
        rawrO   r   r   r   r)   r@   Nz
text/plain)
raw_encoder5   getr
   
isinstancebytesstrr+   r	   encoder7   r8   r9   )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disabledr@   )r   _for_untrusted_contentr;   lowerr6   rT   r+   r   SKIP_DECODErU   rW   r   )r>   rY   r!   r"   acceptforce_trusted_contentdecoder   r   r   r      s2   
 
 zSerializerRegistry.loadsc              	   C   s    t d|t| j|||S )Nz-Refusing to deserialize {} content of type {})r   formatr3   r<   rT   )r>   ctypewhyr   r   r   r^     s   z)SerializerRegistry._for_untrusted_content)r@   r,   )__name__
__module____qualname____doc__r?   r   rH   rK   r   rP   r   TRUSTED_CONTENTr   r^   r   r   r   r   r4   7   s    
!
@
.r4   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   r@   r   rR   Nr   )rU   rW   r+   r	   rX   )rY   r!   r[   r"   r   r   r   rS     s   

rS   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   )jsonrm   application/jsonr@   r!   r"   N)kombu.utilsrm   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 )	zRegister 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yamlapplication/x-yamlr@   ro   c                  _      t d)zRaise 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_available8  s   z$register_yaml.<locals>.not_available)rt   rq   r   	safe_dump	safe_loadImportError)rt   r{   r   r   r   register_yaml*  s   r   c                 C   s   t t| S r,   )r/   r   r-   r   r   r   unpickleC     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_dumpsM  r   z%register_pickle.<locals>.pickle_dumpsr   application/x-python-serializer   ro   N)r   r   rq   r   r   )r   r   r   r   register_pickleG  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 )zNRegister msgpack serializer.

    See Also:
        https://msgpack.org/.
    Nr   )r   r   packbunpackbc                        | ddS )NT)use_bin_typer   r   )r   r   r   packa  r   zregister_msgpack.<locals>.packc                    r   )NF)rQ   r   r   )r   r   r   unpackd  r   z register_msgpack.<locals>.unpackc                  _   rv   )Nz(msgpack requires msgpack-python >= 0.4.0rw   rx   r   r   r   version_mismatchg     z*register_msgpack.<locals>.version_mismatchc                  _   rv   )NzKNo decoder installed for msgpack. Please install the msgpack-python libraryrw   rx   r   r   r   r{   l  r   z'register_msgpack.<locals>.not_availablemsgpackapplication/x-msgpackr   ro   )r   versionr   r   r~   
ValueErrorrq   r   )r   r   r   r   r{   r   r   r   register_msgpackU  s&   

r   rm   )rm   r   rt   r   rn   ru   r   r   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 )zEnable 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   rH   rN   )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 )aA  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.
    rm   N)r   rq   r6   rK   rH   )allowedrA   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 )zReplace 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 )rE   r   ).0nr=   r   r   	<setcomp>  s     z)prepare_accept_content.<locals>.<setcomp>rL   r   )rq   r=   rN   r
   ry   )content_typesr=   er   r   r   prepare_accept_content  s   r   r,   )?rk   r   osr   r%   collectionsr   
contextlibr   ior   
exceptionsr   r   r	   r
   r   utils.compatr   utils.encodingr   r   __all__	frozensetr`   rl   platform
startswithr   rd   r.   pickle_loadintenvironrT   r   r    	Exceptionr+   r/   r3   r4   rq   r   r   r   r   rS   rs   r   r   r   r   rP   
_setupfunsobjectr   r   r   epry   rA   r   r   r   r   r   <module>   sv    


 W	$
