22#
33# SPDX-License-Identifier: MIT
44
5+ import base64
56from pathlib import Path
67from typing import Any , Optional , Union , cast
78
1516from .constants import DEFAULT_WS_URL
1617from .control import set_control
1718from .event_queue import EventQueue
18- from .file_ops import download , download_file , upload , upload_file
19+ from .file_ops import download , upload , upload_file
1920from .pins import gpio_list , pin_listen , pin_read
2021from .protocol_types import EventMessage , ResponseMessage
2122from .serial import monitor_lines , write_serial
@@ -92,17 +93,18 @@ async def upload_file(
9293 """
9394 return await upload_file (self ._transport , filename , local_path )
9495
95- async def download (self , name : str ) -> ResponseMessage :
96+ async def download (self , name : str ) -> bytes :
9697 """
9798 Download a file from the simulator.
9899
99100 Args:
100101 name: The name of the file to download.
101102
102103 Returns:
103- The response message from the server .
104+ The downloaded file content as bytes .
104105 """
105- return await download (self ._transport , name )
106+ result = await download (self ._transport , name )
107+ return base64 .b64decode (result ["result" ]["binary" ])
106108
107109 async def download_file (self , name : str , local_path : Optional [Path ] = None ) -> None :
108110 """
@@ -112,7 +114,12 @@ async def download_file(self, name: str, local_path: Optional[Path] = None) -> N
112114 name: The name of the file to download.
113115 local_path: The local path to save the downloaded file. If not provided, uses the name as the path.
114116 """
115- await download_file (self ._transport , name , local_path )
117+ if local_path is None :
118+ local_path = Path (name )
119+
120+ result = await self .download (name )
121+ with open (local_path , "wb" ) as f :
122+ f .write (result )
116123
117124 async def start_simulation (
118125 self ,
0 commit comments