o
    Zh'                     @   s<  d Z ddlZddlZddlZddlZddlZddlmZmZ ddlZddl	Z	ddl
Z
ddl
mZ ddlmZ ddlmZ dd	 Zd
edee fddZdedefddZdedeeef ddfddZejdddZG dd de
jZG dd deejZdefddZd
eeef deeef fddZd efd!d"ZdS )#zCommon utilities for the SDK.    N)AnyUnion)alias_generators   )_api_client)errorsc           	      C   s  |du rdS t |dd D ]\}}|dru|dd }|| vr>t|tr4dd tt|D | |< n
td| d| t|tr_t | | D ]\}}t|||d	 d ||  qI dS | | D ]}t|||d	 d | qc dS |d
r|dd }|| vri g| |< t| | d ||d	 d |  dS | |i } q| 	|d }|dur|sdS ||krdS t|t
rt|t
r|| dS td|d  d| d| d|| |d < dS )a9  Examples:

  set_value_by_path({}, ['a', 'b'], v)
    -> {'a': {'b': v}}
  set_value_by_path({}, ['a', 'b[]', c], [v1, v2])
    -> {'a': {'b': [{'c': v1}, {'c': v2}]}}
  set_value_by_path({'a': {'b': [{'c': v1}, {'c': v2}]}}, ['a', 'b[]', 'd'], v3)
    -> {'a': {'b': [{'c': v1, 'd': v3}, {'c': v2, 'd': v3}]}}
  N[]c                 S   s   g | ]}i qS  r   ).0_r   r   K/var/www/html/lang_env/lib/python3.10/site-packages/google/genai/_common.py
<listcomp>3   s    z%set_value_by_path.<locals>.<listcomp>zvalue z$ must be a list given an array path r   [0]r   z+Cannot set value for an existing key. Key: z; Existing value: z; New value: .)	enumerateendswith
isinstancelistrangelen
ValueErrorset_value_by_path
setdefaultgetdictupdate)	datakeysvalueikeykey_namejdZexisting_datar   r   r   r   "   sR   





 r   r   r    c                    s   dgkr| S t D ]k\ }| s dS |dr4|dd }|| v r1 fdd| | D   S  dS |dr[|dd }|| v rX| | rXt| | d	  d
 d   S  dS || v rd| | } qt| trtt| |rtt| |} q dS | S )zExamples:

  get_value_by_path({'a': {'b': v}}, ['a', 'b'])
    -> v
  get_value_by_path({'a': {'b': [{'c': v1}, {'c': v2}]}}, ['a', 'b[]', 'c'])
    -> [v1, v2]
  _selfNr	   r
   c                    s"   g | ]}t | d  d qS )r   N)get_value_by_path)r   r&   r"   r    r   r   r   p   s   " z%get_value_by_path.<locals>.<listcomp>r   r   r   r   )r   r   r(   r   	BaseModelhasattrgetattr)r   r    r#   r$   r   r)   r   r(   `   s*   


"
r(   objreturnc                 C   sP   t | tjr| jddS t | trdd |  D S t | tr&dd | D S | S )ad  Recursively converts a given object to a dictionary.

  If the object is a Pydantic model, it uses the model's `model_dump()` method.

  Args:
    obj: The object to convert.

  Returns:
    A dictionary representation of the object, a list of objects if a list is
    passed, or the object itself if it is not a dictionary, list, or Pydantic
    model.
  T)exclude_nonec                 S   s   i | ]	\}}|t |qS r   convert_to_dict)r   r#   r!   r   r   r   
<dictcomp>   s    z#convert_to_dict.<locals>.<dictcomp>c                 S      g | ]}t |qS r   r0   )r   itemr   r   r   r          z#convert_to_dict.<locals>.<listcomp>)r   pydanticr*   
model_dumpr   itemsr   )r-   r   r   r   r1      s   

r1   modelresponsec                 C   s   t | }|D ]b\}}dd | j D }|| jvr%||vr%|| q|||}| j| j}t|tu r?t	|d }t
|trQt|turQt|| qt
|t rj|D ]}t
|tritt	|d | qXqdS )zfRemoves extra fields from the response that are not in the model.

  Mutates the response in place.
  c                 S   s   i | ]\}}|j |qS r   )alias)r   r#   Z
field_infor   r   r   r2      s    z(_remove_extra_fields.<locals>.<dictcomp>r   N)r   r8   Zmodel_fieldspopr   
annotationtyping
get_originr   get_argsr   r   _remove_extra_fields)r9   r:   
key_valuesr#   r!   Z	alias_mapr=   r4   r   r   r   rA      s(   


rA   Tr*   )boundc                   @   sx   e Zd Zejejdddddddejfd	Z	e
deje deeef deeef d	efd
dZd	eeef fddZdS )r*   Tr   Zforbidbase64)	Zalias_generatorZpopulate_by_nameZfrom_attributesZprotected_namespacesextraZarbitrary_types_allowedZser_json_bytesZval_json_bytesZignored_typesclsr:   kwargsr.   c                C   s   t | | | |}|S N)rA   Zmodel_validate)rG   r:   rH   Zvalidated_responser   r   r   _from_response   s   

zBaseModel._from_responsec                 C   s   | j dddS )NTjson)r/   mode)r7   )selfr   r   r   to_json_dict   s   zBaseModel.to_json_dictN)__name__
__module____qualname__r6   Z
ConfigDictr   Zto_camelr>   TypeVarZmodel_configclassmethodTyperC   r   strobjectrJ   rN   r   r   r   r   r*      s.    


c                       s$   e Zd ZdZe fddZ  ZS )CaseInSensitiveEnumzCase insensitive enum.c                    s   z| |   W S  tyI   z	| |  W  Y S  tyH   t| d| j  zt | |}t||_	||_
|W  Y  Y S    Y Y Y d S w w )Nz is not a valid )upperKeyErrorlowerwarningswarnrO   super__new__rU   _name__value_)rG   r!   Zunknown_enum_val	__class__r   r   	_missing_   s    

zCaseInSensitiveEnum._missing_)rO   rP   rQ   __doc__rS   rc   __classcell__r   r   ra   r   rW      s    rW   c                  C   s0   t j  d} t jdd }|  d| S )z]Composes a timestamped unique name.

  Returns:
      A string representing a unique name.
  z%Y%m%d%H%M%Sr      r   )datetimenowstrftimeuuiduuid4hex)	timestamp	unique_idr   r   r   timestamped_unique_name   s   ro   c                 C   s   i }t | ts	| S |  D ]e\}}t |tr!t|d||< qt |tjr.| ||< qt |tr:t	|||< qt |t
rntdd |D rQdd |D ||< tdd |D rddd |D ||< qdd |D ||< q|||< q|S )	a  Converts unserializable types in dict to json.dumps() compatible types.

  This function is called in models.py after calling convert_to_dict(). The
  convert_to_dict() can convert pydantic object to dict. However, the input to
  convert_to_dict() is dict mixed of pydantic object and nested dict(the output
  of converters). So they may be bytes in the dict and they are out of
  `ser_json_bytes` control in model_dump(mode='json') called in
  `convert_to_dict`, as well as datetime deserialization in Pydantic json mode.

  Returns:
    A dictionary with json.dumps() incompatible type (e.g. bytes datetime)
    to compatible type (e.g. base64 encoded string, isoformat date string).
  asciic                 s   s    | ]}t |tV  qd S rI   )r   bytesr   vr   r   r   	<genexpr>  s    z.encode_unserializable_types.<locals>.<genexpr>c                 S   s   g | ]
}t |d qS )rp   )rE   urlsafe_b64encodedecoderr   r   r   r   r     s    z/encode_unserializable_types.<locals>.<listcomp>c                 s   s    | ]	}t |tjV  qd S rI   )r   rg   rr   r   r   r   rt   !  s    c                 S   s   g | ]}|  qS r   )	isoformatrr   r   r   r   r   "  r5   c                 S   r3   r   )encode_unserializable_typesrr   r   r   r   r   $  r5   )r   r   r8   rq   rE   ru   rv   rg   rw   rx   r   all)r   Zprocessed_datar#   r!   r   r   r   rx     s(   





rx   messagec                    s    fdd}|S )z&Experimental warning, only warns once.c                    s"   dt   fdd}|S )NFc                     s(   sdt jtjdd  | i |S )NT   )rz   category
stacklevel)r[   r\   r   ZExperimentalWarning)argsrH   )funcrz   warning_doner   r   wrapper.  s   z8experimental_warning.<locals>.decorator.<locals>.wrapper)	functoolswraps)r   r   rz   )r   r   r   	decorator,  s   
z'experimental_warning.<locals>.decoratorr   )rz   r   r   r   r   experimental_warning*  s   r   )rd   rE   rg   enumr   r>   r   r   rj   r[   r6   r    r   r   r   r   rU   r(   rV   r1   r   rA   rR   rC   r*   EnumrW   ro   rx   r   r   r   r   r   <module>   s:   >#

'"&