
    Ng                     ~    d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZ d dlmZ d dlmZ  G d de          ZdS )    N)Path)IteratorListOptionalSequenceTupleUnion)	ByteStore)InvalidKeyExceptionc                   &   e Zd ZdZdddddeeef         dee         dee         de	d	df
d
Z
ded	efdZded	dfdZdee         d	eee                  fdZdeeeef                  d	dfdZdee         d	dfdZddee         d	ee         f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                |    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   s        Y/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain/storage/file_system.py__init__zLocalFileStore.__init__(   s9    , i1133$"(    keyc           
         t          j        d|          st          d|           t          j                            | j        |z            }t          j                            t          | j                  |g          }|t          | j                  k    r t          d| d| j         d| d|           t          |          S )zGet 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   ospathabspathr   
commonpathstrr   )r   r   	full_pathcommon_paths       r   _get_full_pathzLocalFileStore._get_full_pathC   s     x.44 	K%&IC&I&IJJJGOODNS$899	g((#dn*=*=y)IJJ#dn----%S S S>S S(3S SGPS S  
 Ir   dirc                     |                                 sS|                     |j                   |                    d           | j        t          j        || j                   dS dS dS )aK  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) The store directory to make

        Returns:
            None
        T)exist_okN)exists_mkdir_for_storeparentmkdirr   r   chmod)r   r%   s     r   r)   zLocalFileStore._mkdir_for_storeX   sr     zz|| 	.!!#*---IItI$$$~)dn-----		. 	. *)r   keysc                 p   g }|D ]}|                      |          }|                                rp|                                }|                    |           | j        r?t          j        |t          j                    t          j        |          j	        f           |                    d           |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"   values         r   mgetzLocalFileStore.mgetj   s     )+ 		$ 		$C++C00I!! $!,,..e$$$$ THYbgi6H6H6Q(RSSSd####r   key_value_pairsc                     |D ]j\  }}|                      |          }|                     |j                   |                    |           | j        t          j        || j                   kdS )zSet 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,   )r   r8   r   r6   r"   s        r   msetzLocalFileStore.mset   s|     * 	5 	5JC++C00I!!)"2333!!%(((*DO444	5 	5r   c                     |D ]?}|                      |          }|                                r|                                 @dS )zDelete 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"   s       r   mdeletezLocalFileStore.mdelete   sW      	# 	#C++C00I!! #  """	# 	#r   prefixc              #      K   |r|                      |          n| j        }|                    d          D ]A}|                                r+|                    | j                  }t          |          V  BdS )zGet 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   r?   prefix_pathfilerelative_paths        r   
yield_keyszLocalFileStore.yield_keys   s       6<Od))&111%%c** 	) 	)D||~~ ) $ 0 0 @ @-(((((	) 	)r   )N)__name__
__module____qualname____doc__r	   r!   r   r   intboolr   r$   r)   r   r   bytesr7   r   r;   r>   r   rH    r   r   r   r      s        > %)#'") ) )d#) SM	)
 C=) ) 
) ) ) )6# $    *.D .T . . . .$# 4+@    .5HU3:->$? 5D 5 5 5 5 #HSM #d # # # #) )# )(3- ) ) ) ) ) )r   r   )r   r   r2   pathlibr   typingr   r   r   r   r   r	   langchain_core.storesr
   langchain.storage.exceptionsr   r   rP   r   r   <module>rU      s    				 				        C C C C C C C C C C C C C C C C + + + + + + < < < < < <`) `) `) `) `)Y `) `) `) `) `)r   