a
    •˜ëj;  ã                   @   sp   d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZ G dd„ deƒZdS )	é    N)ÚIteratorÚSequence)ÚPath)ÚOptionalÚUnion)Ú	ByteStore)ÚInvalidKeyExceptionc                   @   sÆ   e Zd ZdZddddœeeef ee ee e	ddœdd„Z
eedœd	d
„Zeddœdd„Zee eee  dœdd„Zeeeef  ddœdd„Zee ddœdd„Zdee ee dœdd„ZdS )ÚLocalFileStorea  BaseStore interface that works on the local file system.

    Examples:
        Create a LocalFileStore instance and perform operations on it:

        .. code-block:: python

            from langchain.storage import LocalFileStore

            # Instantiate the LocalFileStore with the root path
            file_store = LocalFileStore("/path/to/root")

            # Set values for keys
            file_store.mset([("key1", b"value1"), ("key2", b"value2")])

            # Get values for keys
            values = file_store.mget(["key1", "key2"])  # Returns [b"value1", b"value2"]

            # Delete keys
            file_store.mdelete(["key1"])

            # Iterate over keys
            for key in file_store.yield_keys():
                print(key)  # noqa: T201

    NF)Ú
chmod_fileÚ	chmod_dirÚupdate_atime)Ú	root_pathr
   r   r   Úreturnc                C   s$   t |ƒ ¡ | _|| _|| _|| _dS )an  Implement the BaseStore interface for the local file system.

        Args:
            root_path (Union[str, Path]): The root path of the file store. All keys are
                interpreted as paths relative to this root.
            chmod_file: (optional, defaults to `None`) If specified, sets permissions
                for newly created files, overriding the current `umask` if needed.
            chmod_dir: (optional, defaults to `None`) If specified, sets permissions
                for newly created dirs, overriding the current `umask` if needed.
            update_atime: (optional, defaults to `False`) If `True`, updates the
                filesystem access time (but not the modified time) when a file is read.
                This allows MRU/LRU cache policies to be implemented for filesystems
                where access time updates are disabled.
        N)r   Úabsoluter   r
   r   r   )Úselfr   r
   r   r   © r   ú\/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/storage/file_system.pyÚ__init__)   s    zLocalFileStore.__init__)Úkeyr   c                 C   sz   t  d|¡sd|› �}t|ƒ‚| j|  ¡ }| j ¡ }tj ||g¡}|t|ƒkrvd|› d|› d|› d|› �}t|ƒ‚|S )zÓGet the full path for a given key relative to the root path.

        Args:
            key (str): The key relative to the root path.

        Returns:
            Path: The full path for the given key.
        z^[a-zA-Z0-9_.\-/]+$zInvalid characters in key: zInvalid key: z+. Key should be relative to the full path. z vs. z and full path of )	ÚreÚmatchr   r   ÚresolveÚosÚpathÚ
commonpathÚstr)r   r   ÚmsgÚ	full_pathr   Zcommon_pathr   r   r   Ú_get_full_pathD   s"    	

ÿÿÿÿzLocalFileStore._get_full_path)Údir_pathr   c                 C   s:   |  ¡ s6|  |j¡ |jdd� | jdur6| | j¡ dS )aQ  Makes a store directory path (including parents) with specified permissions.

        This is needed because `Path.mkdir()` is restricted by the current `umask`,
        whereas the explicit `os.chmod()` used here is not.

        Args:
            dir_path: (Path) The store directory to make

        Returns:
            None
        T)Úexist_okN)ÚexistsÚ_mkdir_for_storeÚparentÚmkdirr   Úchmod)r   r   r   r   r   r"   \   s
    
zLocalFileStore._mkdir_for_store)Úkeysr   c                 C   sb   g }|D ]T}|   |¡}| ¡ rR| ¡ }| |¡ | jr\t |t ¡ | ¡ j	f¡ q| d¡ q|S )a  Get the values associated with the given keys.

        Args:
            keys: A sequence of keys.

        Returns:
            A sequence of optional values associated with the keys.
            If a key is not found, the corresponding value will be None.
        N)
r   r!   Ú
read_bytesÚappendr   r   ÚutimeÚtimeÚstatÚst_mtime)r   r&   Úvaluesr   r   Úvaluer   r   r   Úmgetn   s    


zLocalFileStore.mget)Úkey_value_pairsr   c                 C   sH   |D ]>\}}|   |¡}|  |j¡ | |¡ | jdur| | j¡ qdS )z™Set the values for the given keys.

        Args:
            key_value_pairs: A sequence of key-value pairs.

        Returns:
            None
        N)r   r"   r#   Úwrite_bytesr
   r%   )r   r0   r   r.   r   r   r   r   Úmset…   s    	


zLocalFileStore.msetc                 C   s(   |D ]}|   |¡}| ¡ r| ¡  qdS )z­Delete the given keys and their associated values.

        Args:
            keys (Sequence[str]): A sequence of keys to delete.

        Returns:
            None
        N)r   r!   Úunlink)r   r&   r   r   r   r   r   Úmdelete•   s    	
zLocalFileStore.mdelete)Úprefixr   c                 c   sF   |r|   |¡n| j}| d¡D ]"}| ¡ r| | j¡}t|ƒV  qdS )zçGet an iterator over keys that match the given prefix.

        Args:
            prefix (Optional[str]): The prefix to match.

        Returns:
            Iterator[str]: An iterator over keys that match the given prefix.
        Ú*N)r   r   ÚrglobÚis_fileÚrelative_tor   )r   r5   Úprefix_pathÚfileÚrelative_pathr   r   r   Ú
yield_keys£   s
    	zLocalFileStore.yield_keys)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   ÚintÚboolr   r   r"   r   ÚlistÚbytesr/   Útupler2   r4   r   r=   r   r   r   r   r	      s"   ú
ùr	   )r   r   r*   Úcollections.abcr   r   Úpathlibr   Útypingr   r   Zlangchain_core.storesr   Zlangchain.storage.exceptionsr   r	   r   r   r   r   Ú<module>   s   