
    3fi                     b    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  G d de      Zy)    N)CallableListOptional)
BaseLoader)Document)get_client_infoc                   P    e Zd ZdZ	 d
dedededeeegef      fdZde	e
   fd	Zy)GCSFileLoaderam  Load documents from Google Cloud Storage file.

    Inherits from [`BaseLoader`][langchain_core.document_loaders.BaseLoader].

    Downloads and loads a single file from GCS bucket using configurable loader.

    !!! note "Installation"

        Requires additional dependencies:

        ```bash
        pip install langchain-google-community[gcs]
        ```
    Nproject_namebucketblobloader_funcc                 p    || _         || _        || _        dt        dt        fd}|r|| _        y|| _        y)a  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.

        ??? example "Using Alternative PDF Loader"

            ```python
            from langchain_community.document_loaders import PyPDFLoader

            loader = GCSFileLoader(..., loader_func=PyPDFLoader)
            ```

        ??? example "Using UnstructuredFileLoader with Custom Arguments"

            ```python
            from langchain_community.document_loaders import UnstructuredFileLoader

            loader = GCSFileLoader(
                ..., loader_func=lambda x: UnstructuredFileLoader(x, mode="elements")
            )
            ```
        	file_pathreturnc                 X    	 ddl m}  |       S # t        $ r d}t        |       Y  w xY w)Nr   )UnstructuredFileLoaderzUnstructuredFileLoader loader not found! Either provide a custom loader with loader_func argument, or install `pip install langchain-google-community`)1langchain_community.document_loaders.unstructuredr   ImportErrorprint)r   r   messages      a/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_google_community/gcs_file.pydefault_loader_funcz3GCSFileLoader.__init__.<locals>.default_loader_funcB   s=    
 *)44  ? 
 gs    ))N)r   r   r   strr   _loader_func)selfr   r   r   r   r   s         r   __init__zGCSFileLoader.__init__   sB    F 	(	53 	5: 	5 ,7K<O    r   c                 @   	 ddl m} |j                  | j                  t        d            }|j                  | j                        }|j                  | j                        }|j                  | j                        j                  }t        j                         5 }| d| j                   }t        j                  t        j                  j!                  |      d       |j#                  |       | j%                  |      }|j'                         }	|	D ]W  }
d	|
j                  v r)d
| j                   d| j                   |
j                  d	<   |s=|
j                  j)                  |       Y |	cddd       S # t        $ r t        d      w xY w# 1 sw Y   yxY w)zLoad documents.r   )storagezCould not import google-cloud-storage python package. Please, install gcs dependency group: `pip install langchain-google-community[gcs]`zgoogle-cloud-storage)client_info/T)exist_oksourcezgs://N)google.cloudr    r   Clientr   r   
get_bucketr   r   get_blobmetadatatempfileTemporaryDirectoryosmakedirspathdirnamedownload_to_filenamer   loadupdate)r   r    storage_clientr   r   r)   temp_dirr   loaderdocsdocs              r   r1   zGCSFileLoader.loadR   sl   	, !?;Q+R ( 
  **4;;7{{499%??499-66((* 	h#*Adii[1IKK	2TB%%i0&&y1F;;=D 2s||+/4T[[M499+-NCLL*LL''1	2
 	 	#  	@ 	"	 	s   E< B6FF<FF)N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r1    r   r   r
   r
      s_    ( >B5P5P 5P 	5P
 huj'89:5Pn!d8n !r   r
   )r,   r*   typingr   r   r   langchain_core.document_loadersr   langchain_core.documentsr   !langchain_google_community._utilsr   r
   r<   r   r   <module>rA      s(    	  + + 6 - =hJ hr   