Skip to content

Commit 17d8f01

Browse files
feat: add download and download_file methods (#5)
1 parent 572afed commit 17d8f01

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/wokwi_client/client.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5+
import base64
56
from pathlib import Path
67
from typing import Any, Optional, Union
78

@@ -14,7 +15,7 @@
1415
from .constants import DEFAULT_WS_URL
1516
from .control import set_control
1617
from .event_queue import EventQueue
17-
from .file_ops import upload, upload_file
18+
from .file_ops import download, upload, upload_file
1819
from .pins import pin_listen, pin_read
1920
from .protocol_types import EventMessage, ResponseMessage
2021
from .serial import monitor_lines, write_serial
@@ -91,6 +92,34 @@ async def upload_file(
9192
"""
9293
return await upload_file(self._transport, filename, local_path)
9394

95+
async def download(self, name: str) -> bytes:
96+
"""
97+
Download a file from the simulator.
98+
99+
Args:
100+
name: The name of the file to download.
101+
102+
Returns:
103+
The downloaded file content as bytes.
104+
"""
105+
result = await download(self._transport, name)
106+
return base64.b64decode(result["result"]["binary"])
107+
108+
async def download_file(self, name: str, local_path: Optional[Path] = None) -> None:
109+
"""
110+
Download a file from the simulator and save it to a local path.
111+
112+
Args:
113+
name: The name of the file to download.
114+
local_path: The local path to save the downloaded file. If not provided, uses the name as the path.
115+
"""
116+
if local_path is None:
117+
local_path = Path(name)
118+
119+
result = await self.download(name)
120+
with open(local_path, "wb") as f:
121+
f.write(result)
122+
94123
async def start_simulation(
95124
self,
96125
firmware: str,

src/wokwi_client/file_ops.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ async def upload_file(
2222
async def upload(transport: Transport, name: str, content: bytes) -> ResponseMessage:
2323
params = UploadParams(name=name, binary=base64.b64encode(content).decode())
2424
return await transport.request("file:upload", params.model_dump())
25+
26+
27+
async def download(transport: Transport, name: str) -> ResponseMessage:
28+
return await transport.request("file:download", {"name": name})

0 commit comments

Comments
 (0)