Skip to content

Commit f6cc44e

Browse files
committed
feat: update download methods to return file content as bytes
1 parent 321b103 commit f6cc44e

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

src/wokwi_client/client.py

Lines changed: 12 additions & 5 deletions
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

@@ -16,7 +17,7 @@
1617
from .constants import DEFAULT_WS_URL
1718
from .control import set_control
1819
from .event_queue import EventQueue
19-
from .file_ops import download, download_file, upload, upload_file
20+
from .file_ops import download, upload, upload_file
2021
from .pins import pin_listen, pin_read
2122
from .protocol_types import EventMessage, ResponseMessage
2223
from .serial import monitor_lines, write_serial
@@ -93,17 +94,18 @@ async def upload_file(
9394
"""
9495
return await upload_file(self._transport, filename, local_path)
9596

96-
async def download(self, name: str) -> ResponseMessage:
97+
async def download(self, name: str) -> bytes:
9798
"""
9899
Download a file from the simulator.
99100
100101
Args:
101102
name: The name of the file to download.
102103
103104
Returns:
104-
The response message from the server.
105+
The downloaded file content as bytes.
105106
"""
106-
return await download(self._transport, name)
107+
result = await download(self._transport, name)
108+
return base64.b64decode(result["result"]["binary"])
107109

108110
async def download_file(self, name: str, local_path: Optional[Path] = None) -> None:
109111
"""
@@ -113,7 +115,12 @@ async def download_file(self, name: str, local_path: Optional[Path] = None) -> N
113115
name: The name of the file to download.
114116
local_path: The local path to save the downloaded file. If not provided, uses the name as the path.
115117
"""
116-
await download_file(self._transport, name, local_path)
118+
if local_path is None:
119+
local_path = Path(name)
120+
121+
result = await self.download(name)
122+
with open(local_path, "wb") as f:
123+
f.write(result)
117124

118125
async def start_simulation(
119126
self,

src/wokwi_client/file_ops.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,3 @@ async def upload(transport: Transport, name: str, content: bytes) -> ResponseMes
2626

2727
async def download(transport: Transport, name: str) -> ResponseMessage:
2828
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)