a
    f2                     @  s  d dl mZ d dlZd dlZd dlZd dlmZmZmZm	Z	 d dl
mZm
Z
 d dlmZmZmZmZ d dlZd dlZddlmZmZmZ dd	lmZ dd
lmZmZmZmZmZmZm Z  ddl!m"Z"m#Z# edZ$ed Z%G dd dZ&ddddddZ'ddddddZ(dddddZ)ddddddZ*dd ddddd!d"d#Z+dd$d%dd&d'd(Z,d)dd)dd*d+Z-ddddd,d-Z.ddddd.d/Z/dd ddddd!d0d1Z0dd$d%dd&d2d3Z1d)dd)dd4d5Z2dS )6    )annotationsN)AnyMappingTypeVarcast)datedatetime)Literalget_argsoverrideget_type_hints   )is_list
is_mappingis_iterable   )is_base64_file_input)is_list_typeis_union_typeextract_type_argis_iterable_typeis_required_typeis_annotated_typestrip_annotated_type)
model_dumpis_typeddict_T)iso8601base64customc                   @  sf   e Zd ZU dZded< ded< ded< ded< ddddd	ddddd
dddZeddddZdS )PropertyInfoao  Metadata class to be used in Annotated types to provide information about a given type.

    For example:

    class MyParams(TypedDict):
        account_holder_name: Annotated[str, PropertyInfo(alias='accountHolderName')]

    This means that {'account_holder_name': 'Robert'} will be transformed to {'accountHolderName': 'Robert'} before being sent to the API.
    
str | NonealiaszPropertyFormat | Noneformatformat_templatediscriminatorNr"   r#   r$   r%   None)r"   r#   r$   r%   returnc                C  s   || _ || _|| _|| _d S )Nr&   )selfr"   r#   r$   r%    r*   Q/var/www/ai-form-bot/venv/lib/python3.9/site-packages/openai/_utils/_transform.py__init__8   s    zPropertyInfo.__init__str)r(   c              
   C  s.   | j j d| j d| j d| j d| j d
S )Nz(alias='z
', format=z, format_template='z', discriminator='z'))	__class____name__r"   r#   r$   r%   )r)   r*   r*   r+   __repr__E   s    zPropertyInfo.__repr__)r/   
__module____qualname____doc____annotations__r,   r   r0   r*   r*   r*   r+   r    (   s   

r    objectz
Any | None)dataexpected_typer(   c                 C  s   | du rdS t | |S )ziWrapper over `transform()` that allows `None` to be passed.

    See `transform()` for more details.
    N)	transformr6   r7   r*   r*   r+   maybe_transformJ   s    r:   c                 C  s   t | tt|d}tt|S )  Transform dictionaries based off of type information from the given type, for example:

    ```py
    class Params(TypedDict, total=False):
        card_id: Required[Annotated[str, PropertyInfo(alias="cardID")]]


    transformed = transform({"card_id": "<my card ID>"}, Params)
    # {'cardID': '<my card ID>'}
    ```

    Any keys / data that does not have type information given will be included as is.

    It should be noted that the transformations that this function does are not represented in the type system.
    
annotation)_transform_recursiver   typer   r6   r7   Ztransformedr*   r*   r+   r8   X   s    r8   r?   ztype | None)type_r(   c                 C  s$   t | rt| d } t| r | S dS )zIf the given type is an `Annotated` type then it is returned, if not `None` is returned.

    This also unwraps the type when applicable, e.g. `Required[Annotated[T, ...]]`
    r   N)r   r
   r   )rA   r*   r*   r+   _get_annotated_typeo   s
    rB   r-   )keyrA   r(   c                 C  sP   t |}|du r| S t|dd }|D ]"}t|tr(|jdur(|j  S q(| S )zTransform the given `data` based on the annotations provided in `type_`.

    Note: this function only looks at `Annotated` types that contain `PropertInfo` metadata.
    Nr   )rB   r
   
isinstancer    r"   )rC   rA   annotated_typer   r=   r*   r*   r+   _maybe_transform_key~   s    rF   )
inner_type)r6   r=   rG   r(   c                  s  du r t }t|r.t| r.t| |S t|r>t| sXt|rvt| rvt| t	svt
|d fdd| D S t|rt|D ]}t|  |d} q| S t| tjrt| ddS t }|du r| S t|dd }|D ], t trވ jdurt|  j j  S q| S )	  Transform the given data against the expected type.

    Args:
        annotation: The direct type annotation given to the particular piece of data.
            This may or may not be wrapped in metadata types, e.g. `Required[T]`, `Annotated[T, ...]` etc

        inner_type: If applicable, this is the "inside" type. This is useful in certain cases where the outside type
            is a container type such as `List[T]`. In that case `inner_type` should be set to `T` so that each entry in
            the list can be transformed using the metadata from the container type.

            Defaults to the same value as the `annotation` argument.
    Nr   c                   s   g | ]}t | d qS )r=   rG   )r>   .0drI   r*   r+   
<listcomp>       z(_transform_recursive.<locals>.<listcomp>rI   TZexclude_unsetr   )r   r   r   _transform_typeddictr   r   r   r   rD   r-   r   r   r
   r>   pydantic	BaseModelr   rB   r    r#   _format_datar$   r6   r=   rG   Zstripped_typesubtyperE   r   r*   rI   r+   r>      s>    

r>   PropertyFormatr!   )r6   format_r$   r(   c                 C  s   t | ttfr8|dkr|  S |dkr8|d ur8| |S |dkrt| rd }t | tjrb|  }n&t | t	j
r|  }t |tr| }t |tstd|  dt| t|dS | S Nr   r   r   zCould not read bytes from z; Received ascii)rD   r   r   	isoformatstrftimer   pathlibPath
read_bytesioIOBasereadr-   encodebytesRuntimeErrorr?   r   	b64encodedecoder6   rW   r$   binaryr*   r*   r+   rS      s"    



rS   zMapping[str, object]c                 C  sX   i }t |dd}|  D ]:\}}||}|d u r<|||< qt||d|t||< q|S NT)include_extrasr<   )r   itemsgetr>   rF   r6   r7   resultr   rC   valuerA   r*   r*   r+   rP      s    

rP   c                   s   | du rdS t | |I dH S )zuWrapper over `async_transform()` that allows `None` to be passed.

    See `async_transform()` for more details.
    N)async_transformr9   r*   r*   r+   async_maybe_transform   s    rq   c                   s"   t | tt|dI dH }tt|S )r;   r<   N)_async_transform_recursiver   r?   r   r@   r*   r*   r+   rp     s    rp   c                  s(  du r t }t|r4t| r4t| |I dH S t|rDt| s^t|rt| rt| t	st
|d fdd| D I dH S t|rt|D ]}t|  |dI dH } q| S t| tjrt| ddS t }|du r| S t|dd }|D ]2 t tr jdurt|  j jI dH   S q| S )	rH   Nr   c                   s    g | ]}t | d I dH qS )rI   N)rr   rJ   rI   r*   r+   rM   ;  rN   z._async_transform_recursive.<locals>.<listcomp>rI   TrO   r   )r   r   r   _async_transform_typeddictr   r   r   r   rD   r-   r   r   r
   rr   rQ   rR   r   rB   r    r#   _async_format_datar$   rT   r*   rI   r+   rr     s>    
rr   c                   s   t | ttfr8|dkr|  S |dkr8|d ur8| |S |dkrt| rd }t | tjrnt| 	 I d H }n&t | t
jr|  }t |tr| }t |tstd|  dt| t|dS | S rX   )rD   r   r   rZ   r[   r   r\   r]   anyior^   r_   r`   ra   r-   rb   rc   rd   r?   r   re   rf   rg   r*   r*   r+   rt   V  s"    


rt   c                   s^   i }t |dd}|  D ]@\}}||}|d u r<|||< qt||dI d H |t||< q|S ri   )r   rk   rl   rr   rF   rm   r*   r*   r+   rs   q  s    

rs   )3
__future__r   r_   r   r\   typingr   r   r   r   r   r   typing_extensionsr	   r
   r   r   ru   rQ   Z_utilsr   r   r   _filesr   Z_typingr   r   r   r   r   r   r   _compatr   r   r   rV   r    r:   r8   rB   rF   r>   rS   rP   rq   rp   rr   rt   rs   r*   r*   r*   r+   <module>   s:   $	";;