o
    î6Wj73  ã                   @  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 d dlmZmZ d dlZddlmZmZmZmZ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„Z%ddœd–dd „Z&d—d$d%„Z'd˜d-d.„Z(d™d1d2„Z)dšd4d5„Z*d›d8d9„Z+dœd;d<„Z,d�d?d@„Z-dždBdC„Z.dŸdFdG„Z/d dIdJ„Z0d¡dLdM„Z1d¢dOdP„Z2dQdRdSœd£dWdX„Z3d¤dZd[„Z4d¥d^d_„Z5e
d`ƒZ6e
daƒZ7ed¦dcdd„ƒZ8ed§dgdd„ƒZ8ed¨dhdd„ƒZ8d©djdd„Z8dªdldm„Z9d«dodp„Z:d¬drds„Z;d­dudv„Z<d®dxdy„Z=d¯d{d|„Z>d°d~d„Z?d±d�d‚„Z@d²d„d…„ZAd³d‰dŠ„ZBd´d‹dŒ„ZCd�dŽœdµd�d‘„ZDd¶d“d”„ZEdS )·é    )ÚannotationsN)	ÚAnyÚTupleÚMappingÚTypeVarÚCallableÚIterableÚSequenceÚcastÚoverload)ÚPath)ÚdateÚdatetime)Ú	TypeGuardÚget_argsé   )ÚOmitÚNotGivenÚ	FileTypesÚArrayFormatÚHeadersLikeÚ_TÚ_TupleT.)ÚboundÚ	_MappingTÚ
_SequenceTÚ	CallableTÚtúIterable[Iterable[_T]]Úreturnúlist[_T]c                 C  s   dd„ | D ƒS )Nc                 S  s   g | ]	}|D ]}|‘qqS © r!   )Ú.0ÚsublistÚitemr!   r!   úf/home/esfera/Documents/content_generation/venv/lib/python3.10/site-packages/anthropic/_utils/_utils.pyÚ
<listcomp>"   s    zflatten.<locals>.<listcomp>r!   )r   r!   r!   r%   Úflatten!   s   r'   Úbrackets)Úarray_formatÚqueryúMapping[str, object]ÚpathsúSequence[Sequence[str]]r)   r   úlist[tuple[str, FileTypes]]c             
   C  s*   g }|D ]}|  t| |dd|d�¡ q|S )a¨  Recursively extract files from the given dictionary based on specified paths.

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

    ``array_format`` controls how ``<array>`` segments contribute to the emitted
    field name. Supported values: ``"brackets"`` (``foo[]``), ``"repeat"`` and
    ``"comma"`` (``foo``), ``"indices"`` (``foo[0]``, ``foo[1]``).

    Note: this mutates the given dictionary.
    r   N©ÚindexÚflattened_keyr)   )ÚextendÚ_extract_items)r*   r,   r)   ÚfilesÚpathr!   r!   r%   Úextract_files%   s   r6   Úarray_indexÚintÚstrc                 C  sR   | dkrdS | dkrd|› d�S | dks| dkrdS t d	| › d
d ttƒ¡› �ƒ‚)Nr(   z[]Úindicesú[ú]ÚrepeatÚcommaÚ zUnknown array_format value: z, choose from ú, )ÚNotImplementedErrorÚjoinr   r   )r)   r7   r!   r!   r%   Ú_array_suffix=   s   ÿrC   ÚobjÚobjectr5   úSequence[str]r0   r1   ú
str | Nonec             
     s†  zˆˆ }W n[ t ya   t| ƒsg  Y S ddlm} ˆd us!J ‚t| ƒrQg }t| ƒD ]!\}}	tˆ |ƒ}
ˆr:ˆ|
 n|
}||	|d� | |tt	|	ƒf¡ q+| Y S || ˆd� ˆtt	| ƒfg Y S w ˆd7 ‰t
| ƒr§ztdd„ ˆˆd … D ƒƒr~|  |¡}n| | }W n tyŽ   g  Y S w ˆd u r–|‰nˆd|› d�7 ‰t|ˆˆˆˆ d	�S t| ƒrÁ|d
kr±g S t‡ ‡‡‡fdd„t| ƒD ƒƒS g S )Nr   )Úassert_is_file_content)Úkeyé   c                 s  s   � | ]}|d kV  qdS )ú<array>Nr!   )r"   Úpr!   r!   r%   Ú	<genexpr>q   ó   € z!_extract_items.<locals>.<genexpr>r;   r<   r/   rK   c              
     s8   g | ]\}}t |ˆˆˆd urˆndtˆ |ƒ ˆ d�‘qS )Nr?   r/   )r3   rC   )r"   r7   r$   ©r)   r1   r0   r5   r!   r%   r&   Š   s    
÷ùÿz"_extract_items.<locals>.<listcomp>)Ú
IndexErrorÚis_givenÚ_filesrH   Úis_listÚ	enumeraterC   Úappendr
   r   Úis_dictÚallÚpopÚKeyErrorr3   r'   )rD   r5   r0   r1   r)   rI   rH   r4   r7   ÚentryÚsuffixÚemitted_keyr$   r!   rO   r%   r3   K   s^   
ë€üû
öÿr3   ú_T | NotGiven | OmitúTypeGuard[_T]c                 C  s   t | tƒ ot | tƒ S ©N)Ú
isinstancer   r   ©rD   r!   r!   r%   rQ   œ   ó   rQ   úTypeGuard[tuple[object, ...]]c                 C  ó
   t | tƒS r_   ©r`   Útuplera   r!   r!   r%   Úis_tupleª   ó   
rg   ú_TupleT | objectúTypeGuard[_TupleT]c                 C  rd   r_   re   ra   r!   r!   r%   Ú
is_tuple_t®   rh   rk   úTypeGuard[Sequence[object]]c                 C  rd   r_   ©r`   r	   ra   r!   r!   r%   Úis_sequence²   rh   rn   ú_SequenceT | objectúTypeGuard[_SequenceT]c                 C  rd   r_   rm   ra   r!   r!   r%   Úis_sequence_t¶   rh   rq   úTypeGuard[Mapping[str, object]]c                 C  rd   r_   ©r`   r   ra   r!   r!   r%   Ú
is_mappingº   rh   rt   ú_MappingT | objectúTypeGuard[_MappingT]c                 C  rd   r_   rs   ra   r!   r!   r%   Úis_mapping_t¾   rh   rw   úTypeGuard[dict[object, object]]c                 C  rd   r_   )r`   Údictra   r!   r!   r%   rV   Â   rh   rV   úTypeGuard[list[object]]c                 C  rd   r_   )r`   Úlistra   r!   r!   r%   rS   Æ   rh   rS   úTypeGuard[Iterable[object]]c                 C  rd   r_   )r`   r   ra   r!   r!   r%   Úis_iterableÊ   rh   r}   r@   Úor)ÚdelimÚfinalÚseqr   r€   c                C  sn   t | ƒ}|dkr
dS |dkr| d S |dkr$| d › d|› d| d › �S | | d d… ¡d|› d| d › � S )Nr   r?   rJ   r   ú éÿÿÿÿ)ÚlenrB   )r�   r   r€   Úsizer!   r!   r%   Ú
human_joinÏ   s   &r†   Ústringc                 C  s   d| › d�S )zOAdd single quotation marks around the given string. Does *not* do any escaping.ú'r!   )r‡   r!   r!   r%   ÚquoteÝ   s   r‰   Úvariantsú Callable[[CallableT], CallableT]c                    s   d‡ fdd„}|S )a4  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: ...
    ```
    Úfuncr   r   c                   s>   t  ˆ ¡j}dd„ | ¡ D ƒ‰t ˆ ¡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>ÚargsrE   Úkwargsr   c            
        sH  t ƒ ‰ t| ƒD ]'\}}z	ˆ  ˆ| ¡ W q ty.   tˆj› dtˆƒ› dt| ƒ› d�ƒd ‚w | ¡ D ]}ˆ  |¡ q3ˆD ]}t‡ fdd„|D ƒƒ}|rN nOq=tˆƒdkrht	dd„ ˆD ƒƒ}d	|› d
�}t|ƒ‚tˆƒdkspJ ‚t
t ˆd ƒˆ  ƒ}	t|	ƒdkr�dt	dd„ |	D ƒƒ› �}t|ƒ‚dt|	d ƒ› �}t|ƒ‚ˆ| i |¤ŽS )Nz	() takes z argument(s) but z were givenc                 3  s   � | ]}|ˆ v V  qd S r_   r!   )r"   r‘   ©Úgiven_paramsr!   r%   rM     rN   z@required_args.<locals>.inner.<locals>.wrapper.<locals>.<genexpr>rJ   c                 S  s*   g | ]}d t dd„ |D ƒdd� d ‘qS )ú(c                 S  ó   g | ]}t |ƒ‘qS r!   ©r‰   ©r"   Úargr!   r!   r%   r&     ó    zLrequired_args.<locals>.inner.<locals>.wrapper.<locals>.<listcomp>.<listcomp>Úand)r€   ú))r†   )r"   Úvariantr!   r!   r%   r&     s   * 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  r—   r!   r˜   r™   r!   r!   r%   r&   "  r›   zMissing required argument: )ÚsetrT   ÚaddrP   Ú	TypeErrorÚ__name__r„   ÚkeysrW   r†   r{   r‰   )
r’   r“   ÚiÚ_rI   rž   ÚmatchesÚ
variationsÚmsgÚmissing)rŒ   Ú
positionalrŠ   r”   r%   Úwrapper  s@   ÿþÿÿÿ
øÿz-required_args.<locals>.inner.<locals>.wrapper)r’   rE   r“   rE   r   rE   )ÚinspectÚ	signatureÚ
parametersÚitemsÚ	functoolsÚwraps)rŒ   Úparamsr«   ©rŠ   )rŒ   rª   r%   Úinnerø   s   þ
#zrequired_args.<locals>.innerN)rŒ   r   r   r   r!   )rŠ   r´   r!   r³   r%   Úrequired_argsâ   s   2rµ   Ú_KÚ_VÚNonec                 C  ó   d S r_   r!   ra   r!   r!   r%   Ústrip_not_given1  ó   rº   úMapping[_K, _V | NotGiven]údict[_K, _V]c                 C  r¹   r_   r!   ra   r!   r!   r%   rº   5  r»   c                 C  r¹   r_   r!   ra   r!   r!   r%   rº   9  r»   ú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!   )r`   r   ©r"   rI   Úvaluer!   r!   r%   Ú
<dictcomp>E  s     z#strip_not_given.<locals>.<dictcomp>)rt   r¯   ra   r!   r!   r%   rº   =  s
   Úvalc                 C  s   t | dd�S )Né
   )Úbase)r8   ©rÂ   r!   r!   r%   Úcoerce_integerH  s   rÆ   Úfloatc                 C  s   t | ƒS r_   )rÇ   rÅ   r!   r!   r%   Úcoerce_floatL  s   rÈ   Úboolc                 C  s   | dkp| dkp| dkS )NÚtrueÚ1Úonr!   rÅ   r!   r!   r%   Úcoerce_booleanP  rb   rÍ   ú
int | Nonec                 C  ó   | d u rd S t | ƒS r_   )rÆ   rÅ   r!   r!   r%   Úmaybe_coerce_integerT  ó   rÐ   úfloat | Nonec                 C  rÏ   r_   )rÈ   rÅ   r!   r!   r%   Úmaybe_coerce_floatZ  rÑ   rÓ   úbool | Nonec                 C  rÏ   r_   )rÍ   rÅ   r!   r!   r%   Úmaybe_coerce_boolean`  rÑ   rÕ   Úprefixc                 C  s   |   |¡r| t|ƒd… S | S )zXRemove a prefix from a string.

    Backport of `str.removeprefix` for Python < 3.9
    N)Ú
startswithr„   )r‡   rÖ   r!   r!   r%   Úremoveprefixf  s   
rØ   r[   c                 C  s    |   |¡r| dt|ƒ … S | S )zXRemove a suffix from a string.

    Backport of `str.removesuffix` for Python < 3.9
    N)Úendswithr„   )r‡   r[   r!   r!   r%   Úremovesuffixp  s   
rÚ   r   c                 C  s    t | ƒ ¡ }tj | ¡}||fS r_   )r   Ú
read_bytesÚosr5   Úbasename)r5   ÚcontentsÚ	file_namer!   r!   r%   Úfile_from_pathz  s   rà   Úheadersr   Úheaderc                 C  s’   |  ¡ }t| ƒr |  ¡ D ]\}}|  ¡ |krt|tƒr|  S qt ddd„ | ¡ ¡}||| ¡ |fD ]}|  	|¡}|r@|  S q3t
d|› d�ƒ‚)Nz([^\w])(\w)c                 S  s   |   d¡|   d¡ ¡  S )NrJ   r   )ÚgroupÚupper)Úpatr!   r!   r%   Ú<lambda>‰  s    z%get_required_header.<locals>.<lambda>zCould not find z header)Úlowerrw   r¯   r`   r9   ÚreÚsubÚ
capitalizerä   ÚgetÚ
ValueError)rá   râ   Úlower_headerÚkÚvÚintercaps_headerÚnormalized_headerrÀ   r!   r!   r%   Úget_required_header€  s   €
ÿrò   c                   C  s    zt  ¡ W S  ty   Y dS w )NÚfalse)ÚsniffioÚcurrent_async_libraryÚ	Exceptionr!   r!   r!   r%   Úget_async_library“  s
   
ÿr÷   é€   ©Úmaxsizerú   c                 C  s   t j| d�}tt|ƒS )zmA version of functools.lru_cache that retains the type signature
    for the wrapped function arguments.
    rù   )r°   Ú	lru_cacher
   r   )rú   r«   r!   r!   r%   rû   š  s   ÿ
rû   Údatac                 C  sZ   t | ƒrdd„ |  ¡ D ƒS t| ƒr t| tttfƒs dd„ | D ƒS t| ttfƒr+|  	¡ S | S )zvTranslates a mapping / sequence recursively in the same fashion
    as `pydantic` v2's `model_dump(mode="json")`.
    c                 S  s   i | ]\}}t |ƒt |ƒ“qS r!   ©Ú	json_safer¿   r!   r!   r%   rÁ   ©  s    zjson_safe.<locals>.<dictcomp>c                 S  r—   r!   rý   )r"   r$   r!   r!   r%   r&   ¬  r›   zjson_safe.<locals>.<listcomp>)
rt   r¯   r}   r`   r9   ÚbytesÚ	bytearrayr   r   Ú	isoformat)rü   r!   r!   r%   rþ   ¤  s   rþ   )r   r   r   r    )r*   r+   r,   r-   r)   r   r   r.   )r)   r   r7   r8   r   r9   )rD   rE   r5   rF   r0   r8   r1   rG   r)   r   r   r.   )rD   r]   r   r^   )rD   rE   r   rc   )rD   ri   r   rj   )rD   rE   r   rl   )rD   ro   r   rp   )rD   rE   r   rr   )rD   ru   r   rv   )rD   rE   r   rx   )rD   rE   r   rz   )rD   rE   r   r|   )r�   rF   r   r9   r€   r9   r   r9   )r‡   r9   r   r9   )rŠ   rF   r   r‹   )rD   r¸   r   r¸   )rD   r¼   r   r½   )rD   rE   r   rE   )rD   r¾   r   rE   )rÂ   r9   r   r8   )rÂ   r9   r   rÇ   )rÂ   r9   r   rÉ   )rÂ   rG   r   rÎ   )rÂ   rG   r   rÒ   )rÂ   rG   r   rÔ   )r‡   r9   rÖ   r9   r   r9   )r‡   r9   r[   r9   r   r9   )r5   r9   r   r   )rá   r   râ   r9   r   r9   )r   r9   )rú   rÎ   r   r‹   )rü   rE   r   rE   )FÚ
__future__r   rÜ   rè   r¬   r°   Útypingr   r   r   r   r   r   r	   r
   r   Úpathlibr   r   r   Útyping_extensionsr   r   rô   Ú_typesr   r   r   r   r   r   rE   r   r9   r   r   r   r'   r6   rC   r3   rQ   rg   rk   rn   rq   rt   rw   rV   rS   r}   r†   r‰   rµ   r¶   r·   rº   rÆ   rÈ   rÍ   rÐ   rÓ   rÕ   rØ   rÚ   rà   rò   r÷   rû   rþ   r!   r!   r!   r%   Ú<module>   sp    ,

ú


Q










K














