o
    %g                  	   @   s  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 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deeef dee defddZ	d.dedeeef dee defddZ	d.dedeeef dee defddZ 	d.dedeeef dee defddZ!	d.dedeeef dee defddZ"	d.dedeeef dee defddZ#	d.dedeeef dee defddZ$	d.dedeeef dee defd d!Z%	d.dedeeef dee defd"d#Z&	d.dedeeef dee defd$d%Z'	d.dedeeef dee defd&d'Z(	d.dedeeef dee de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)_transformers)types)BaseApiClient)get_value_by_path)set_value_by_path)
AsyncPagerPagerzgoogle_genai.files
api_clientfrom_objectparent_objectreturnc                 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	pageTokengetvsetvr   r   r   	to_object r   T/mnt/skqttb/ctump_chatbot/chatbot/lib/python3.10/site-packages/google/genai/files.py_ListFilesConfig_to_mldev%   s   
r    c                 C   s6   i }t |dgd urt|dgt| t |dg| |S )Nconfig)r   r   r    r   r   r   r   _ListFilesParameters_to_mldev;   s   r"   c                 C   z   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t |dg |S Ndetailsmessagecoder   r   r   r   r   _FileStatus_to_mldevM      r(   c                 C   &  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t |dg t |dgd urNt|dgt |dg t |dgd urat|d	gt |dg t |d
gd urtt|dgt |d
g t |dgd urt|dgt |dg t |dgd urt|dgt |dg t |dgd urt|dgt |dg t |dgd urt|dgt |dg t |dgd urt|dgt |dg t |dgd urt|dgt |dg t |dgd urt|dgt |dg t |dgd urt|dgt| t |dg| |S )Nnamedisplay_namedisplayName	mime_typemimeType
size_bytes	sizeBytescreate_time
createTimeexpiration_timeexpirationTimeupdate_time
updateTimesha256_hash
sha256Hashuridownload_uridownloadUristatesourcevideo_metadatavideoMetadataerror)r   r   r(   r   r   r   r   _File_to_mldev_   H   rB   c                 C   s\   i }t |dgd urt|dgt| t |dg| t |dgd ur,t|dgt |dg |S )Nfiler!   )r   r   rB   r   r   r   r   _CreateFileParameters_to_mldev   s   rE   c              	   C   ^   i }t |dgd urt|ddgt| t |dg t |dgd ur-t|dgt |dg |S Nr+   _urlrD   r!   r   r   tt_file_namer   r   r   r   _GetFileParameters_to_mldev      rL   c              	   C   rF   rG   rI   r   r   r   r   _DeleteFileParameters_to_mldev   rM   rN   c                 C   r#   r$   r   r   r   r   r   _FileStatus_from_mldev   r)   rO   c                 C   r*   )Nr+   r-   r,   r/   r.   r1   r0   r3   r2   r5   r4   r7   r6   r9   r8   r:   r<   r;   r=   r>   r@   r?   rA   )r   r   rO   r   r   r   r   _File_from_mldev   rC   rP   c                    sd   i t |dgd urtdgt |dg t |dgd ur0tdg fddt |dgD  S )NnextPageTokennext_page_tokenfilesc                    s   g | ]}t  |qS r   )rP   ).0itemr   r   r   r   
<listcomp>)  s    
z1_ListFilesResponse_from_mldev.<locals>.<listcomp>r   )r   r   r   r   rV   r   _ListFilesResponse_from_mldev  s   
	rX   c                 C   s.   i }t |dgd urt|dgt |dg |S )NhttpHeadershttp_headersr   r   r   r   r   _CreateFileResponse_from_mldev2  s   r[   c                 C   s   i }|S Nr   r   r   r   r   _DeleteFileResponse_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j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r!   r!   r   c          
      C   s   t j|d}| jjrtdt| j|}|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	|||}| jjsgt| j|}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.rH   rS   r   ?r!   Ngetresponsekwargs)r
   _ListFilesParameters_api_clientvertexai
ValueErrorr"   rb   
format_mapr   popr!   http_optionsr   convert_to_dictencode_unserializable_typesrequestrX   ListFilesResponse_from_response
model_dump_verify_response
selfr!   parameter_modelrequest_dictrequest_url_dictpathquery_paramsrl   response_dictreturn_valuer   r   r   _listJ  sH   




zFiles._listrD   c                C   s   t j||d}| jjrtdt| j|}|d}|r"d|}nd}|d}|r4| dt| }|	dd  d }|j
d urK|j
jd urK|j
j}t|}t|}| jd|||}	| jjsht| j|	}	t jj|	| d	}
| j|
 |
S 
NrD   r!   r`   rH   zupload/v1beta/filesr   ra   r!   postrc   )r
   _CreateFileParametersrg   rh   ri   rE   rb   rj   r   rk   r!   rl   r   rm   rn   ro   r[   CreateFileResponserq   rr   rs   ru   rD   r!   rv   rw   rx   ry   rz   rl   r{   r|   r   r   r   _create  sJ   




zFiles._creater+   c                C      t j||d}| jjrtdt| j|}|d}|r"d|}nd}|d}|r4| dt| }|	dd d}|j
durK|j
jdurK|j
j}t|}t|}| jd	|||}	| jjsht| 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+   r!   r`   rH   files/{file}r   ra   r!   Nrb   rc   )r
   _GetFileParametersrg   rh   ri   rL   rb   rj   r   rk   r!   rl   r   rm   rn   ro   rP   Filerq   rr   rs   ru   r+   r!   rv   rw   rx   ry   rz   rl   r{   r|   r   r   r   rb     sF   




z	Files.getc                C   r   )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   r`   rH   r   r   ra   r!   Ndeleterc   )r
   _DeleteFileParametersrg   rh   ri   rN   rb   rj   r   rk   r!   rl   r   rm   rn   ro   r]   DeleteFileResponserq   rr   rs   r   r   r   r   r     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|tjrw|jdu rOtdt|dr]d|jvr]td| }|d	tj | | |_||tj n1t|}|rtj|st| d
tj||_|jdu rt|\|_}|jdu rtd|r|jr|j}ntjdddd|j |j dd}| j|tj |dd}	|	j!du sd|	j!vrt"d|	j!d }
t|tjr| j #||
|j}n	| j #||
|j}tjj$t%| 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`   r.   r+   r,   Nfiles/zfUnknown mime type: Could not determine the mimetype for your file
 please set the `mime_type` argumentmodeb'The file must be opened in binary mode.r    is not a valid file path.iUnknown mime type: Could not determine the mimetype for your file
    please set the `mime_type` argument application/json	resumablestartzContent-TypezX-Goog-Upload-ProtocolzX-Goog-Upload-Commandz#X-Goog-Upload-Header-Content-Lengthz!X-Goog-Upload-Header-Content-Typeapi_versionheadersrl   r   x-goog-upload-urlPFailed to create file. Upload URL did not returned from the create file request.rD   rc   r   )(rg   rh   ri   r
   UploadFileConfig
isinstancedictr   r.   r+   r,   
startswithioIOBasehasattrr   tellseekosSEEK_ENDr0   SEEK_SETfspathry   isfileFileNotFoundErrorgetsize	mimetypes
guess_typerl   HttpOptionsr   CreateFileConfigrZ   KeyErrorupload_filerq   rP   jsonrr   ru   rD   r!   config_modelfile_objoffsetfs_path_rl   rd   
upload_urlreturn_filer   r   r   uploadT  s   











zFiles.uploadc                C   s   t d| j| j|d|S NrS   r_   )r   r}   ru   r!   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	
| j |}d| d}ddi}| dt| }d}t|d	gdurUt|d	g}| j ||}t|tjrg||_|S t|tjrv|jdurv||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.r   	:downloadaltmediara   rl   r   )rg   rh   ri   r   r   r
   DownloadFileConfigr   r;   rJ   rK   r   r   download_fileVideovideo_bytesGeneratedVideovideo)	ru   rD   r!   r   r+   ry   rz   rl   datar   r   r   download  s<   )
zFiles.download)#__name__
__module____qualname__r   r
   ListFilesConfigOrDictrp   r}   
FileOrDictCreateFileConfigOrDictr   r   strGetFileConfigOrDictr   rb   DeleteFileConfigOrDictr   r   r   pathlibPathr   PathLiker   r   UploadFileConfigOrDictr   r   r   r   r   DownloadFileConfigOrDictbytesr   r   r   r   r   r^   H  sj    
J
:
F
J
i
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j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_   r!   r   c          
         s   t j|d}| jjrtdt| j|}|d}|r"d|}nd}|d}|r4| dt| }|	dd d}|j
durK|j
jdurK|j
j}t|}t|}| jd	|||I dH }| jjskt| j|}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`   rH   rS   r   ra   r!   Nrb   rc   )r
   rf   rg   rh   ri   r"   rb   rj   r   rk   r!   rl   r   rm   rn   async_requestrX   rp   rq   rr   rs   rt   r   r   r   r}     sJ   





zAsyncFiles._listrD   c                   s   t j||d}| jjrtdt| j|}|d}|r#d|}nd}|d}|r5| dt| }|	dd  d }|j
d urL|j
jd urL|j
j}t|}t|}| jd|||I d H }	| jjslt| j|	}	t jj|	| d	}
| j|
 |
S r~   )r
   r   rg   rh   ri   rE   rb   rj   r   rk   r!   rl   r   rm   rn   r   r[   r   rq   rr   rs   r   r   r   r   r   `  sL   





zAsyncFiles._creater+   c                      t j||d}| jjrtdt| j|}|d}|r#d|}nd}|d}|r5| dt| }|	dd d}|j
durL|j
jdurL|j
j}t|}t|}| jd	|||I dH }	| jjslt| 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)
    r   r`   rH   r   r   ra   r!   Nrb   rc   )r
   r   rg   rh   ri   rL   rb   rj   r   rk   r!   rl   r   rm   rn   r   rP   r   rq   rr   rs   r   r   r   r   rb     sH   





zAsyncFiles.getc                   r   )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   r`   rH   r   r   ra   r!   Nr   rc   )r
   r   rg   rh   ri   rN   rb   rj   r   rk   r!   rl   r   rm   rn   r   r]   r   rq   rr   rs   r   r   r   r   r     sL   





zAsyncFiles.deletec                   s6  | 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|tjrx|jdu rPtdt|dr^d|jvr^td| }|d	tj | | |_||tj n1t|}|rtj|st| d
tj||_|jdu rt|\|_}|jdu rtd|r|jr|j}ntjdddd|j |j dd}| j|tj |ddI dH }	|	j!du sd|	j!vrt"d|	j!d }
t|tjr| j #||
|jI dH }n| j #||
|jI dH }tjj$t%| 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`   r   Nr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rD   rc   r   )(rg   rh   ri   r
   r   r   r   r   r.   r+   r,   r   r   r   r   r   r   r   r   r   r0   r   r   ry   r   r   r   r   r   rl   r   r   r   rZ   r   async_upload_filerq   rP   r   rr   r   r   r   r   r   $  s   












zAsyncFiles.uploadc                   s    t d| j| j|dI d H |S r   )r   r}   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| j |}d| d}d}t	|dgdur:t	|dg}ddi}|rI| dt
| }| j ||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`   Nr   r   rl   r   r   ra   r   )rg   rh   ri   r   r   r
   r   rJ   rK   r   r   async_download_file)	ru   rD   r!   r   r+   ry   rl   rz   r   r   r   r   r     s.   #

zAsyncFiles.download)!r   r   r   r   r
   r   rp   r}   r   r   r   r   r   r   r   rb   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     sj    
J
:
F
J
h
r   r\   )-r   loggingr   r   r   typingr   r   r   urllib.parser   r   r   r   r	   rJ   r
   rg   r   r   r   r   r   pagersr   r   	getLoggerloggerr   objectr    r"   r(   rB   rE   rL   rN   rO   rP   rX   r[   r]   
BaseModuler^   r   r   r   r   r   <module>   s  








<









<






   S