|
2 | 2 | #
|
3 | 3 | # SPDX-License-Identifier: MIT
|
4 | 4 |
|
| 5 | +import base64 |
5 | 6 | from pathlib import Path
|
6 | 7 | from typing import Any, Optional, Union
|
7 | 8 |
|
|
14 | 15 | from .constants import DEFAULT_WS_URL
|
15 | 16 | from .control import set_control
|
16 | 17 | from .event_queue import EventQueue
|
17 |
| -from .file_ops import upload, upload_file |
| 18 | +from .file_ops import download, upload, upload_file |
18 | 19 | from .pins import pin_listen, pin_read
|
19 | 20 | from .protocol_types import EventMessage, ResponseMessage
|
20 | 21 | from .serial import monitor_lines, write_serial
|
@@ -91,6 +92,34 @@ async def upload_file(
|
91 | 92 | """
|
92 | 93 | return await upload_file(self._transport, filename, local_path)
|
93 | 94 |
|
| 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 | + |
94 | 123 | async def start_simulation(
|
95 | 124 | self,
|
96 | 125 | firmware: str,
|
|
0 commit comments