a
    !fy.                     @  s  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mZ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mZmZmZ d dlZddlmZmZmZmZmZ ddlm Z! dd	lm"Z" dd
lm#Z# e
dZ$e
dee%df dZ&e
de	e'e%f dZ(e
dee% dZ)e
dedef dZ*dddddZ+ddddddZ,dddd dd!d"d#Z-d$d%d&d'd(Z.dd)d&d*d+Z/d,d-d&d.d/Z0dd0d&d1d2Z1d3d4d&d5d6Z2dd7d&d8d9Z3d:d;d&d<d=Z4dd>d&d?d@Z5ddAd&dBdCZ6dDdEdFdGdHZ7dDdEdFdIdJZ8dDdEdFdKdLZ9dDdEdFdMdNZ:dDdDdFdOdPZ;dDddDdQdRdSZ<dddTdUdVZ=dWdXdYddZdZdZd[d\d]Z>dZdZd^d_d`Z?ddadbdcddZ@e
deZAe
dfZBedgdgd&dhdiZCedjdkd&dldiZCeddd&dmdiZCdndd&dodiZCdZddpdqdrZDdZdsdpdtduZEdZdEdpdvdwZFd dxdpdydzZGd d{dpd|d}ZHd d~dpddZIdZdZdZdddZJdZdZdZdddZKdZddddZLddZdZdddZMdZdddZNdS )    )annotationsN)	AnyTupleMappingTypeVarCallableIterableSequencecastoverload)Path)Required	Annotated	TypeGuardget_args
get_origin   )HeadersNotGiven	FileTypes
NotGivenOrHeadersLike)is_union)
parse_date)parse_datetime_T_TupleT.)bound	_MappingT
_SequenceT	CallableTzIterable[Iterable[_T]]zlist[_T])treturnc                 C  s   dd | D S )Nc                 S  s   g | ]}|D ]}|qqS  r#   ).0Zsublistitemr#   r#   U/var/www/html/python-backend/venv/lib/python3.9/site-packages/openai/_utils/_utils.py
<listcomp>$       zflatten.<locals>.<listcomp>r#   )r!   r#   r#   r&   flatten#   s    r)   zMapping[str, object]zSequence[Sequence[str]]zlist[tuple[str, FileTypes]])querypathsr"   c             	   C  s(   g }|D ]}| t| |ddd q|S )zRecursively extract files from the given dictionary based on specified paths.

    A path may look like this ['foo', 'files', '<array>', 'data'].

    Note: this mutates the given dictionary.
    r   Nindexflattened_key)extend_extract_items)r*   r+   filespathr#   r#   r&   extract_files'   s    r3   objectzSequence[str]intz
str | None)objr2   r-   r.   r"   c                  s  z }W nX t yd   t| tr,g  Y S ddlm} ||  d  d usPJ  tt| fg Y S 0 d7 t| rz$tkr| 	|}n| | }W n t
y   g  Y S 0  d u r| n d| d7  t| dS t| r|dk rg S t fd	d
| D S g S )Nr   )assert_is_file_content)key   []r,   z<array>c                   s,   g | ]$}t | d ur  d nddqS )Nz[]r,   )r0   )r$   r%   r.   r-   r2   r#   r&   r'   l   s   z"_extract_items.<locals>.<listcomp>)
IndexError
isinstancer   _filesr7   r
   r   is_dictlenpopKeyErrorr0   is_listr)   )r6   r2   r-   r.   r8   r7   r%   r#   r<   r&   r0   :   sF    



r0   zNotGivenOr[_T]zTypeGuard[_T])r6   r"   c                 C  s   t | t S Nr>   r   r6   r#   r#   r&   is_given{   s    rH   zTypeGuard[tuple[object, ...]]c                 C  s
   t | tS rE   r>   tuplerG   r#   r#   r&   is_tuple   s    rK   z_TupleT | objectzTypeGuard[_TupleT]c                 C  s
   t | tS rE   rI   rG   r#   r#   r&   
is_tuple_t   s    rL   zTypeGuard[Sequence[object]]c                 C  s
   t | tS rE   r>   r	   rG   r#   r#   r&   is_sequence   s    rN   z_SequenceT | objectzTypeGuard[_SequenceT]c                 C  s
   t | tS rE   rM   rG   r#   r#   r&   is_sequence_t   s    rO   zTypeGuard[Mapping[str, object]]c                 C  s
   t | tS rE   r>   r   rG   r#   r#   r&   
is_mapping   s    rQ   z_MappingT | objectzTypeGuard[_MappingT]c                 C  s
   t | tS rE   rP   rG   r#   r#   r&   is_mapping_t   s    rR   zTypeGuard[dict[object, object]]c                 C  s
   t | tS rE   )r>   dictrG   r#   r#   r&   r@      s    r@   zTypeGuard[list[object]]c                 C  s
   t | tS rE   )r>   listrG   r#   r#   r&   rD      s    rD   typebool)typr"   c                 C  s   t | tkS rE   )r   r   rW   r#   r#   r&   is_annotated_type   s    rY   c                 C  s   t | p
| tkS rE   )r   rT   rX   r#   r#   r&   is_list_type   s    rZ   c                 C  s   t t| S rE   )	_is_unionr   rX   r#   r#   r&   is_union_type   s    r\   c                 C  s   t | tkS rE   )r   r   rX   r#   r#   r&   is_required_type   s    r]   c                 C  s*   t | st| r&tttt| d S | S )Nr   )r]   rY   strip_annotated_typer
   rU   r   rX   r#   r#   r&   r^      s    r^   )rW   r-   r"   c              	   C  sF   t | }ztt|| W S  ty@   td|  d| dY n0 d S )NzExpected type z" to have a type argument at index z but it did not)r   r
   rU   r=   RuntimeError)rW   r-   argsr#   r#   r&   extract_type_arg   s
    ra   )r%   r"   c                 C  s@   t | r ttdd |  D S t| r<ttdd | D S | S )zMinimal reimplementation of copy.deepcopy() that will only copy certain object types:

    - mappings, e.g. `dict`
    - list

    This is done for performance reasons.
    c                 S  s   i | ]\}}|t |qS r#   deepcopy_minimal)r$   kvr#   r#   r&   
<dictcomp>   r(   z$deepcopy_minimal.<locals>.<dictcomp>c                 S  s   g | ]}t |qS r#   rb   )r$   entryr#   r#   r&   r'      r(   z$deepcopy_minimal.<locals>.<listcomp>)rQ   r
   r   itemsrD   )r%   r#   r#   r&   rc      s
    rc   z, or)delimfinalstr)seqrj   rk   r"   c                C  sn   t | }|dkrdS |dkr$| d S |dkrH| d  d| d| d  S || d d d| d| d   S )Nr    r9   r    )rA   join)rm   rj   rk   sizer#   r#   r&   
human_join   s    rs   )stringr"   c                 C  s   d|  dS )zOAdd single quotation marks around the given string. Does *not* do any escaping.'r#   )rt   r#   r#   r&   quote   s    rv   z Callable[[CallableT], CallableT])variantsr"   c                    s   ddd fdd}|S )aJ  Decorator to enforce a given set of arguments or variants of arguments are passed to the decorated function.

    Useful for enforcing runtime validation of overloaded functions.

    Example usage:
    ```py
    @overload
    def foo(*, a: str) -> str:
        ...

    @overload
    def foo(*, b: bool) -> str:
        ...

    # This enforces the same constraints that a static type checker would
    # i.e. that either a or b must be passed to the function
    @required_args(['a'], ['b'])
    def foo(*, a: str | None = None, b: bool | None = None) -> str:
        ...
    ```
    r    )funcr"   c                   sF   t  j}dd | D t dddd fdd}|S )Nc                 S  s&   g | ]\}}|j |j|jhv r|qS r#   )kindPOSITIONAL_ONLYPOSITIONAL_OR_KEYWORD)r$   nameparamr#   r#   r&   r'     s   z0required_args.<locals>.inner.<locals>.<listcomp>r4   )r`   kwargsr"   c            
        s4  t   t| D ]R\}}z |  W q ty^   tj dt dt|  dY q0 q| D ]} | qjD ]$}t fdd|D }|r~ q&q~tdkrt	dd D }d	| d
}nNt
t d   }	t|	dkrdt	dd |	D  }ndt|	d  }t|| i |S )Nz	() takes z argument(s) but z were givenc                 3  s   | ]}| v V  qd S rE   r#   )r$   r}   Zgiven_paramsr#   r&   	<genexpr>  r(   z@required_args.<locals>.inner.<locals>.wrapper.<locals>.<genexpr>r9   c                 S  s*   g | ]"}d t dd |D dd d qS )(c                 S  s   g | ]}t |qS r#   rv   r$   argr#   r#   r&   r'   "  r(   zLrequired_args.<locals>.inner.<locals>.wrapper.<locals>.<listcomp>.<listcomp>and)rk   ))rs   )r$   variantr#   r#   r&   r'   "  r(   zArequired_args.<locals>.inner.<locals>.wrapper.<locals>.<listcomp>z,Missing required arguments; Expected either z arguments to be givenr   zMissing required arguments: c                 S  s   g | ]}t |qS r#   r   r   r#   r#   r&   r'   )  r(   zMissing required argument: )set	enumerateaddr=   	TypeError__name__rA   keysallrs   rT   rv   )
r`   r~   i_r8   r   matchesZ
variationsmsgmissing)rx   
positionalrw   r   r&   wrapper  s.    ,z-required_args.<locals>.inner.<locals>.wrapper)inspect	signature
parametersrh   	functoolswraps)rx   paramsr   rw   )rx   r   r&   inner  s    
zrequired_args.<locals>.innerr#   )rw   r   r#   r   r&   required_args   s    .r   _K_VNonec                 C  s   d S rE   r#   rG   r#   r#   r&   strip_not_given8  s    r   zMapping[_K, _V | NotGiven]zdict[_K, _V]c                 C  s   d S rE   r#   rG   r#   r#   r&   r   =  s    c                 C  s   d S rE   r#   rG   r#   r#   r&   r   B  s    zobject | Nonec                 C  s*   | du rdS t | s| S dd |  D S )zHRemove all top-level keys where their values are instances of `NotGiven`Nc                 S  s    i | ]\}}t |ts||qS r#   rF   )r$   r8   valuer#   r#   r&   rf   O  r(   z#strip_not_given.<locals>.<dictcomp>)rQ   rh   rG   r#   r#   r&   r   G  s
    )valr"   c                 C  s   t | ddS )N
   )base)r5   r   r#   r#   r&   coerce_integerR  s    r   floatc                 C  s   t | S rE   )r   r   r#   r#   r&   coerce_floatV  s    r   c                 C  s   | dkp| dkp| dkS )Ntrue1onr#   r   r#   r#   r&   coerce_booleanZ  s    r   z
int | Nonec                 C  s   | d u rd S t | S rE   )r   r   r#   r#   r&   maybe_coerce_integer^  s    r   zfloat | Nonec                 C  s   | d u rd S t | S rE   )r   r   r#   r#   r&   maybe_coerce_floatd  s    r   zbool | Nonec                 C  s   | d u rd S t | S rE   )r   r   r#   r#   r&   maybe_coerce_booleanj  s    r   )rt   prefixr"   c                 C  s   |  |r| t|d S | S )zXRemove a prefix from a string.

    Backport of `str.removeprefix` for Python < 3.9
    N)
startswithrA   )rt   r   r#   r#   r&   removeprefixp  s    
r   )rt   suffixr"   c                 C  s    |  |r| dt|  S | S )zXRemove a suffix from a string.

    Backport of `str.removesuffix` for Python < 3.9
    N)endswithrA   )rt   r   r#   r#   r&   removesuffixz  s    
r   r   )r2   r"   c                 C  s    t |  }tj| }||fS rE   )r   
read_bytesosr2   basename)r2   contents	file_namer#   r#   r&   file_from_path  s    r   r   )headersheaderr"   c                 C  s   |  }t| trLtt| } |  D ]&\}}|  |kr$t|tr$|  S q$tddd |	 }|||
 |fD ]}| |}|rr|  S qrtd| dd S )Nz([^\w])(\w)c                 S  s   |  d|  d  S )Nr9   r   )groupupper)patr#   r#   r&   <lambda>  r(   z%get_required_header.<locals>.<lambda>zCould not find z header)lowerr>   r   r
   r   rh   rl   resub
capitalizer   get
ValueError)r   r   Zlower_headerrd   re   Zintercaps_headerZnormalized_headerr   r#   r#   r&   get_required_header  s    




r   )r"   c                   C  s$   z
t  W S  ty   Y dS 0 d S )Nfalse)sniffioZcurrent_async_library	Exceptionr#   r#   r#   r&   get_async_library  s    
r   )O
__future__r   r   r   r   r   typingr   r   r   r   r   r   r	   r
   r   pathlibr   Ztyping_extensionsr   r   r   r   r   r   _typesr   r   r   r   r   Z_compatr   r[   r   r   r   r4   r   rl   r   r   r    r)   r3   r0   rH   rK   rL   rN   rO   rQ   rR   r@   rD   rY   rZ   r\   r]   r^   ra   rc   rs   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r#   r#   r#   r&   <module>   sv   ,AH

