
    Ah:                     v    S SK r S SKrS SKrS SKJrJr  S SKJr  S SKJ	r	J
r
  S SKJr  S SKJr   " S S\5      rg)	    N)IteratorSequence)Path)OptionalUnion)	ByteStore)InvalidKeyExceptionc                       \ rS rSrSrSSSS.S\\\4   S\\	   S\\	   S	\
S
S4
S jjrS\S
\4S jrS\S
S4S jrS\\   S
\\\      4S jrS\\\\4      S
S4S jrS\\   S
S4S jrSS\\   S
\\   4S jjrSrg)LocalFileStore   a  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                d    [        U5      R                  5       U l        X l        X0l        X@l        g)a  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        U/var/www/html/shao/venv/lib/python3.13/site-packages/langchain/storage/file_system.py__init__LocalFileStore.__init__)   s(    , i113$"(    keyc                 \   [         R                  " SU5      (       d  SU 3n[        U5      eU R                  U-  R	                  5       nU R                  R	                  5       n[
        R                  R                  XC/5      nU[        U5      :w  a  SU SU SU SU 3n[        U5      eU$ )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	   r   resolveospath
commonpathstr)r   r   msg	full_pathr   common_paths         r   _get_full_pathLocalFileStore._get_full_pathD   s     xx.44/u5C%c**^^c)224	NN**,	gg(())?@#i.(u$O+U;-/A)N  &c**r   dir_pathc                     UR                  5       (       dT  U R                  UR                  5        UR                  SS9  U R                  b  UR                  U R                  5        ggg)a  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'   s     r   r+   LocalFileStore._mkdir_for_store\   sU       !!(//2NNDN)~~)t~~. * !r   keysc                 ~   / nU H  nU R                  U5      nUR                  5       (       az  UR                  5       nUR                  U5        U R                  (       aF  [
        R                  " U[        R                  " 5       UR                  5       R                  45        M  M  UR                  S5        M     U$ )zGet 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   r0   valuesr   r#   values         r   mgetLocalFileStore.mgetn   s     )+C++C0I!!!,,.e$$$HHYinn6F6O6O(PQ % d#  r   key_value_pairsc                     U Hl  u  p#U R                  U5      nU R                  UR                  5        UR                  U5        U R                  c  MQ  UR                  U R                  5        Mn     g)zqSet 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<   r   r9   r#   s        r   msetLocalFileStore.mset   s\     *JC++C0I!!)"2"23!!%(*0 *r   c                     U H;  nU R                  U5      nUR                  5       (       d  M+  UR                  5         M=     g)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   r0   r   r#   s       r   mdeleteLocalFileStore.mdelete   s9     C++C0I!!  " r   prefixc              #     #    U(       a  U R                  U5      OU R                  nUR                  S5       HB  nUR                  5       (       d  M  UR	                  U R                  5      n[        U5      v   MD     g7f)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   rE   prefix_pathfilerelative_paths        r   
yield_keysLocalFileStore.yield_keys   s^      6<d))&1%%c*D||~~ $ 0 0 @-(( +s   AA?-A?)r   r   r   r   )N)__name__
__module____qualname____firstlineno____doc__r   r!   r   r   intboolr   r%   r+   r   listbytesr:   tupler?   rC   r   rN   __static_attributes__ r   r   r   r      s    > %)#'")d#) SM	)
 C=) ) 
)6# $ 0/ /$ /$# 4+@ .1HU3:->$? 1D 1 #HSM #d #)# )(3- ) )r   r   )r   r   r5   collections.abcr   r   pathlibr   typingr   r   langchain_core.storesr   langchain.storage.exceptionsr	   r   r[   r   r   <module>ra      s+    	 	  .  " + <c)Y c)r   