Skip to content

base_file_manager_driver

BaseFileManagerDriver

Bases: ABC

BaseFileManagerDriver can be used to list, load, and save files.

Attributes:

Name Type Description
default_loader

The default loader to use for loading file contents into artifacts.

loaders

Dictionary of file extension specific loaders to use for loading file contents into artifacts.

Source code in griptape/drivers/file_manager/base_file_manager_driver.py
@define
class BaseFileManagerDriver(ABC):
    """BaseFileManagerDriver can be used to list, load, and save files.

    Attributes:
        default_loader: The default loader to use for loading file contents into artifacts.
        loaders: Dictionary of file extension specific loaders to use for loading file contents into artifacts.
    """

    workdir: str = field(kw_only=True)
    encoding: Optional[str] = field(default=None, kw_only=True)

    def list_files(self, path: str) -> TextArtifact:
        entries = self.try_list_files(path)
        return TextArtifact("\n".join(list(entries)))

    @abstractmethod
    def try_list_files(self, path: str) -> list[str]: ...

    def load_file(self, path: str) -> BlobArtifact | TextArtifact:
        if self.encoding is None:
            return BlobArtifact(self.try_load_file(path))
        else:
            return TextArtifact(self.try_load_file(path).decode(encoding=self.encoding), encoding=self.encoding)

    @abstractmethod
    def try_load_file(self, path: str) -> bytes: ...

    def save_file(self, path: str, value: bytes | str) -> InfoArtifact:
        if isinstance(value, str):
            value = value.encode() if self.encoding is None else value.encode(encoding=self.encoding)
        elif isinstance(value, (bytearray, memoryview)):
            raise ValueError(f"Unsupported type: {type(value)}")

        self.try_save_file(path, value)

        return InfoArtifact("Successfully saved file")

    @abstractmethod
    def try_save_file(self, path: str, value: bytes) -> None: ...

encoding: Optional[str] = field(default=None, kw_only=True) class-attribute instance-attribute

workdir: str = field(kw_only=True) class-attribute instance-attribute

list_files(path)

Source code in griptape/drivers/file_manager/base_file_manager_driver.py
def list_files(self, path: str) -> TextArtifact:
    entries = self.try_list_files(path)
    return TextArtifact("\n".join(list(entries)))

load_file(path)

Source code in griptape/drivers/file_manager/base_file_manager_driver.py
def load_file(self, path: str) -> BlobArtifact | TextArtifact:
    if self.encoding is None:
        return BlobArtifact(self.try_load_file(path))
    else:
        return TextArtifact(self.try_load_file(path).decode(encoding=self.encoding), encoding=self.encoding)

save_file(path, value)

Source code in griptape/drivers/file_manager/base_file_manager_driver.py
def save_file(self, path: str, value: bytes | str) -> InfoArtifact:
    if isinstance(value, str):
        value = value.encode() if self.encoding is None else value.encode(encoding=self.encoding)
    elif isinstance(value, (bytearray, memoryview)):
        raise ValueError(f"Unsupported type: {type(value)}")

    self.try_save_file(path, value)

    return InfoArtifact("Successfully saved file")

try_list_files(path) abstractmethod

Source code in griptape/drivers/file_manager/base_file_manager_driver.py
@abstractmethod
def try_list_files(self, path: str) -> list[str]: ...

try_load_file(path) abstractmethod

Source code in griptape/drivers/file_manager/base_file_manager_driver.py
@abstractmethod
def try_load_file(self, path: str) -> bytes: ...

try_save_file(path, value) abstractmethod

Source code in griptape/drivers/file_manager/base_file_manager_driver.py
@abstractmethod
def try_save_file(self, path: str, value: bytes) -> None: ...