o
    Tis                     @   s0  d Z ddlmZmZ ddlmZ ddlmZmZ ddlm	Z	m
Z
mZ ddlmZmZmZmZmZmZ ddl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lm Z  ddl!m"Z" ddl#m$Z$m%Z%m&Z&m'Z' e
dZ(G dd de)Z*G dd deZ+defddZ,e'e+de	de	de+fddZ-dS )z"Preconfigured converters for bson.    )	b85decode	b85encode)Set)datedatetime)AnyTypeVarUnion)DEFAULT_CODEC_OPTIONSCodecOptionsInt64ObjectIddecodeencode   )
is_mappingis_subclass)mapping_structure_factory)BaseConverter	Converter)StructureHookidentity)is_literal_containing_enums)configure_union_passthrough   )is_primitive_enum'literals_with_enums_unstructure_factoryvalidate_datetimewrapTc                   @   s   e Zd ZdZdS )Base85Bytesz5A subclass to help with binary key encoding/decoding.N)__name__
__module____qualname____doc__ r&   r&   G/home/ubuntu/.local/lib/python3.10/site-packages/cattrs/preconf/bson.pyr!      s    r!   c                   @   sT   e Zd Zddefdededededef
dd	Zefd
ede	e
 dede
fddZdS )BsonConverterNFobjunstructure_as
check_keyscodec_optionsreturnc                 C   s   t | j||d||dS )N)r*   )r+   r,   )r   unstructure)selfr)   r*   r+   r,   r&   r&   r'   dumps    s
   zBsonConverter.dumpsdataclc                 C   s   |  t||d|S )N)r,   )	structurer   )r/   r1   r2   r,   r&   r&   r'   loads-   s   zBsonConverter.loads)r"   r#   r$   r
   r   boolr   bytesr0   typer    r4   r&   r&   r&   r'   r(      s2    
r(   	converterc                    s   ddt f fdd}dt dtf fdd} tdd	   t|  t|  td
d	  tt	t
tttdttttf	    tt  tt  tdd	   tdd	   tdd	   tt dS )a  
    Configure the converter for use with the bson library.

    * sets are serialized as lists
    * byte mapping keys are base85-encoded into strings when unstructuring, and reverse
    * non-string, non-byte mapping keys are coerced into strings when unstructuring
    * a deserialization hook is registered for bson.ObjectId by default
    * string and int enums are passed through when unstructuring

    .. versionchanged:: 24.2.0
        Enums are left to the library to unstructure, speeding them up.
    Nr2   c                    sN   t }t| dd }|rt|d t rd }nt|d trdd } j| ||dS )N__args__r   c                 S   s   t | dS )Nutf8)r   r   )kr&   r&   r'   key_handlerL   s   zIconfigure_converter.<locals>.gen_unstructure_mapping.<locals>.key_handler)unstructure_tor<   )strgetattrr   r6   gen_unstructure_mapping)r2   r=   r<   argsr8   r&   r'   r@   D   s   z4configure_converter.<locals>.gen_unstructure_mappingr-   c                    s>   t | dd }|rt|d trt|  td}|S t|  }|S )Nr9   r   )key_type)r?   r   r6   r   r!   )r2   rA   hrB   r&   r'   gen_structure_mappingS   s   
z2configure_converter.<locals>.gen_structure_mappingc                 S      t | S N)r   v_r&   r&   r'   <lambda>[       z%configure_converter.<locals>.<lambda>c                 S   rF   rG   )r   rH   r&   r&   r'   rK   _   rL   c                 S   s   |   S rG   )	isoformat)rI   r&   r&   r'   rK   h   rL   c                 S   s
   t | S rG   )r   fromisoformatrH   r&   r&   r'   rK   i   s   
 c                 S   s   t S rG   r   )tr&   r&   r'   rK   j   s    rG   )r   r   register_structure_hookr!   !register_unstructure_hook_factoryr   register_structure_hook_factoryr   r   r	   r>   r5   intfloatr6   r   r   register_unstructure_hookr   r   r   r   r   r   )r8   r@   rE   r&   rB   r'   configure_converter6   s"   rV   rA   kwargsr-   c                  O   s2   t ti|di |d< t| i |}t| |S )Nunstruct_collection_overrides)r   listgetr(   rV   )rA   rW   resr&   r&   r'   make_converterp   s   
r\   N).r%   base64r   r   collections.abcr   r   r   typingr   r   r	   bsonr
   r   r   r   r   r   _compatr   r   colsr   
convertersr   r   dispatchr   fnsr   literalsr   
strategiesr    r   r   r   r   r    r6   r!   r(   rV   r\   r&   r&   r&   r'   <module>   s(     :