o
    Ti                     @  s  d 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	 ddlmZ ddlmZ dd	lmZmZmZmZ dd
lmZ ddlmZ ddlmZmZmZ ddlmZmZ ddlm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& ddl'm(Z( ddl)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7m8Z8 edZ9g dZ:G dd de+Z;d:d!d"Z<e8e;d;d&d'Z=d:d(d)Z>d<d+d,Z?d=d.d/Z@	0d>d?d4d5ZAd@d7d8ZBd9S )Az%Preconfigured converters for msgspec.    )annotations	b64decode)Callable)is_dataclass)datedatetime)Enum)partial)AnyTypeVarUnionget_type_hints)has)resolve_types)Structconvertto_builtins)Encoderdecode   )fieldsget_args
get_originis_bare
is_mappingis_sequenceis_subclass)is_namedtuple)BaseConverter	Converter)UnstructureHookidentity) make_hetero_tuple_unstructure_fn)is_literal_containing_enums)configure_union_passthrough   )'literals_with_enums_unstructure_factorywrapT)MsgspecJsonConverterconfigure_convertermake_converterc                   @  sJ   e Zd ZU dZe Zded< ddddZdddZdddZ	dddZ
dS )r+   z2A converter specialized for the _msgspec_ library.r   encoderNobjr   unstructure_askwargsreturnbytesc                 K  s   | j j| j||dfi |S )z-Unstructure and encode `obj` into JSON bytes.)r0   )r.   encodeunstructure)selfr/   r0   r1    r7   J/home/ubuntu/.local/lib/python3.10/site-packages/cattrs/preconf/msgspec.pydumps/   s
   zMsgspecJsonConverter.dumpsCallable[[Any], bytes]c                 K  s$   |  |}|ttfv r| jjS | jS )z*Produce a `dumps` hook for the given type.)get_unstructure_hookr#   r   r.   r4   r9   )r6   r0   r1   unstruct_hookr7   r7   r8   get_dumps_hook5   s   
z#MsgspecJsonConverter.get_dumps_hookdatacltype[T]r*   c                 K  s   |  t|fi ||S )z7Decode and structure `cl` from the provided JSON bytes.)	structurer   )r6   r>   r?   r1   r7   r7   r8   loads>   s   zMsgspecJsonConverter.loadsCallable[[bytes], T]c                 C  s   t | j|dS )z*Produce a `loads` hook for the given type.)r?   )r
   rB   )r6   r?   r7   r7   r8   get_loads_hookB   s   z#MsgspecJsonConverter.get_loads_hookN)r/   r   r0   r   r1   r   r2   r3   )r0   r   r1   r   r2   r:   )r>   r3   r?   r@   r1   r   r2   r*   )r?   r@   r2   rC   )__name__
__module____qualname____doc__r   r.   __annotations__r9   r=   rB   rD   r7   r7   r7   r8   r+   )   s   
 

	r+   	converterr    r2   Nonec                 C  s   t |  | tt | dd dd  | tt | tdd  | tdd  | t	dd  | t
t ttttttdf |  dS )a  Configure the converter for the msgspec library.

    * bytes are serialized as base64 strings, directly by msgspec
    * datetimes and dates are passed through to be serialized as RFC 3339 directly
    * enums are passed through to msgspec directly
    * union passthrough configured for str, bool, int, float and None
    * bare, string and int enums are passed through when unstructuring

    .. versionchanged:: 24.2.0
        Enums are left to the library to unstructure, speeding them up.
    c                 S  
   t | tS rE   )r   r	   )tr7   r7   r8   <lambda>W      
 z%configure_converter.<locals>.<lambda>c                 S  s   t S rE   r"   )rN   cr7   r7   r8   rO   W   s    c                 S  s   t | S rE   r   v_r7   r7   r8   rO   [   s    c                 S  rM   rE   )r   r   rR   r7   r7   r8   rO   \   rP   c                 S  s
   t | S rE   )r   fromisoformatrR   r7   r7   r8   rO   ]   rP   N)configure_passthroughsregister_unstructure_hookr   r   !register_unstructure_hook_factoryregister_structure_hookr   r3   r   r   r%   r(   r&   r   strboolintfloatrK   r7   r7   r8   r,   G   s   r,   argsr   r1   c                  O  s   t | i |}t| |S rE   )r+   r,   )r_   r1   resr7   r7   r8   r-   d   s   r-   c                 C  sT   |  tt | tt | tt | tt	 | t
tt	dd | tt dS )zConfigure optimizing passthroughs.

    A passthrough is when we let msgspec handle something automatically.

    .. versionchanged:: 25.1.0
        Dataclasses with private attributes are now passed through.
    F)msgspec_skips_privateN)rW   r3   r   rX   r   mapping_unstructure_factoryr   seq_unstructure_factory	attrs_has!msgspec_attrs_unstructure_factoryr   r
   r   namedtuple_unstructure_factoryr^   r7   r7   r8   rV   k   s   
rV   r!   c                 C  sF   t | rt}nt| }|d }|j|dd}|ttfv r|S || S )z3The msgspec unstructure hook factory for sequences.r   Fcache_result)r   r   r   r;   r#   r   gen_unstructure_iterable)typerK   type_argr_   handlerr7   r7   r8   rc      s   
rc   r   c                 C  s   t | rt}t}|j|dd}|j|dd}n"t| }t|dkr&|\}}n|t}}|j|dd}|j|dd}|ttfv rG|ttfv rGtS || S )z2The msgspec unstructure hook factory for mappings.Frg   r   )r   r   r;   r   lenr#   r   gen_unstructure_mapping)rj   rK   key_argval_argkey_handlervalue_handlerr_   r7   r7   r8   rb      s"   


rb   Trj   ra   r[   c                   sj   t | }t|p| }t| r!tdd |D r!t|  t|p| }|r3t fdd|D r3 | S tS )zChoose whether to use msgspec handling or our own.

    Args:
        msgspec_skips_private: Whether the msgspec library skips unstructuring
            private attributes, making us do the work.
    c                 s  s    | ]	}t |jtV  qd S rE   )
isinstancerj   rZ   ).0ar7   r7   r8   	<genexpr>   s    z4msgspec_attrs_unstructure_factory.<locals>.<genexpr>c                 3  s4    | ]}|j d p j|jddttfvV  qdS )rT   Frg   N)name
startswithr;   rj   r#   r   )rt   attrr^   r7   r8   rv      s    
)r   r   rd   anyr   gen_unstructure_attrs_fromdictr   )rj   rK   ra   originattribsr7   r^   r8   re      s   	
re   type[tuple]c                   s>   t  fddt|  D rtS t|  ttt|  dS )zCA hook factory for unstructuring namedtuples, modified for msgspec.c                 3  s"    | ]}  |ttfv V  qd S rE   )r;   r#   r   )rt   rN   r^   r7   r8   rv      s
    
z1namedtuple_unstructure_factory.<locals>.<genexpr>)unstructure_to	type_args)allr   valuesr#   r$   tuple)rj   rK   r7   r^   r8   rf      s   
rf   N)rK   r    r2   rL   )r_   r   r1   r   r2   r+   )rK   r    r2   r!   )rK   r   r2   r!   )T)rj   r   rK   r    ra   r[   r2   r!   )rj   r~   rK   r   r2   r!   )CrI   
__future__r   base64r   collections.abcr   dataclassesr   r   r   enumr	   	functoolsr
   typingr   r   r   r   attrsr   rd   r   msgspecr   r   r   msgspec.jsonr   r   _compatr   r   r   r   r   r   r   colsr   
convertersr   r    dispatchr!   fnsr#   genr$   literalsr%   
strategiesr&    r(   r)   r*   __all__r+   r,   r-   rV   rc   rb   re   rf   r7   r7   r7   r8   <module>   sD    $	



