o
    DfV<                     @  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   qd3d1d2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   L/home/ubuntu/webapp/venv/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 )	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yamlzapplication/x-yamlrA   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_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 )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   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 )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 )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	%
