o
    kDi|                  	   @   s  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
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$G d!d" d"ej%Z&G d#d$ d$ej%Z'dS )&    N)AnyOptionalUnion)	urlencode   )_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   s.   i }t | dgd urt|dgt | dg |S )Nfilegetvsetvr   r   	to_object r   R/home/air/goalskill_t/back/venv/lib/python3.10/site-packages/google/genai/files.py_CreateFileParameters_to_mldev&   s   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_mldev1      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_mldev>      r)   c                 C   r   r   r   r   r   r   r   _DeleteFileResponse_from_mldevK   r!   r+   c                 C   r"   r#   r&   r   r   r   r   _GetFileParameters_to_mldevX   r*   r,   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_mldeve   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_mldevz   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   nextPageTokennext_page_tokenfilesc                 S   s   g | ]}|qS r   r   ).0itemr   r   r   
<listcomp>   s    z1_ListFilesResponse_from_mldev.<locals>.<listcomp>r   r   r   r   r   _ListFilesResponse_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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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%   r7   r.   ?r3   getresponsekwargsheaders)r   _ListFilesParameters_api_clientvertexai
ValueErrorr4   rA   
format_mapr   popr3   http_optionsr   convert_to_dictencode_unserializable_typesrequestbodyjsonloadsr;   ListFilesResponse_from_response
model_dumpHttpResponserF   r   _verify_responseselfr3   parameter_modelrequest_dictrequest_url_dictpathquery_paramsrM   rC   response_dictreturn_valuer   r   r   _list   sD   




zFiles._listr   c                C   s2  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   r?   r%   zupload/v1beta/filesr.   r@   r3   postshould_return_http_response)r   rB   )r   _CreateFileParametersrH   rI   rJ   r   rA   rK   r   rL   r3   rM   r   rN   rO   rP   getattrCreateFileResponserX   rQ   rR   rS   r    rU   rV   rZ   r   r3   r[   r\   r]   r^   r_   rM   rC   ra   r`   r   r   r   _create   sP   





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   r?   r%   files/{file}r.   r@   r3   NrA   rB   )r   _GetFileParametersrH   rI   rJ   r,   rA   rK   r   rL   r3   rM   r   rN   rO   rP   rQ   rR   rS   FilerU   rV   rX   rZ   r$   r3   r[   r\   r]   r^   r_   rM   rC   r`   ra   r   r   r   rA     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/...')
    rl   r?   r%   rm   r.   r@   r3   NdeleterB   rE   )r   _DeleteFileParametersrH   rI   rJ   r)   rA   rK   r   rL   r3   rM   r   rN   rO   rP   rQ   rR   rS   r+   DeleteFileResponserU   rV   rW   rF   r   rX   rp   r   r   r   rq   N  sJ   




zFiles.deletec                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`.
    r?   	mime_typer$   display_nameNfiles/user_http_optionsuser_mime_typeTrM   rf   rd   x-goog-upload-urlPFailed to create file. Upload URL did not returned from the create file request.rM   r   rB   r   )rH   rI   rJ   r   UploadFileConfig
isinstancedictro   ru   r$   rv   
startswithr	   prepare_resumable_uploadrM   
size_bytesrk   CreateFileConfigr   rF   KeyErrorioIOBaseupload_fileosfspathrU   rR   rV   rZ   r   r3   config_modelfile_objrM   r   ru   rC   
upload_urlreturn_file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
    r?   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.rw   	:downloadaltmediar@   rM   r~   r   )rH   rI   rJ   r   r   r   DownloadFileConfigro   download_urir'   r(   r   r   download_fileVideovideo_bytesGeneratedVideovideo)	rZ   r   r3   r   r$   r^   r_   rM   datar   r   r   download  s<   )

zFiles.downloadc                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)
    r7   r=   )rb   r   rZ   r3   list_requestr   r   r   list2  s   
z
Files.list)!__name__
__module____qualname__r   r   ListFilesConfigOrDictrT   rb   
FileOrDictCreateFileConfigOrDictri   rk   strGetFileConfigOrDictro   rA   DeleteFileConfigOrDictrs   rq   r   r   PathLiker   r   UploadFileConfigOrDictr   r   r   DownloadFileConfigOrDictbytesr   r   r   r   r   r   r   r<      sj    
8
@
B
K
P
R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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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   rG   rH   rI   rJ   r4   rA   rK   r   rL   r3   rM   r   rN   rO   async_requestrQ   rR   rS   r;   rT   rU   rV   rW   rF   r   rX   rY   r   r   r   rb   Q  sJ   





zAsyncFiles._listr   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 }	|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 rc   )r   rg   rH   rI   rJ   r   rA   rK   r   rL   r3   rM   r   rN   rO   r   rh   ri   rX   rQ   rR   rS   r    rU   rV   rj   r   r   r   rk     sR   






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)
    rl   r?   r%   rm   r.   r@   r3   NrA   rB   )r   rn   rH   rI   rJ   r,   rA   rK   r   rL   r3   rM   r   rN   rO   r   rQ   rR   rS   ro   rU   rV   rX   rp   r   r   r   rA     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/...')
    rl   r?   r%   rm   r.   r@   r3   Nrq   rB   rE   )r   rr   rH   rI   rJ   r)   rA   rK   r   rL   r3   rM   r   rN   rO   r   rQ   rR   rS   r+   rs   rU   rV   rW   rF   r   rX   rp   r   r   r   rq   	  sL   





zAsyncFiles.deletec                   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`.
    r?   rt   Nrw   rx   Tr{   rd   r|   zX-Goog-Upload-URLr}   r~   r   rB   r   )rH   rI   rJ   r   r   r   r   ro   ru   r$   rv   r   r	   r   rM   r   rk   r   r   rF   r   r   r   async_upload_filer   r   rU   rR   rV   r   r   r   r   r   P  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)
    r?   Nrw   r   rM   r   r   r@   r~   r   )rH   rI   rJ   r   r   r   r   r'   r(   r   r   async_download_file)	rZ   r   r3   r   r$   r^   rM   r_   r   r   r   r   r     s.   #

zAsyncFiles.downloadc                   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)
    r7   r=   N)rb   r   r   r   r   r   r     s   zAsyncFiles.list)r   r   r   r   r   r   rT   rb   r   r   ri   rk   r   r   ro   rA   r   rs   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   O  sj    
:
@
D
K
U
Cr   )N)(r   rR   loggingr   typingr   r   r   urllib.parser    r   r   r	   r
   r'   r   r   r   r   r   pagersr   r   	getLoggerloggerr   r   objectr   r    r)   r+   r,   r2   r4   r;   
BaseModuler<   r   r   r   r   r   <module>   s   
















   :