22#
33# SPDX-License-Identifier: MIT
44
5+ import base64
56from pathlib import Path
67from typing import Any , Optional , Union
78
1617from .constants import DEFAULT_WS_URL
1718from .control import set_control
1819from .event_queue import EventQueue
19- from .file_ops import download , download_file , upload , upload_file
20+ from .file_ops import download , upload , upload_file
2021from .pins import pin_listen , pin_read
2122from .protocol_types import EventMessage , ResponseMessage
2223from .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 ,
0 commit comments