Skip to content

Commit 4049bf8

Browse files
authored
Merge pull request #246 from jumpstarter-dev/opendal-comments
Add comments to the opendal packages
2 parents cae60f6 + b721e09 commit 4049bf8

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

packages/jumpstarter-driver-opendal/jumpstarter_driver_opendal/adapter.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
@dataclass(frozen=True, kw_only=True, slots=True)
1515
class AsyncFileStream(ObjectStream[bytes]):
16+
"""
17+
wrapper type for opendal.AsyncFile to make it compatible with anyio streams
18+
"""
19+
1620
file: AsyncFile
1721

1822
async def send(self, item: bytes):
@@ -42,21 +46,22 @@ async def aclose(self):
4246

4347
@dataclass(kw_only=True)
4448
class OpendalAdapter(ClientAdapter):
45-
operator: Operator
46-
path: str
47-
mode: Literal["rb", "wb"] = "rb"
49+
operator: Operator # opendal.Operator for the storage backend
50+
path: str # file path in storage backend relative to the storage root
51+
mode: Literal["rb", "wb"] = "rb" # binary read or binary write mode
4852

4953
async def __aenter__(self):
54+
# if the access mode is binary read, and the storage backend supports presigned read requests
5055
if self.mode == "rb" and self.operator.capability().presign_read:
56+
# create presigned url for the specified file with a 60 second expiration
5157
presigned = await self.operator.to_async_operator().presign_read(self.path, expire_second=60)
5258
return PresignedRequestResource(
5359
headers=presigned.headers, url=presigned.url, method=presigned.method
5460
).model_dump(mode="json")
61+
# otherwise stream the file content from the client to the exporter
5562
else:
5663
file = await self.operator.to_async_operator().open(self.path, self.mode)
57-
5864
self.resource = self.client.resource_async(AsyncFileStream(file=file))
59-
6065
return await self.resource.__aenter__()
6166

6267
async def __aexit__(self, exc_type, exc_value, traceback):

packages/jumpstarter-driver-opendal/jumpstarter_driver_opendal/driver_test.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,23 @@
1414
def test_drivers_mock_storage_mux_fs(monkeypatch: pytest.MonkeyPatch):
1515
with serve(MockStorageMux()) as client:
1616
with TemporaryDirectory() as tempdir:
17+
# original file on the client to be pushed to the exporter
1718
original = Path(tempdir) / "original"
19+
# new file read back from the exporter to the client
1820
readback = Path(tempdir) / "readback"
1921

20-
# absolute path
22+
# test accessing files with absolute path
23+
24+
# fill the original file with random bytes
2125
original.write_bytes(randbytes(1024 * 1024 * 10))
26+
# write the file to the storage on the exporter
2227
client.write_local_file(str(original))
28+
# read the storage on the exporter to a local file
2329
client.read_local_file(str(readback))
30+
# ensure the contents are equal
2431
assert original.read_bytes() == readback.read_bytes()
2532

26-
# relative path
33+
# test accessing files with relative path
2734
with monkeypatch.context() as m:
2835
m.chdir(tempdir)
2936

@@ -39,6 +46,7 @@ def test_drivers_mock_storage_mux_fs(monkeypatch: pytest.MonkeyPatch):
3946

4047

4148
def test_drivers_mock_storage_mux_http():
49+
# dummy HTTP server returning static test content
4250
class StaticHandler(BaseHTTPRequestHandler):
4351
def do_HEAD(self):
4452
self.send_response(200)
@@ -52,11 +60,13 @@ def do_GET(self):
5260
self.wfile.write(b"testcontent" * 1000)
5361

5462
with serve(MockStorageMux()) as client:
63+
# start the HTTP server
5564
server = HTTPServer(("127.0.0.1", 8080), StaticHandler)
5665
server_thread = Thread(target=server.serve_forever)
5766
server_thread.daemon = True
5867
server_thread.start()
5968

69+
# write a remote file from the http server to the exporter
6070
fs = Operator("http", endpoint="http://127.0.0.1:8080")
6171
client.write_file(fs, "test")
6272

0 commit comments

Comments
 (0)