a
    bg                     @   s   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 edd	d
dG dd deZdS )    N)CallableListOptional)
deprecated)Document)
BaseLoaderUnstructuredFileLoader)get_client_infoz0.0.32z1.0z(langchain_google_community.GCSFileLoader)ZsinceZremovalZalternative_importc                   @   sF   e Zd ZdZd	eeeeeegef  dddZe	e
 dddZdS )
GCSFileLoaderzLoad from GCS file.N)project_namebucketblobloader_funcc                 C   s4   || _ || _|| _ttddd}|r*|n|| _dS )a9  Initialize with bucket and key name.

        Args:
            project_name: The name of the project to load
            bucket: The name of the GCS bucket.
            blob: The name of the GCS blob to load.
            loader_func: A loader function that instantiates a loader based on a
                file_path argument. If nothing is provided, the
                UnstructuredFileLoader is used.

        Examples:
            To use an alternative PDF loader:
            >> from from langchain_community.document_loaders import PyPDFLoader
            >> loader = GCSFileLoader(..., loader_func=PyPDFLoader)

            To use UnstructuredFileLoader with additional arguments:
            >> loader = GCSFileLoader(...,
            >>      loader_func=lambda x: UnstructuredFileLoader(x, mode="elements"))

        )	file_pathreturnc                 S   s   t | S )Nr   )r    r   {/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/document_loaders/gcs_file.pydefault_loader_func4   s    z3GCSFileLoader.__init__.<locals>.default_loader_funcN)r   r   r   strr   _loader_func)selfr   r   r   r   r   r   r   r   __init__   s
    zGCSFileLoader.__init__)r   c                 C   s  zddl m} W n ty*   tdY n0 |j| jtdd}|| j}|| j}|	| jj
}t }| d| j }tjtj|dd || | |}| }	|	D ]8}
d	|
j
v rd
| j d| j |
j
d	< |r|
j
| q|	W  d   S 1 s
0    Y  dS )zLoad documents.r   )storagezpCould not import google-cloud-storage python package. Please install it with `pip install google-cloud-storage`.zgoogle-cloud-storage)Zclient_info/T)exist_oksourcezgs://N)Zgoogle.cloudr   ImportErrorZClientr   r
   Z
get_bucketr   r   Zget_blobmetadatatempfileTemporaryDirectoryosmakedirspathdirnameZdownload_to_filenamer   loadupdate)r   r   Zstorage_clientr   r   r   temp_dirr   loaderdocsdocr   r   r   r%   9   s0    





zGCSFileLoader.load)N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r%   r   r   r   r   r      s    $r   )r!   r   typingr   r   r   Zlangchain_core._api.deprecationr   Zlangchain_core.documentsr   Z)langchain_community.document_loaders.baser   Z1langchain_community.document_loaders.unstructuredr	   Z&langchain_community.utilities.vertexair
   r   r   r   r   r   <module>   s   