o
    Zh                     @   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                   @   sN   e Zd ZdZ	ddedededeeegef  fddZd	e	e
 fd
dZdS )GCSFileLoaderzLoad from GCS file.Nproject_namebucketblobloader_funcc                 C   s<   || _ || _|| _dtdtfdd}|r|| _dS || _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   d/var/www/html/lang_env/lib/python3.10/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w |j| jtdd}|| j}|| j}|	| jj
}t J}| d| j }tjtj|dd || | |}| }	|	D ]}
d	|
j
v rmd
| j d| j |
j
d	< |ru|
j
| qY|	W  d   S 1 sw   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   s6   




$zGCSFileLoader.loadr   )__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    