Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow cads-adaptors to directly write on the cache filesystem #198

Merged
merged 5 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions cads_adaptors/adaptors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import abc
import contextlib
import pathlib
from typing import Any, BinaryIO

import cads_adaptors.tools.logger
Expand Down Expand Up @@ -85,6 +86,7 @@ def __init__(
self,
form: list[dict[str, Any]] | dict[str, Any] | None,
context: Context | None = None,
cache_tmp_path: pathlib.Path | None = None,
**config: Any,
) -> None:
self.form = form
Expand All @@ -93,6 +95,9 @@ def __init__(
self.context = Context()
else:
self.context = context
self.cache_tmp_path = (
pathlib.Path() if cache_tmp_path is None else cache_tmp_path
)

@abc.abstractmethod
def normalise_request(self, request: Request) -> Request:
Expand Down Expand Up @@ -184,11 +189,19 @@ def retrieve(self, request: Request) -> BinaryIO:
microseconds=time_elapsed.microsecond,
).total_seconds()

self.context.add_stdout(f"Sleeping {time_sleep} s")
time.sleep(time_sleep)
with open("dummy.grib", "wb") as fp:

dummy_file = self.cache_tmp_path / "dummy.grib"
self.context.add_stdout(f"Writing {size} B to {dummy_file!s}")
tic = time.perf_counter()
with dummy_file.open("wb") as fp:
with open("/dev/urandom", "rb") as random:
while size > 0:
length = min(size, 10240)
fp.write(random.read(length))
size -= length
return open("dummy.grib", "rb")
toc = time.perf_counter()
self.context.add_stdout(f"Elapsed time to write the file: {toc - tic} s")

return dummy_file.open("rb")
11 changes: 11 additions & 0 deletions tests/test_10_cache_tmp_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os
import pathlib

import cads_adaptors


def test_cache_tmp_path_dummy_adaptor(tmp_path: pathlib.Path) -> None:
dummy_adaptor = cads_adaptors.DummyAdaptor(None, cache_tmp_path=tmp_path)
result = dummy_adaptor.retrieve({"size": 1})
assert result.name == str(tmp_path / "dummy.grib")
assert os.path.getsize(result.name) == 1