o
    pkjk                  	   @   sb  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mZm	Z	 d dl
mZ d dl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 dd
lmZ ddlmZ ddlmZ ddlmZmZ edZ	d*deee ef e!f deee ef  dee ef fddZ"	d*deee ef e!f deee ef  dee ef fddZ#	d*deee ef e!f deee ef  dee ef fddZ$	d*deee ef e!f deee ef  dee ef fddZ%	d*deee ef e!f deee ef  dee ef fddZ&	d*deee ef e!f deee ef  dee ef fddZ'	d*deee ef e!f deee ef  dee ef fddZ(	d*deee ef e!f deee ef  dee ef fd d!Z)	d*deee ef e!f deee ef  dee ef fd"d#Z*	d*deee ef e!f deee ef  dee ef fd$d%Z+G d&d' d'ej,Z-G d(d) d)ej,Z.dS )+    N)AnyOptionalUnionoverload)	urlencode   )_api_client)_api_module)_common)_extra_utils)_transformers)types)get_value_by_path)set_value_by_path)
AsyncPagerPagerzgoogle_genai.filesfrom_objectparent_objectreturnc                 C   .   i }t | dgd urt|dgt | dg |S )Nfilegetvsetvr   r   	to_object r   P/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/google/genai/files.py_CreateFileParameters_to_mldev)      r   c                 C   .   i }t | dgd urt|dgt | dg |S NsdkHttpResponsesdk_http_responser   r   r   r   r   _CreateFileResponse_from_mldev4      r$   c                 C   6   i }t | dgd urt|ddgtt | dg |S Nname_urlr   r   r   tt_file_namer   r   r   r   _DeleteFileParameters_to_mldevA      r-   c                 C   r    r!   r   r   r   r   r   _DeleteFileResponse_from_mldevN   r%   r/   c                 C   r&   r'   r*   r   r   r   r   _GetFileParameters_to_mldev[   r.   r0   c                 C   r   )Nurisr   r   r   r   r   )_InternalRegisterFilesParameters_to_mldevh   r   r2   c                 C   sX   i }t | dgd urt|ddgt | dg t | dgd ur*t|ddgt | dg |S )N	page_size_querypageSize
page_token	pageTokenr   r   r   r   r   _ListFilesConfig_to_mldevs   s   
r8   c                 C   s*   i }t | dgd urtt | dg| |S )Nconfig)r   r8   r   r   r   r   _ListFilesParameters_to_mldev   s   r:   c                 C   s   i }t | dgd urt|dgt | dg t | dgd ur(t|dgt | dg t | dgd ur@t|dgdd t | dgD  |S )Nr"   r#   nextPageTokennext_page_tokenfilesc                 S      g | ]}|qS r   r   .0itemr   r   r   
<listcomp>       z1_ListFilesResponse_from_mldev.<locals>.<listcomp>r   r   r   r   r   _ListFilesResponse_from_mldev   s    rD   c                 C   s^   i }t | dgd urt|dgt | dg t | dgd ur-t|dgdd t | dgD  |S )Nr"   r#   r=   c                 S   r>   r   r   r?   r   r   r   rB      rC   z5_RegisterFilesResponse_from_mldev.<locals>.<listcomp>r   r   r   r   r   !_RegisterFilesResponse_from_mldev   s    rE   c                
   @   2  e Zd Zdddeej dejfddZdddejdeej	 dej
fdd	Zddd
edeej dejfddZddd
edeej dejfddZdddeje deej dejfddZdddeeeje ejf deej dejfddZeddddeeejej ej!f dddeej" de#fddZ$edddeeejej ej!f deeeje ejf deej" ddfddZ$ddddeeejej ej!f deeeeje ejf  deej" dee# fddZ$ddde%j&j'j(deje deej dejfddZ)dddeej de*ej fddZdS )FilesNr9   r9   r   c             	   C   sF  t j|d}| jjrtdt|}|d}|rd|}nd}|d}|r1| dt| }|	dd  d }|j
d urH|j
jd urH|j
j}t|}t|}| jd|||}|js`i nt|j}	| jjsnt|	}	t jj|	t|dd rdt|j
d	d t|j
d
d t|j
dd dini d}
t j|jd|
_| j|
 |
S NrH   iThis method is only supported in Gemini Developer API mode, not in Gemini Enterprise Agent Platform mode.r)   r=   r4   ?r9   getresponse_schemaresponse_json_schemainclude_all_fieldsrM   rN   rO   responsekwargsheaders)r   _ListFilesParametersr   vertexai
ValueErrorr:   rL   
format_mapr   popr9   http_optionsr
   convert_to_dictencode_unserializable_typesrequestbodyjsonloadsrD   ListFilesResponse_from_responsegetattrHttpResponserU   r#   _verify_responseselfr9   parameter_modelrequest_dictrequest_url_dictpathquery_paramsr[   rR   response_dictreturn_valuer   r   r   _list   sb   





zFiles._listr   c             	   C   h  t j||d}| jjrtdt|}|d}|r d|}nd}|d}|r2| dt| }|	dd  d }|j
d urI|j
jd urI|j
j}t|}t|}| jd|||}	|d urtt|d	d rtt j|	d
}
| j|
 |
S |	jsyi nt|	j}| jjst|}t jj|t|dd rdt|j
dd t|j
dd t|j
dd dini d}
| j|
 |
S Nr   r9   rJ   r)   zupload/v1beta/filesr4   rK   r9   postshould_return_http_responser#   rM   rN   rO   rP   rQ   )r   _CreateFileParametersr   rW   rX   r   rL   rY   r   rZ   r9   r[   r
   r\   r]   r^   rd   CreateFileResponserf   r_   r`   ra   r$   rc   rh   r   r9   ri   rj   rk   rl   rm   r[   rR   ro   rn   r   r   r   _create   n   






zFiles._creater(   c             	   C   s(  t j||d}| jjrtdt|}|d}|r d|}nd}|d}|r2| dt| }|	dd d}|j
durI|j
jdurI|j
j}t|}t|}| jd	|||}	|	jsai nt|	j}
t jj|
t|ddrdt|j
d
dt|j
ddt|j
dddini d}| j| |S )ai  Retrieves the file information from the service.

    Args:
      name (str): The name identifier for the file to retrieve.
      config (GetFileConfig): Optional, configuration for the get method.

    Returns:
      File: The file information.

    Usage:

    .. code-block:: python

      file = client.files.get(name='files/...')
      print(file.uri)
    r(   r9   rJ   r)   files/{file}r4   rK   r9   NrL   rM   rN   rO   rP   rQ   )r   _GetFileParametersr   rW   rX   r0   rL   rY   r   rZ   r9   r[   r
   r\   r]   r^   r_   r`   ra   Filerc   rd   rf   rh   r(   r9   ri   rj   rk   rl   rm   r[   rR   rn   ro   r   r   r   rL   K  sZ   





z	Files.getc             	   C   sH  t j||d}| jjrtdt|}|d}|r d|}nd}|d}|r2| dt| }|	dd d}|j
durI|j
jdurI|j
j}t|}t|}| jd	|||}	|	jsai nt|	j}
| jjsot|
}
t jj|
t|ddrdt|j
d
dt|j
ddt|j
dddini d}t j|	jd|_| j| |S )a]  Deletes a remotely stored file.

    Args:
      name (str): The name identifier for the file to delete.
      config (DeleteFileConfig): Optional, configuration for the delete method.

    Returns:
      DeleteFileResponse: The response for the delete method

    Usage:

    .. code-block:: python

      client.files.delete(name='files/...')
    r|   rJ   r)   r}   r4   rK   r9   NdeleterM   rN   rO   rP   rQ   rT   )r   _DeleteFileParametersr   rW   rX   r-   rL   rY   r   rZ   r9   r[   r
   r\   r]   r^   r_   r`   ra   r/   DeleteFileResponserc   rd   re   rU   r#   rf   r   r   r   r   r     sh   





zFiles.deleter1   c             	   C   rq   Nr1   r9   rJ   r)   zfiles:registerr4   rK   r9   rt   ru   rv   rM   rN   rO   rP   rQ   )r    _InternalRegisterFilesParametersr   rW   rX   r2   rL   rY   r   rZ   r9   r[   r
   r\   r]   r^   rd   RegisterFilesResponserf   r_   r`   ra   rE   rc   rh   r1   r9   ri   rj   rk   rl   rm   r[   rR   ro   rn   r   r   r   _register_files  r{   zFiles._register_filesc                C   s^  | j jrtdt }|r*t|trtjdi |}n|}tj|j|j	|j
d}nt }|j	dur@|j	ds@d|j	 |_	tj||j|jd\}}}||_||_| j|tj|ddd}|jdu sp|jjdu spd	|jjvrttd
|jjd	 }	t|tjr| j j||	|j|d}
nt|}| j j||	|j|d}
tjj|
jd |r| dS i dS )a  Calls the API to upload a file using a supported file service.

    Args:
      file: A path to the file or an `IOBase` object to be uploaded. If it's an
        IOBase object, it must be opened in blocking (the default) mode and
        binary mode. In other words, do not use non-blocking mode or text mode.
        The given stream must be seekable, that is, it must be able to call
        `seek()` on 'path'.
      config: Optional parameters to set `display_name`, `mime_type`, and
        `name`.
    =This method is only supported in the Gemini Developer client.	mime_typer(   display_nameNfiles/user_http_optionsuser_mime_typeTr[   ru   rs   x-goog-upload-urlPFailed to create file. Upload URL was not returned from the create file request.r[   r   rQ   r   )r   rW   rX   r   UploadFileConfig
isinstancedictr   r   r(   r   
startswithr   prepare_resumable_uploadr[   
size_bytesrz   CreateFileConfigr#   rU   KeyErrorioIOBaseupload_fileosfspathrc   r`   
model_dumprh   r   r9   config_modelfile_objr[   r   r   rR   
upload_urlreturn_filefs_pathr   r   r   uploadB  sf   





zFiles.uploaddestinationr9   r   c                C      d S Nr   rh   r   r   r9   r   r   r   download     zFiles.downloadc                C   r   r   r   r   r   r   r   r     r   c          
      C   s   | j jrtdd}|rt|trtjdi |}n|}t|tjr+|jdu r+tdt	
|}d| d}ddi}| dt| }d}t|d	gdurSt|d	g}| j j|||d
}	|du rzt|tjrk|	|_|	S t|tjrz|jdurz|	|j_|	S )a  Downloads a file's data from storage.

    Files created by `upload` can't be downloaded. You can tell which files are
    downloadable by checking the `source` or `download_uri` property.

    Note: When destination is None, this method returns the data as bytes. For
    `Video` and `GeneratedVideo` objects, if destination is None, it also sets
    the
    `video_bytes` property on the `Video` object. When destination is provided,
    chunks are streamed directly to disk or the writable stream without loading
    the full file into memory, and None is returned.

    Args:
      file: A file name, uri, or file object identifying which file to download.
      destination: Optional, a local file path or writable binary stream to
        write the file directly to in chunks, avoiding holding the full file in
        memory.
      config: Optional, configuration for the get method.

    Returns:
      bytes: The file data as bytes if destination is None; otherwise None.

    Usage:

    .. code-block:: python

      for file in client.files.list():
        if file.download_uri is not None:
          break
      else:
        raise ValueError('No files found with a `download_uri`.')

      # Download to memory:
      data = client.files.download(file=file)

      # Stream directly to a file path (avoids memory spike):
      client.files.download(file=file, destination="output.mp4")

      # Download video object:
      video = types.Video(uri=file.uri)
      video_bytes = client.files.download(file=video)
      video.video_bytes
    r   NOnly generated files can be downloaded, uploaded files can't be downloaded. You can tell which files are downloadable by checking the `source` or `download_uri` property.r   	:downloadaltmediarK   r[   r[   r   r   )r   rW   rX   r   r   r   DownloadFileConfigr   download_urir+   r,   r   r   download_fileVideovideo_bytesGeneratedVideovideo)
rh   r   r   r9   r   r(   rl   rm   r[   datar   r   r   r     s@   2

authc                C   s   t |tjjjstd|du rt }ntj|}|j	dd}|j
p't }|jp,i }dd | D }t| j|}d| |d< |jrL|j|d	< ||_||_
| j||d
S )*Registers gcs files with the file service.:auth must be a google.auth.credentials.Credentials object.NTdeepc                 S      i | ]	\}}|  |qS r   lowerr@   kvr   r   r   
<dictcomp>      z(Files.register_files.<locals>.<dictcomp>Bearer authorizationx-goog-user-projectr   )r   googler   credentialsCredentialsrX   r   RegisterFilesConfigmodel_validate
model_copyr[   HttpOptionsrU   itemsr   get_token_from_credentialsquota_project_idr   rh   r   r1   r9   r[   rU   tokenr   r   r   register_files  s$   


zFiles.register_filesc                C   s   | j }td|| j |d|S )a  Lists all files from the service.

    Args:
      config (ListFilesConfig): Optional, configuration for the list method.

    Returns:
      A Pager object that contains one page of files. When iterating over
      the pager, it automatically fetches the next page if there are more.

    Usage:

    .. code-block:: python

      for file in client.files.list(config={'page_size': 10}):
        print(file.name)
    r=   rH   )rp   r   rh   r9   list_requestr   r   r   list   s   
z
Files.list)+__name__
__module____qualname__r   r   ListFilesConfigOrDictrb   rp   
FileOrDictCreateFileConfigOrDictrx   rz   strGetFileConfigOrDictr   rL   DeleteFileConfigOrDictr   r   builtinsr   RegisterFilesConfigOrDictr   r   r   r   PathLiker   r   UploadFileConfigOrDictr   r   r   r   DownloadFileConfigOrDictbytesr   r   r   r   r   r   r   r   r   r   r   rG      s    
H
P
R
[
S
M	
a
"rG   c                
   @   rF   )
AsyncFilesNrH   r9   r   c             	      sN  t j|d}| jjrtdt|}|d}|r d|}nd}|d}|r2| dt| }|	dd  d }|j
d urI|j
jd urI|j
j}t|}t|}| jd|||I d H }|jsdi nt|j}	| jjsrt|	}	t jj|	t|dd rdt|j
d	d t|j
d
d t|j
dd dini d}
t j|jd|
_| j|
 |
S rI   )r   rV   r   rW   rX   r:   rL   rY   r   rZ   r9   r[   r
   r\   r]   async_requestr_   r`   ra   rD   rb   rc   rd   re   rU   r#   rf   rg   r   r   r   rp   ?  sh   






zAsyncFiles._listr   c             	      p  t j||d}| jjrtdt|}|d}|r!d|}nd}|d}|r3| dt| }|	dd  d }|j
d urJ|j
jd urJ|j
j}t|}t|}| jd|||I d H }	|d urxt|d	d rxt j|	d
}
| j|
 |
S |	js}i nt|	j}| jjst|}t jj|t|dd rdt|j
dd t|j
dd t|j
dd dini d}
| j|
 |
S rr   )r   rw   r   rW   rX   r   rL   rY   r   rZ   r9   r[   r
   r\   r]   r   rd   rx   rf   r_   r`   ra   r$   rc   ry   r   r   r   rz     p   







zAsyncFiles._creater(   c             	      s0  t j||d}| jjrtdt|}|d}|r!d|}nd}|d}|r3| dt| }|	dd d}|j
durJ|j
jdurJ|j
j}t|}t|}| jd	|||I dH }	|	jsei nt|	j}
t jj|
t|ddrdt|j
d
dt|j
ddt|j
dddini d}| j| |S )as  Retrieves the file information from the service.

    Args:
      name (str): The name identifier for the file to retrieve.
      config (GetFileConfig): Optional, configuration for the get method.

    Returns:
      File: The file information.

    Usage:

    .. code-block:: python

      file = await client.aio.files.get(name='files/...')
      print(file.uri)
    r|   rJ   r)   r}   r4   rK   r9   NrL   rM   rN   rO   rP   rQ   )r   r~   r   rW   rX   r0   rL   rY   r   rZ   r9   r[   r
   r\   r]   r   r_   r`   ra   r   rc   rd   rf   r   r   r   r   rL     s`   






zAsyncFiles.getc             	      sP  t j||d}| jjrtdt|}|d}|r!d|}nd}|d}|r3| dt| }|	dd d}|j
durJ|j
jdurJ|j
j}t|}t|}| jd	|||I dH }	|	jsei nt|	j}
| jjsst|
}
t jj|
t|ddrdt|j
d
dt|j
ddt|j
dddini d}t j|	jd|_| j| |S )ag  Deletes a remotely stored file.

    Args:
      name (str): The name identifier for the file to delete.
      config (DeleteFileConfig): Optional, configuration for the delete method.

    Returns:
      DeleteFileResponse: The response for the delete method

    Usage:

    .. code-block:: python

      await client.aio.files.delete(name='files/...')
    r|   rJ   r)   r}   r4   rK   r9   Nr   rM   rN   rO   rP   rQ   rT   )r   r   r   rW   rX   r-   rL   rY   r   rZ   r9   r[   r
   r\   r]   r   r_   r`   ra   r/   r   rc   rd   re   rU   r#   rf   r   r   r   r   r   '  sj   






zAsyncFiles.deleter1   c             	      r   r   )r   r   r   rW   rX   r2   rL   rY   r   rZ   r9   r[   r
   r\   r]   r   rd   r   rf   r_   r`   ra   rE   rc   r   r   r   r   r   ~  r   zAsyncFiles._register_filesc                   s  | j jr	tdt }|r+t|trtjdi |}n|}tj|j|j	|j
d}nt }|j	durA|j	dsAd|j	 |_	tj||j|jd\}}}||_||_| j|tj|dddI dH }|jdu sz|jjdu szd	|jjvr~d
|jjvr~tdd	|jjv r|jjd	 }	n|jjd
 }	t|tjr| j j||	|j|dI dH }
nt|}| j j||	|j|dI dH }
tjj|
jd |r| dS i dS )a  Calls the API to upload a file asynchronously using a supported file service.

    Args:
      file: A path to the file or an `IOBase` object to be uploaded. If it's an
        IOBase object, it must be opened in blocking (the default) mode and
        binary mode. In other words, do not use non-blocking mode or text mode.
        The given stream must be seekable, that is, it must be able to call
        `seek()` on 'path'.
      config: Optional parameters to set `display_name`, `mime_type`, and
        `name`.
    r   r   Nr   r   Tr   rs   r   zX-Goog-Upload-URLr   r   r   rQ   r   )r   rW   rX   r   r   r   r   r   r   r(   r   r   r   r   r[   r   rz   r   r#   rU   r   r   r   async_upload_filer   r   rc   r`   r   r   r   r   r   r     sn   





zAsyncFiles.uploadr   r   c                      d S r   r   r   r   r   r   r        zAsyncFiles.downloadc                   r   r   r   r   r   r   r   r   )  r   c          
         s  | j jr	tdd}|rt|trtjdi |}n|}t|tjr,|jdu r,tdt	
|}d| d}d}t|dgdurGt|dg}ddi}|rV| d	t| }| j j|||d
I dH }	|du rt|tjrq|	|_|	S t|tjr|jdur|	|j_|	S )a  Downloads a file's data from the file service asynchronously.

    The Gemini Enterprise Agent Platform implementation of the API does not
    include the file service.

    Files created by `upload` can't be downloaded. You can tell which files are
    downloadable by checking the `download_uri` property.

    Note: When destination is None, this method returns the data as bytes. For
    `Video` and `GeneratedVideo` objects, if destination is None, it also sets
    the
    `video_bytes` property on the `Video` object. When destination is provided,
    chunks are streamed directly to disk or the writable stream without loading
    the full file into memory, and None is returned.

    Args:
      file: A file name, uri, or file object identifying which file to download.
      destination: Optional, a local file path or writable binary stream to
        write the file directly to in chunks, avoiding holding the full file in
        memory.
      config: Optional, configuration for the get method.

    Returns:
      bytes: The file data as bytes if destination is None; otherwise None.

    Usage:

    .. code-block:: python

      async for file in await client.aio.files.list():
        if file.download_uri is not None:
          break
      else:
        raise ValueError('No files found with a `download_uri`.')

      # Download to memory:
      data = await client.aio.files.download(file=file)

      # Stream directly to a file path (avoids memory spike):
      await client.aio.files.download(file=file, destination="output.mp4")
    r   Nr   r   r   r[   r   r   rK   r   r   )r   rW   rX   r   r   r   r   r   r   r+   r,   r   r   async_download_filer   r   r   r   )
rh   r   r   r9   r   r(   rl   r[   rm   r   r   r   r   r   3  sD   0

r   c                   s   t |tjjjstd|du rt }ntj|}|j	dd}|j
p(t }|jp-i }dd | D }t| j|I dH }d| |d< |jrP|j|d	< ||_||_
| j||d
I dH S )r   r   NTr   c                 S   r   r   r   r   r   r   r   r     r   z-AsyncFiles.register_files.<locals>.<dictcomp>r   r   r   r   )r   r   r   r   r   rX   r   r   r   r   r[   r   rU   r   r    async_get_token_from_credentialsr   r   r   r   r   r   r     s*   



zAsyncFiles.register_filesc                   s$   | j }td|| j |dI dH |S )a  Lists all files from the service asynchronously.

    Args:
      config (ListFilesConfig): Optional, configuration for the list method.

    Returns:
      A Pager object that contains one page of files. When iterating over
      the pager, it automatically fetches the next page if there are more.

    Usage:

    .. code-block:: python

      async for file in await client.aio.files.list(config={'page_size': 10}):
        print(file.name)
    r=   rH   N)rp   r   r   r   r   r   r     s   zAsyncFiles.list)+r   r   r   r   r   r   rb   rp   r   r   rx   rz   r   r   r   rL   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   =  s    
J
P
T
[
S
R	
a
$r   r   )/r   r   r`   loggingr   typingr   r   r   r   urllib.parser   google.authr    r   r	   r
   r   r   r+   r   r   r   r   r   pagersr   r   	getLoggerloggerr   r   objectr   r$   r-   r/   r0   r2   r8   r:   rD   rE   
BaseModulerG   r   r   r   r   r   <module>   s   




















     