File: //usr/local/lib/python3.10/dist-packages/langchain/storage/__pycache__/file_system.cpython-310.pyc
o
���g� � @ sp d dl Z d dlZd dlZd dlmZ d dlmZmZmZm Z m
Z
mZ d dlm
Z
d dlmZ G dd� de
�ZdS )� N)�Path)�Iterator�List�Optional�Sequence�Tuple�Union)� ByteStore)�InvalidKeyExceptionc @ s� e Zd ZdZdddd�deeef dee dee de d df
d
d�Z
ded efd
d�Zded dfdd�Zde
e d eee fdd�Zde
eeef d dfdd�Zde
e d dfdd�Zddee d ee fdd�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 �returnc C s$ t |��� | _|| _|| _|| _dS )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 �H/usr/local/lib/python3.10/dist-packages/langchain/storage/file_system.py�__init__( s
zLocalFileStore.__init__�keyc C sz t �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 )r r � full_path�common_pathr r r �_get_full_pathC s ����zLocalFileStore._get_full_path�dirc C sD |� � s| �|j� |jdd� | jdur t�|| j� dS dS dS )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! r r r r$ X s
�zLocalFileStore._mkdir_for_store�keysc C sd g }|D ]+}| � |�}|�� r*|�� }|�|� | jr)t�|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 �mgetj s
�zLocalFileStore.mget�key_value_pairsc C sJ |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 r r'