Skip to content

Commit 75909db

Browse files
committed
Merge remote-tracking branch 'origin/main' into add-download
2 parents f6cc44e + 111d347 commit 75909db

File tree

2 files changed

+10
-76
lines changed

2 files changed

+10
-76
lines changed

src/wokwi_client/client.py

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5-
import base64
65
from pathlib import Path
76
from typing import Any, Optional, Union
87

98
from wokwi_client.framebuffer import (
10-
compare_framebuffer_png,
11-
framebuffer_png_bytes,
12-
framebuffer_read,
9+
read_framebuffer_png_bytes,
1310
save_framebuffer_png,
1411
)
1512

1613
from .__version__ import get_version
1714
from .constants import DEFAULT_WS_URL
1815
from .control import set_control
1916
from .event_queue import EventQueue
20-
from .file_ops import download, upload, upload_file
17+
from .file_ops import upload, upload_file
2118
from .pins import pin_listen, pin_read
2219
from .protocol_types import EventMessage, ResponseMessage
2320
from .serial import monitor_lines, write_serial
@@ -94,34 +91,6 @@ async def upload_file(
9491
"""
9592
return await upload_file(self._transport, filename, local_path)
9693

97-
async def download(self, name: str) -> bytes:
98-
"""
99-
Download a file from the simulator.
100-
101-
Args:
102-
name: The name of the file to download.
103-
104-
Returns:
105-
The downloaded file content as bytes.
106-
"""
107-
result = await download(self._transport, name)
108-
return base64.b64decode(result["result"]["binary"])
109-
110-
async def download_file(self, name: str, local_path: Optional[Path] = None) -> None:
111-
"""
112-
Download a file from the simulator and save it to a local path.
113-
114-
Args:
115-
name: The name of the file to download.
116-
local_path: The local path to save the downloaded file. If not provided, uses the name as the path.
117-
"""
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)
124-
12594
async def start_simulation(
12695
self,
12796
firmware: str,
@@ -270,22 +239,10 @@ async def set_control(
270239
"""
271240
return await set_control(self._transport, part=part, control=control, value=value)
272241

273-
async def framebuffer_read(self, id: str) -> ResponseMessage:
274-
"""Read the current framebuffer for the given device id."""
275-
return await framebuffer_read(self._transport, id=id)
276-
277-
async def framebuffer_png_bytes(self, id: str) -> bytes:
242+
async def read_framebuffer_png_bytes(self, id: str) -> bytes:
278243
"""Return the current framebuffer as PNG bytes."""
279-
return await framebuffer_png_bytes(self._transport, id=id)
244+
return await read_framebuffer_png_bytes(self._transport, id=id)
280245

281246
async def save_framebuffer_png(self, id: str, path: Path, overwrite: bool = True) -> Path:
282247
"""Save the current framebuffer as a PNG file."""
283248
return await save_framebuffer_png(self._transport, id=id, path=path, overwrite=overwrite)
284-
285-
async def compare_framebuffer_png(
286-
self, id: str, reference: Path, save_mismatch: Optional[Path] = None
287-
) -> bool:
288-
"""Compare the current framebuffer with a reference PNG file."""
289-
return await compare_framebuffer_png(
290-
self._transport, id=id, reference=reference, save_mismatch=save_mismatch
291-
)

src/wokwi_client/framebuffer.py

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@
2424
from .transport import Transport
2525

2626
__all__ = [
27-
"framebuffer_read",
28-
"framebuffer_png_bytes",
27+
"read_framebuffer",
28+
"read_framebuffer_png_bytes",
2929
"save_framebuffer_png",
30-
"compare_framebuffer_png",
3130
]
3231

3332

34-
async def framebuffer_read(transport: Transport, *, id: str) -> ResponseMessage:
33+
async def read_framebuffer(transport: Transport, *, id: str) -> ResponseMessage:
3534
"""Issue `framebuffer:read` for the given device id and return raw response."""
3635
return await transport.request("framebuffer:read", {"id": id})
3736

@@ -44,9 +43,9 @@ def _extract_png_b64(resp: ResponseMessage) -> str:
4443
return png_b64
4544

4645

47-
async def framebuffer_png_bytes(transport: Transport, *, id: str) -> bytes:
46+
async def read_framebuffer_png_bytes(transport: Transport, *, id: str) -> bytes:
4847
"""Return decoded PNG bytes for the framebuffer of device `id`."""
49-
resp = await framebuffer_read(transport, id=id)
48+
resp = await read_framebuffer(transport, id=id)
5049
return base64.b64decode(_extract_png_b64(resp))
5150

5251

@@ -64,30 +63,8 @@ async def save_framebuffer_png(
6463
"""
6564
if path.exists() and not overwrite:
6665
raise WokwiError(f"File already exists and overwrite=False: {path}")
67-
data = await framebuffer_png_bytes(transport, id=id)
66+
data = await read_framebuffer_png_bytes(transport, id=id)
6867
path.parent.mkdir(parents=True, exist_ok=True)
6968
with open(path, "wb") as f:
7069
f.write(data)
7170
return path
72-
73-
74-
async def compare_framebuffer_png(
75-
transport: Transport, *, id: str, reference: Path, save_mismatch: Path | None = None
76-
) -> bool:
77-
"""Compare the current framebuffer PNG with a reference file.
78-
79-
Performs a byte-for-byte comparison. If different and `save_mismatch` is
80-
provided, writes the current framebuffer PNG there.
81-
82-
Returns True if identical, False otherwise.
83-
"""
84-
if not reference.exists():
85-
raise WokwiError(f"Reference image does not exist: {reference}")
86-
current = await framebuffer_png_bytes(transport, id=id)
87-
ref_bytes = reference.read_bytes()
88-
if current == ref_bytes:
89-
return True
90-
if save_mismatch:
91-
save_mismatch.parent.mkdir(parents=True, exist_ok=True)
92-
save_mismatch.write_bytes(current)
93-
return False

0 commit comments

Comments
 (0)