Skip to content

Commit

Permalink
Add BaseFileLoader.save() (#1433)
Browse files Browse the repository at this point in the history
  • Loading branch information
collindutter authored Dec 20, 2024
1 parent 4b53ef5 commit 78c7e06
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `BranchTask` for selecting which Tasks (if any) to run based on a condition.
- Support for `BranchTask` in `StructureVisualizer`.
- `EvalEngine` for evaluating the performance of an LLM's output against a given input.
- `BaseFileLoader.save()` method for saving an Artifact to a destination.

### Changed

Expand Down
6 changes: 6 additions & 0 deletions docs/griptape-framework/data/loaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ multiple sources with [load_collection()](../../reference/griptape/loaders/base_

The following Loaders load a file using a [FileManagerDriver](../../reference/griptape/drivers/file_manager/base_file_manager_driver.md) and loads the resulting data into an [Artifact](../../griptape-framework/data/artifacts.md) for the respective file type.

As a convenience, File Loaders also have a `save()` method that can save an Artifact to a destination.

```python
--8<-- "docs/griptape-framework/data/src/loaders_save.py"
```

### Text

Loads text files into [TextArtifact](../../griptape-framework/data/artifacts.md#text)s:
Expand Down
10 changes: 10 additions & 0 deletions docs/griptape-framework/data/src/loaders_save.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from griptape.drivers import LocalFileManagerDriver
from griptape.loaders import TextLoader

loader = TextLoader(file_manager_driver=LocalFileManagerDriver())

data = loader.load("tests/resources/test.txt")

data.value = data.value.replace("foo", "bar")

loader.save("tests/resources/test.txt", data)
5 changes: 5 additions & 0 deletions griptape/loaders/base_file_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ def fetch(self, source: str | PathLike) -> bytes:
return data.encode(self.encoding)
else:
return data

def save(self, destination: str | PathLike, artifact: A) -> None:
"""Saves the Artifact to a destination."""
artifact.encoding = self.encoding
self.file_manager_driver.save_file(str(destination), artifact.to_bytes())
40 changes: 40 additions & 0 deletions tests/unit/loaders/test_base_file_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import tempfile

import pytest

from griptape.loaders.text_loader import TextLoader


class TestBaseFileLoader:
@pytest.fixture(params=["ascii", "utf-8", None])
def loader(self, request):
encoding = request.param
if encoding is None:
return TextLoader()
else:
return TextLoader(encoding=encoding)

@pytest.fixture(params=["path_from_resource_path"])
def create_source(self, request):
return request.getfixturevalue(request.param)

def test_fetch(self, loader, create_source):
source = create_source("test.txt")

data = loader.fetch(source)

assert data.startswith(b"foobar foobar foobar")

def test_save(self, loader, create_source):
source = create_source("test.txt")

data = loader.load(source)

with tempfile.TemporaryDirectory() as temp_dir:
destination = create_source(f"{temp_dir}/test.txt")

loader.save(destination, data)

data_copy = loader.load(destination)

assert data.value == data_copy.value

0 comments on commit 78c7e06

Please sign in to comment.