o
    Yi                  	   @   sV  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 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)	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   Z	to_object r   L/home/kim/smarthome/.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,   r.   c                 C   r   )Nurisr   r   r   r   r   )_InternalRegisterFilesParameters_to_mldevh   r   r0   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 )NZ	page_size_queryZpageSizeZ
page_tokenZ	pageTokenr   r   r   r   r   _ListFilesConfig_to_mldevs   s   
r2   c                 C   s*   i }t | dgd urtt | dg| |S )Nconfig)r   r2   r   r   r   r   _ListFilesParameters_to_mldev   s   r4   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!   ZnextPageTokenZ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    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dd t | dgD  |S )Nr    r!   r5   c                 S   r6   r   r   r7   r   r   r   r:      r;   z5_RegisterFilesResponse_from_mldev.<locals>.<listcomp>r   r   r   r   r   !_RegisterFilesResponse_from_mldev   s    r=   c                	   @   s  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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dddeeejejejf deej  de!fddZ"ddde#j$j%j&de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r3   r3   r   c                C   s  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|	| d	}
t j|jd
|
_| j|
 |
S Nr?   =This method is only supported in the Gemini Developer client.r'   r5   r1   ?r3   getresponsekwargsheaders)r   _ListFilesParametersr   vertexai
ValueErrorr4   rC   
format_mapr   popr3   http_optionsr	   convert_to_dictencode_unserializable_typesrequestbodyjsonloadsr<   ListFilesResponse_from_response
model_dumpHttpResponserH   r!   _verify_responseselfr3   parameter_modelrequest_dictrequest_url_dictpathquery_paramsrN   rE   response_dictreturn_valuer   r   r   _list   sD   




zFiles._listr   c                C   2  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|| d}
| j|
 |
S Nr   r3   rA   r'   zupload/v1beta/filesr1   rB   r3   postshould_return_http_responser!   rD   )r   _CreateFileParametersr   rJ   rK   r   rC   rL   r   rM   r3   rN   r	   rO   rP   rQ   getattrCreateFileResponserY   rR   rS   rT   r"   rV   rW   r[   r   r3   r\   r]   r^   r_   r`   rN   rE   rb   ra   r   r   r   _create   P   





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|
| 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&   r3   rA   r'   files/{file}r1   rB   r3   NrC   rD   )r   _GetFileParametersr   rJ   rK   r.   rC   rL   r   rM   r3   rN   r	   rO   rP   rQ   rR   rS   rT   FilerV   rW   rY   r[   r&   r3   r\   r]   r^   r_   r`   rN   rE   ra   rb   r   r   r   rC   +  s<   




z	Files.getc                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}
| jjsot|
}
t jj|
| 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/...')
    rp   rA   r'   rq   r1   rB   r3   NdeleterD   rG   )r   _DeleteFileParametersr   rJ   rK   r+   rC   rL   r   rM   r3   rN   r	   rO   rP   rQ   rR   rS   rT   r-   DeleteFileResponserV   rW   rX   rH   r!   rY   rt   r   r   r   ru   l  sJ   




zFiles.deleter/   c                C   rd   Nr/   r3   rA   r'   zfiles:registerr1   rB   r3   rg   rh   ri   rD   )r    _InternalRegisterFilesParametersr   rJ   rK   r0   rC   rL   r   rM   r3   rN   r	   rO   rP   rQ   rk   RegisterFilesResponserY   rR   rS   rT   r=   rV   rW   r[   r/   r3   r\   r]   r^   r_   r`   rN   rE   rb   ra   r   r   r   _register_files  ro   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 `diplay_name`, `mime_type`, and `name`.
    rA   	mime_typer&   display_nameNfiles/Zuser_http_optionsZuser_mime_typeTrN   rh   rf   x-goog-upload-urlPFailed to create file. Upload URL did not returned from the create file request.rN   r   rD   r   )r   rJ   rK   r   UploadFileConfig
isinstancedictrs   r   r&   r   
startswithr
   prepare_resumable_uploadrN   
size_bytesrn   CreateFileConfigr!   rH   KeyErrorioIOBaseZupload_fileosfspathrV   rS   rW   r[   r   r3   config_modelZfile_objrN   r   r   rE   Z
upload_urlZreturn_fileZfs_pathr   r   r   upload  sf   





zFiles.uploadc          	      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
}t|tjrf||_|S t|tjru|jduru||j_|S )ar  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: This method returns the data as bytes. For `Video` and
    `GeneratedVideo` objects there is an additional side effect, that it also
    sets the `video_bytes` property on the `Video` object.

    Args:
      file (str): A file name, uri, or file object. Identifying which file to
        download.
      config (DownloadFileConfigOrDict): Optional, configuration for the get
        method.

    Returns:
      File: The file data as bytes.

    Usage:

    .. code-block:: python

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

      video = types.Video(uri=file.uri)
      video_bytes = client.files.download(file=video)
      video.video_bytes
    rA   NzOnly 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mediarB   rN   r   r   )r   rJ   rK   r   r   r   DownloadFileConfigrs   Zdownload_urir)   r*   r   r   Zdownload_fileVideoZvideo_bytesGeneratedVideoZvideo)	r[   r   r3   r   r&   r_   r`   rN   datar   r   r   download>  s<   )

zFiles.download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r8   kvr   r   r   
<dictcomp>      z(Files.register_files.<locals>.<dictcomp>Bearer authorizationx-goog-user-projectry   )r   googler   credentialsCredentialsrK   r   RegisterFilesConfigmodel_validate
model_copyrN   HttpOptionsrH   itemsr   Zget_token_from_credentialsquota_project_idr}   r[   r   r/   r3   rN   rH   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)
    r5   r?   )rc   r   r[   r3   Zlist_requestr   r   r   list  s   
z
Files.list))__name__
__module____qualname__r   r   ListFilesConfigOrDictrU   rc   
FileOrDictCreateFileConfigOrDictrl   rn   strGetFileConfigOrDictrs   rC   DeleteFileConfigOrDictrw   ru   r   RegisterFilesConfigOrDictr{   r}   r   r   PathLiker   r   UploadFileConfigOrDictr   r   r   DownloadFileConfigOrDictbytesr   r   r   r   r   r   r   r   r   r   r   r>      s    
8
@
B
K
C
P
V
"r>   c                	   @   sz  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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dddeeejf deej defddZ ddde!j"j#j$de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 )
AsyncFilesNr?   r3   r   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|||I d H }|jsdi nt|j}	| jjsrt|	}	t jj|	| d	}
t j|jd
|
_| j|
 |
S r@   )r   rI   r   rJ   rK   r4   rC   rL   r   rM   r3   rN   r	   rO   rP   async_requestrR   rS   rT   r<   rU   rV   rW   rX   rH   r!   rY   rZ   r   r   r   rc     sJ   





zAsyncFiles._listr   c                   :  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|| d}
| j|
 |
S re   )r   rj   r   rJ   rK   r   rC   rL   r   rM   r3   rN   r	   rO   rP   r   rk   rl   rY   rR   rS   rT   r"   rV   rW   rm   r   r   r   rn     R   






zAsyncFiles._creater&   c                   s   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|
| 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)
    rp   rA   r'   rq   r1   rB   r3   NrC   rD   )r   rr   r   rJ   rK   r.   rC   rL   r   rM   r3   rN   r	   rO   rP   r   rR   rS   rT   rs   rV   rW   rY   rt   r   r   r   rC   D  sB   





zAsyncFiles.getc                   s  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|
| 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/...')
    rp   rA   r'   rq   r1   rB   r3   Nru   rD   rG   )r   rv   r   rJ   rK   r+   rC   rL   r   rM   r3   rN   r	   rO   rP   r   rR   rS   rT   r-   rw   rV   rW   rX   rH   r!   rY   rt   r   r   r   ru     sL   





zAsyncFiles.deleter/   c                   r   rx   )r   rz   r   rJ   rK   r0   rC   rL   r   rM   r3   rN   r	   rO   rP   r   rk   r{   rY   rR   rS   rT   r=   rV   rW   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 `diplay_name`, `mime_type`, and `name`.
    rA   r~   Nr   r   Tr   rf   r   zX-Goog-Upload-URLr   r   r   rD   r   )r   rJ   rK   r   r   r   r   rs   r   r&   r   r   r
   r   rN   r   rn   r   r!   rH   r   r   r   Zasync_upload_filer   r   rV   rS   rW   r   r   r   r   r     sn   





zAsyncFiles.uploadc          	         s   | j jr	tdd}|rt|trtjd
i |}n|}t|}d| d}d}t	|dgdur8t	|dg}ddi}|rG| dt
| }| j j||d	I dH }|S )at  Downloads a file's data from the file service.

    The Vertex-AI implementation of the API foes 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.

    Args:
      File (str): A file name, uri, or file object. Identifying which file to
        download.
      config (DownloadFileConfigOrDict): Optional, configuration for the get
        method.

    Returns:
      File: The file data as bytes.

    Usage:

    .. code-block:: python

      for file client.files.list():
        if file.download_uri is not None:
          break
      else:
        raise ValueError('No files found with a `download_uri`.')
      data = client.files.download(file=file)
      # data = client.files.download(file=file.name)
      # data = client.files.download(file=file.uri)
    rA   Nr   r   rN   r   r   rB   r   r   )r   rJ   rK   r   r   r   r   r)   r*   r   r   Zasync_download_file)	r[   r   r3   r   r&   r_   rN   r`   r   r   r   r   r   ^  s.   #

zAsyncFiles.downloadr   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   ry   )r   r   r   r   r   rK   r   r   r   r   rN   r   rH   r   r   Z 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)
    r5   r?   N)rc   r   r   r   r   r   r     s   zAsyncFiles.list)'r   r   r   r   r   r   rU   rc   r   r   rl   rn   r   r   rs   rC   r   rw   ru   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    
:
@
D
K
C
U
G
$r   )N)-r   rS   loggingr   typingr   r   r   urllib.parser   Zgoogle.authr    r   r   r	   r
   r   r)   r   r   r   r   r   Zpagersr   r   	getLoggerloggerr   r   objectr   r"   r+   r-   r.   r0   r2   r4   r<   r=   Z
BaseModuler>   r   r   r   r   r   <module>   s   




















    