HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/local/lib/python3.10/dist-packages/langchain/storage/__pycache__/file_system.cpython-310.pyc
o

���g��@spddlZddlZddlZddlmZddlmZmZmZm	Z	m
Z
mZddlm
Z
ddlmZGdd�de
�ZdS)�N)�Path)�Iterator�List�Optional�Sequence�Tuple�Union)�	ByteStore)�InvalidKeyExceptionc@s�eZdZdZdddd�deeefdeedeede	d	df
d
d�Z
ded	efd
d�Zded	dfdd�Zde
ed	eeefdd�Zde
eeefd	dfdd�Zde
ed	dfdd�Zddeed	eefdd�ZdS)�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�returncCs$t|���|_||_||_||_dS)anImplement 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�absoluterrr
r)�selfrrr
r�r�H/usr/local/lib/python3.10/dist-packages/langchain/storage/file_system.py�__init__(s
zLocalFileStore.__init__�keyc	Cszt�d|�s
td|����tj�|j|�}tj�t|j�|g�}|t|j�kr9td|�d|j�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_.\-/]+$zInvalid characters in key: z
Invalid key: z*. Key should be relative to the full path.z vs. z and full path of )
�re�matchr
�os�path�abspathr�
commonpath�strr)rr�	full_path�common_pathrrr�_get_full_pathCs	����zLocalFileStore._get_full_path�dircCsD|��s|�|j�|jdd�|jdur t�||j�dSdSdS)aKMakes 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) The store directory to make

        Returns:
            None
        T)�exist_okN)�exists�_mkdir_for_store�parent�mkdirr
r�chmod)rr!rrrr$Xs
�zLocalFileStore._mkdir_for_store�keyscCsdg}|D]+}|�|�}|��r*|��}|�|�|jr)t�|t��t�|�j	f�q|�d�q|S)aGet 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�appendrr�utime�time�stat�st_mtime)rr(�valuesrr�valuerrr�mgetjs


�zLocalFileStore.mget�key_value_pairscCsJ|D] \}}|�|�}|�|j�|�|�|jdur"t�||j�qdS)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rr')rr2rr0rrrr�mset�s	


��zLocalFileStore.msetcCs(|D]}|�|�}|��r|��qdS)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)rr(rrrrr�mdelete�s	
��zLocalFileStore.mdelete�prefixccsH�|r|�|�n|j}|�d�D]}|��r!|�|j�}t|�VqdS)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)rr7�prefix_path�file�
relative_pathrrr�
yield_keys�s�	
��zLocalFileStore.yield_keys)N)�__name__�
__module__�__qualname__�__doc__rrrr�int�boolrr r$rr�bytesr1rr4r6rr?rrrrrs,�
����
� r)rrr,�pathlibr�typingrrrrrr�langchain_core.storesr	�langchain.storage.exceptionsr
rrrrr�<module>s