o
    i?{                  	   @   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   X/var/www/html/karishye-ai-python/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j de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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 )aa  Lists all files from the service.

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

    Returns:
      ListFilesResponse: The response for the list method.

    Usage:

    .. code-block:: python

      pager = client.files.list(config={'page_size': 10})
      for file in pager.page:
        print(file.name)
    r=   =This method is only supported in the Gemini Developer client.r%   r7   r.   ?r3   Ngetresponsekwargsheaders)r   _ListFilesParameters_api_clientvertexai
ValueErrorr4   r@   
format_mapr   popr3   http_optionsr   convert_to_dictencode_unserializable_typesrequestbodyjsonloadsr;   ListFilesResponse_from_response
model_dumpHttpResponserE   r   _verify_responseselfr3   parameter_modelrequest_dictrequest_url_dictpathquery_paramsrL   rB   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   rA   )r   _CreateFileParametersrG   rH   rI   r   r@   rJ   r   rK   r3   rL   r   rM   rN   rO   getattrCreateFileResponserW   rP   rQ   rR   r    rT   rU   rY   r   r3   rZ   r[   r\   r]   r^   rL   rB   r`   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   Nr@   rA   )r   _GetFileParametersrG   rH   rI   r,   r@   rJ   r   rK   r3   rL   r   rM   rN   rO   rP   rQ   rR   FilerT   rU   rW   rY   r$   r3   rZ   r[   r\   r]   r^   rL   rB   r_   r`   r   r   r   r@     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/...')
    rk   r>   r%   rl   r.   r?   r3   NdeleterA   rD   )r   _DeleteFileParametersrG   rH   rI   r)   r@   rJ   r   rK   r3   rL   r   rM   rN   rO   rP   rQ   rR   r+   DeleteFileResponserT   rU   rV   rE   r   rW   ro   r   r   r   rp   _  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rL   re   rc   x-goog-upload-urlPFailed to create file. Upload URL did not returned from the create file request.rL   r   rA   r   )rG   rH   rI   r   UploadFileConfig
isinstancedictrn   rt   r$   ru   
startswithr	   prepare_resumable_uploadrL   
size_bytesrj   CreateFileConfigr   rE   KeyErrorioIOBaseupload_fileosfspathrT   rQ   rU   rY   r   r3   config_modelfile_objrL   r   rt   rB   
upload_urlreturn_filefs_pathr   r   r   upload  sf   





zFiles.uploadc                C   s   t d| j| j|d|S Nr7   r=   )r   ra   rY   r3   r   r   r   list  s   
z
Files.listc          	      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.rv   	:downloadaltmediar?   rL   r}   r   )rG   rH   rI   r   r   r   DownloadFileConfigrn   download_urir'   r(   r   r   download_fileVideovideo_bytesGeneratedVideovideo)	rY   r   r3   r   r$   r]   r^   rL   datar   r   r   download  s<   )

zFiles.download)!__name__
__module____qualname__r   r   ListFilesConfigOrDictrS   ra   
FileOrDictCreateFileConfigOrDictrh   rj   strGetFileConfigOrDictrn   r@   DeleteFileConfigOrDictrr   rp   r   r   PathLiker   r   UploadFileConfigOrDictr   r   r   r   r   DownloadFileConfigOrDictbytesr   r   r   r   r   r<      sj    
I
@
B
K
M
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j de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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 )ak  Lists all files from the service.

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

    Returns:
      ListFilesResponse: The response for the list method.

    Usage:

    .. code-block:: python

      pager = await client.aio.files.list(config={'page_size': 10})
      for file in pager.page:
        print(file.name)
    r=   r>   r%   r7   r.   r?   r3   Nr@   rA   rD   )r   rF   rG   rH   rI   r4   r@   rJ   r   rK   r3   rL   r   rM   rN   async_requestrP   rQ   rR   r;   rS   rT   rU   rV   rE   r   rW   rX   r   r   r   ra   P  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 rb   )r   rf   rG   rH   rI   r   r@   rJ   r   rK   r3   rL   r   rM   rN   r   rg   rh   rW   rP   rQ   rR   r    rT   rU   ri   r   r   r   rj     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)
    rk   r>   r%   rl   r.   r?   r3   Nr@   rA   )r   rm   rG   rH   rI   r,   r@   rJ   r   rK   r3   rL   r   rM   rN   r   rP   rQ   rR   rn   rT   rU   rW   ro   r   r   r   r@     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/...')
    rk   r>   r%   rl   r.   r?   r3   Nrp   rA   rD   )r   rq   rG   rH   rI   r)   r@   rJ   r   rK   r3   rL   r   rM   rN   r   rP   rQ   rR   r+   rr   rT   rU   rV   rE   r   rW   ro   r   r   r   rp     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>   rs   Nrv   rw   Trz   rc   r{   zX-Goog-Upload-URLr|   r}   r   rA   r   )rG   rH   rI   r   r~   r   r   rn   rt   r$   ru   r   r	   r   rL   r   rj   r   r   rE   r   r   r   async_upload_filer   r   rT   rQ   rU   r   r   r   r   r   `  sn   





zAsyncFiles.uploadc                   s    t d| j| j|dI d H |S r   )r   ra   r   r   r   r   r     s   zAsyncFiles.listc          	         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>   Nrv   r   rL   r   r   r?   r}   r   )rG   rH   rI   r   r   r   r   r'   r(   r   r   async_download_file)	rY   r   r3   r   r$   r]   rL   r^   r   r   r   r   r     s.   #

zAsyncFiles.download)r   r   r   r   r   r   rS   ra   r   r   rh   rj   r   r   rn   r@   r   rr   rp   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   N  sj    
K
@
D
K
R
r   )N)(r   rQ   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   
















   9