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 G dd de	ZdS )    N)AnyListOptional)Document)
BaseLoader)UnstructuredFileLoaderc                   @   sD   e Zd ZdZdeeeeee ddddZdee	e
 dd	d
ZdS )OBSFileLoaderz Load from the `Huawei OBS file`.N )bucketkeyclientendpointconfigreturnc                 C   s   zddl m} W n ty*   tdY n0 |s|s<td|sFt }|dr^||dd}n"||d|d	|d
|d}t||std|| _|| _	|| _
dS )a=
  Initialize the OBSFileLoader with the specified settings.

        Args:
            bucket (str): The name of the OBS bucket to be used.
            key (str): The name of the object in the OBS bucket.
            client (ObsClient, optional): An instance of the ObsClient to connect to OBS.
            endpoint (str, optional): The endpoint URL of your OBS bucket. This parameter is mandatory if `client` is not provided.
            config (dict, optional): The parameters for connecting to OBS, provided as a dictionary. This parameter is ignored if `client` is provided. The dictionary could have the following keys:
                - "ak" (str, optional): Your OBS access key (required if `get_token_from_ecs` is False and bucket policy is not public read).
                - "sk" (str, optional): Your OBS secret key (required if `get_token_from_ecs` is False and bucket policy is not public read).
                - "token" (str, optional): Your security token (required if using temporary credentials).
                - "get_token_from_ecs" (bool, optional): Whether to retrieve the security token from ECS. Defaults to False if not provided. If set to True, `ak`, `sk`, and `token` will be ignored.

        Raises:
            ValueError: If the `esdk-obs-python` package is not installed.
            TypeError: If the provided `client` is not an instance of ObsClient.
            ValueError: If `client` is not provided, but `endpoint` is missing.

        Note:
            Before using this class, make sure you have registered with OBS and have the necessary credentials. The `ak`, `sk`, and `endpoint` values are mandatory unless `get_token_from_ecs` is True or the bucket policy is public read. `token` is required when using temporary credentials.

        Example:
            To create a new OBSFileLoader with a new client:
            ```
            config = {
                "ak": "your-access-key",
                "sk": "your-secret-key"
            }
            obs_loader = OBSFileLoader("your-bucket-name", "your-object-key", config=config)
            ```

            To create a new OBSFileLoader with an existing client:
            ```
            from obs import ObsClient

            # Assuming you have an existing ObsClient object 'obs_client'
            obs_loader = OBSFileLoader("your-bucket-name", "your-object-key", client=obs_client)
            ```

            To create a new OBSFileLoader without an existing client:
            ```
            obs_loader = OBSFileLoader("your-bucket-name", "your-object-key", endpoint="your-endpoint-url")
            ```
        r   )	ObsClientzfCould not import esdk-obs-python python package. Please install it with `pip install esdk-obs-python`.z.Either OBSClient or endpoint must be provided.Zget_token_from_ecsZECS)serverZsecurity_provider_policyZaksktoken)Zaccess_key_idZsecret_access_keyZsecurity_tokenr   zClient must be ObsClient typeN)Zobsr   ImportError
ValueErrordictget
isinstance	TypeErrorr   r
   r   )selfr
   r   r   r   r   r    r   {/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/document_loaders/obs_file.py__init__   s0    4


zOBSFileLoader.__init__single)moder   c                 C   s   t  h}| d| j d| j }tjtj|dd | jj	| j| j|d t
||d}| W  d   S 1 sv0    Y  dS )zLoad documents./T)exist_ok)Z
bucketNameZ	objectKeydownloadFile)r   N)tempfileTemporaryDirectoryr
   r   osmakedirspathdirnamer   r"   r   load)r   r   temp_dir	file_pathloaderr   r   r   r)   _   s    

zOBSFileLoader.load)Nr	   N)r   )__name__
__module____qualname____doc__strr   r   r   r   r   r   r)   r   r   r   r   r      s      Or   )r%   r#   typingr   r   r   Zlangchain_core.documentsr   Z)langchain_community.document_loaders.baser   Z1langchain_community.document_loaders.unstructuredr   r   r   r   r   r   <module>   s   