Skip to content

Commit 321b103

Browse files
committed
feat: add download and download_file methods to WokwiClient and file_ops
1 parent dab255f commit 321b103

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/wokwi_client/client.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from .constants import DEFAULT_WS_URL
1717
from .control import set_control
1818
from .event_queue import EventQueue
19-
from .file_ops import upload, upload_file
19+
from .file_ops import download, download_file, upload, upload_file
2020
from .pins import pin_listen, pin_read
2121
from .protocol_types import EventMessage, ResponseMessage
2222
from .serial import monitor_lines, write_serial
@@ -93,6 +93,28 @@ async def upload_file(
9393
"""
9494
return await upload_file(self._transport, filename, local_path)
9595

96+
async def download(self, name: str) -> ResponseMessage:
97+
"""
98+
Download a file from the simulator.
99+
100+
Args:
101+
name: The name of the file to download.
102+
103+
Returns:
104+
The response message from the server.
105+
"""
106+
return await download(self._transport, name)
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+
await download_file(self._transport, name, local_path)
117+
96118
async def start_simulation(
97119
self,
98120
firmware: str,

src/wokwi_client/file_ops.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,16 @@ 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})
29+
30+
31+
async def download_file(transport: Transport, name: str, local_path: Optional[Path] = None) -> None:
32+
if local_path is None:
33+
local_path = Path(name)
34+
35+
result = await download(transport, name)
36+
with open(local_path, "wb") as f:
37+
f.write(base64.b64decode(result["result"]["binary"]))

0 commit comments

Comments
 (0)