o
    Zhܱ                  	   @   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	dKdedeeef dee defddZ	dKdedeeef dee defddZ	dKdedeeef dee defddZ 	dKdedeeef dee defddZ!	dKdedeeef dee defddZ"	dKdedeeef dee defddZ#	dKdedeeef dee defddZ$	dKdedeeef dee defd d!Z%	dKdedeeef dee defd"d#Z&	dKdedeeef dee defd$d%Z'	dKdedeeef dee defd&d'Z(	dKdedeeef dee defd(d)Z)	dKdedeeef dee defd*d+Z*	dKdedeeef dee defd,d-Z+d.efd/d0Z,d.efd1d2Z-	dKdedeeef dee defd3d4Z.	dKdedeeef dee defd5d6Z/	dKdedeeef dee defd7d8Z0	dKdedeeef dee defd9d:Z1	dKdedeeef dee defd;d<Z2	dKdedeeef dee defd=d>Z3	dKdedeeef dee defd?d@Z4	dKdedeeef dee defdAdBZ5	dKdedeeef dee defdCdDZ6	dKdedeeef dee defdEdFZ7G dGdH dHej8Z9G dIdJ dJej8Z:dS )L    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   X   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	pageTokengetvsetvr   r   r   	to_object r   I/var/www/html/lang_env/lib/python3.10/site-packages/google/genai/files.py_ListFilesConfig_to_mldev%      
r   c                 C   r   r   r   r   r   r   r   _ListFilesConfig_to_vertex;   r   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_mldevQ   s   r"   c                 C   s    i }t |dgd urtd|S )Nr!   /config parameter is not supported in Vertex AI.r   
ValueErrorr   r   r   r   _ListFilesParameters_to_vertexc   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_mldevo      r,   c                 C   sP   i }t |dgd urtdt |dgd urtdt |dgd ur&td|S )Nr)   z0details parameter is not supported in Vertex AI.r*   z0message parameter is not supported in Vertex AI.r+   z-code parameter is not supported in Vertex AI.r$   r   r   r   r   _FileStatus_to_vertex   s   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   rG   c                 C   sX  i }t |dgd urtdt |dgd urtdt |dgd ur&tdt |dgd ur2tdt |d	gd ur>td
t |dgd urJtdt |dgd urVtdt |dgd urbtdt |dgd urntdt |dgd urztdt |dgd urtdt |dgd urtdt |dgd urtdt |dgd urtd|S )Nr0   -name parameter is not supported in Vertex AI.r1   z5display_name parameter is not supported in Vertex AI.r3   z2mime_type parameter is not supported in Vertex AI.r5   z3size_bytes parameter is not supported in Vertex AI.r7   z4create_time parameter is not supported in Vertex AI.r9   z8expiration_time parameter is not supported in Vertex AI.r;   z4update_time parameter is not supported in Vertex AI.r=   z4sha256_hash parameter is not supported in Vertex AI.r?   z,uri parameter is not supported in Vertex AI.r@   z5download_uri parameter is not supported in Vertex AI.rB   z.state parameter is not supported in Vertex AI.rC   z/source parameter is not supported in Vertex AI.rD   z7video_metadata parameter is not supported in Vertex AI.rF   z.error parameter is not supported in Vertex AI.r$   r   r   r   r   _File_to_vertex   s<   rJ   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   rG   r   r   r   r   _CreateFileParameters_to_mldev   s   rL   c                 C   8   i }t |dgd urtdt |dgd urtd|S )NrK   z-file parameter is not supported in Vertex AI.r!   r#   r$   r   r   r   r   _CreateFileParameters_to_vertex     rN   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 Nr0   _urlrK   r!   r   r   tt_file_namer   r   r   r   _GetFileParameters_to_mldev!     rV   c                 C   rM   Nr0   rI   r!   r#   r$   r   r   r   r   _GetFileParameters_to_vertex4  rO   rY   c              	   C   rP   rQ   rS   r   r   r   r   _DeleteFileParameters_to_mldevC  rW   rZ   c                 C   rM   rX   r$   r   r   r   r   _DeleteFileParameters_to_vertexV  rO   r[   
enum_valuec                 C   "   | t g dv rt|  dd S )N)ZSTATE_UNSPECIFIED
PROCESSINGZACTIVEZFAILED* enum value is not supported in Vertex AI.setr%   r\   r   r   r   "_FileState_to_vertex_enum_validatee     rc   c                 C   r]   )N)ZSOURCE_UNSPECIFIEDZUPLOADEDZ	GENERATEDr_   r`   rb   r   r   r   #_FileSource_to_vertex_enum_validatej  rd   re   c                 C   r'   r(   r   r   r   r   r   _FileStatus_from_mldevo  r-   rf   c                 C      i }|S Nr   r   r   r   r   _FileStatus_from_vertex     ri   c                 C   r/   )Nr0   r2   r1   r4   r3   r6   r5   r8   r7   r:   r9   r<   r;   r>   r=   r?   rA   r@   rB   rC   rE   rD   rF   )r   r   rf   r   r   r   r   _File_from_mldev  rH   rk   c                 C   rg   rh   r   r   r   r   r   _File_from_vertex  rj   rl   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 )NZnextPageTokenZnext_page_tokenfilesc                    s   g | ]}t  |qS r   )rk   ).0itemr   r   r   r   
<listcomp>  s    
z1_ListFilesResponse_from_mldev.<locals>.<listcomp>r   )r   r   r   r   rp   r   _ListFilesResponse_from_mldev  s   
	rr   c                 C   rg   rh   r   r   r   r   r   _ListFilesResponse_from_vertex  rj   rs   c                 C   .   i }t |dgd urt|dgt |dg |S NZhttpHeadershttp_headersr   r   r   r   r   _CreateFileResponse_from_mldev     rw   c                 C   rt   ru   r   r   r   r   r   _CreateFileResponse_from_vertex  rx   ry   c                 C   rg   rh   r   r   r   r   r   _DeleteFileResponse_from_mldev  rj   rz   c                 C   rg   rh   r   r   r   r   r   _DeleteFileResponse_from_vertex  rj   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}t
|trG|d	d}nt|d	rS|durS|j}t|}t|}| jd
|||}| jjrqt| j|}nt| 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}   4This method is only supported in the default client.rR   rm   r   ?r!   Nhttp_optionsgetresponsekwargs)r
   _ListFilesParameters_api_clientvertexair%   r"   r   
format_mapr   pop
isinstancedicthasattrr   r   convert_to_dictencode_unserializable_typesrequestrs   rr   ListFilesResponse_from_response
model_dump_verify_response
selfr!   parameter_modelrequest_dictrequest_url_dictpathquery_paramsr   response_dictreturn_valuer   r   r   _list  sL   




zFiles._listrK   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 }t
|trH|dd }nt|drT|d urT|j}t|}t|}| jd	|||}	| jjrrt| j|	}	nt| j|	}	t jj|	| d
}
| j|
 |
S NrK   r!   r~   rR   zupload/v1beta/filesr   r   r!   r   postr   )r
   _CreateFileParametersr   r   r%   rL   r   r   r   r   r   r   r   r   r   r   r   r   ry   rw   CreateFileResponser   r   r   r   rK   r!   r   r   r   r   r   r   r   r   r   r   r   _createc  sN   




zFiles._creater0   c                C     t j||d}| jjrtdt| j|}|d}|r"d|}nd}|d}|r4| dt| }|	dd d}t
|trH|d	d}nt|d	rT|durT|j}t|}t|}| jd
|||}	| jjrrt| j|	}	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)
    r0   r!   r~   rR   files/{file}r   r   r!   Nr   r   r   )r
   _GetFileParametersr   r   r%   rV   r   r   r   r   r   r   r   r   r   r   r   r   rl   rk   Filer   r   r   r   r0   r!   r   r   r   r   r   r   r   r   r   r   r   r     sF   




z	Files.getc                C   r   )ah  Deletes an existing file from the service.

    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~   rR   r   r   r   r!   Nr   deleter   )r
   _DeleteFileParametersr   r   r%   rZ   r   r   r   r   r   r   r   r   r   r   r   r   r{   rz   DeleteFileResponser   r   r   r   r   r   r   r     sN   




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 |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`.
    UVertex AI does not support creating files. You can upload files to GCS files instead.r3   r0   r1   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r   r   x-goog-upload-urlPFailed to create file. Upload URL did not returned from the create file request.rK   r   r   )'r   r   r%   r
   UploadFileConfigr   r   r   r3   r0   r1   
startswithioIOBaser   r   tellseekosSEEK_ENDr5   SEEK_SETfspathr   isfileFileNotFoundErrorgetsize	mimetypes
guess_typer   HttpOptionsr   CreateFileConfigrv   KeyErrorZupload_filer   rk   r   r   rK   r!   config_modelZfile_objoffsetZfs_path_r   r   Z
upload_urlZreturn_filer   r   r   upload(  s   











zFiles.uploadc                C   s   t d| j| j|d|S Nrm   r}   )r   r   r   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
    @Vertex AI does not support the Files API. Use GCS files instead.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mediar   r   r   )r   r   r%   r   r   r
   DownloadFileConfigr   r@   rT   rU   r   r   Zdownload_fileVideoZvideo_bytesGeneratedVideoZvideo)	r   rK   r!   r   r0   r   r   r   datar   r   r   download  s<   )
zFiles.download)#__name__
__module____qualname__r   r
   ListFilesConfigOrDictr   r   
FileOrDictCreateFileConfigOrDictr   r   strGetFileConfigOrDictr   r   DeleteFileConfigOrDictr   r   r   pathlibPathr   PathLiker   r   UploadFileConfigOrDictr   r   r   r   r   DownloadFileConfigOrDictbytesr   r   r   r   r   r|     sj    
K
;
E
K
j
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}t
|trH|d	d}nt|d	rT|durT|j}t|}t|}| jd
|||I dH }| jjrut| j|}nt| 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~   rR   rm   r   r   r!   Nr   r   r   )r
   r   r   r   r%   r"   r   r   r   r   r   r   r   r   r   r   r   async_requestrs   rr   r   r   r   r   r   r   r   r   r     sN   





zAsyncFiles._listrK   c                   s  t j||d}| jjrtdt| j|}|d}|r#d|}nd}|d}|r5| dt| }|	dd  d }t
|trI|dd }nt|drU|d urU|j}t|}t|}| jd	|||I d H }	| jjrvt| j|	}	nt| j|	}	t jj|	| d
}
| j|
 |
S r   )r
   r   r   r   r%   rL   r   r   r   r   r   r   r   r   r   r   r   r   ry   rw   r   r   r   r   r   r   r   r   r   6  sP   





zAsyncFiles._creater0   c                     t j||d}| jjrtdt| j|}|d}|r#d|}nd}|d}|r5| dt| }|	dd d}t
|trI|d	d}nt|d	rU|durU|j}t|}t|}| jd
|||I dH }	| jjrvt| j|	}	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)
    r   r~   rR   r   r   r   r!   Nr   r   r   )r
   r   r   r   r%   rV   r   r   r   r   r   r   r   r   r   r   r   r   rl   rk   r   r   r   r   r   r   r   r   r   p  sH   





zAsyncFiles.getc                   r   )ar  Deletes an existing file from the service.

    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~   rR   r   r   r   r!   Nr   r   r   )r
   r   r   r   r%   rZ   r   r   r   r   r   r   r   r   r   r   r   r   r{   rz   r   r   r   r   r   r   r   r   r     sP   





zAsyncFiles.deletec                   s4  | 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 |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   rK   r   r   )'r   r   r%   r
   r   r   r   r   r3   r0   r1   r   r   r   r   r   r   r   r   r   r5   r   r   r   r   r   r   r   r   r   r   r   r   rv   r   Zasync_upload_filer   rk   r   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   c  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   r   r   r   r   r   )r   r   r%   r   r   r
   r   rT   rU   r   r   Zasync_download_file)	r   rK   r!   r   r0   r   r   r   r   r   r   r   r   m  s.   #

zAsyncFiles.download)!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   r   r   r   r   r   r     sj    
K
;
E
K
i
r   rh   );r   loggingr   r   r   typingr   r   r   urllib.parser   r   r   r   r	   rT   r
   r   r   r   r   r   r   Zpagersr   r   	getLoggerloggerr   objectr   r    r"   r&   r,   r.   rG   rJ   rL   rN   rV   rY   rZ   r[   rc   re   rf   ri   rk   rl   rr   rs   rw   ry   rz   r{   Z
BaseModuler|   r   r   r   r   r   <module>   s  














<

6

















<














   V