Source code for are.simulation.apps.virtual_file_system
# Copyright (c) Meta Platforms, Inc. and affiliates.# All rights reserved.## This source code is licensed under the terms described in the LICENSE file in# the root directory of this source tree.fromtypingimportAnyfromare.simulation.apps.appimportApp,Protocolfromare.simulation.apps.sandbox_file_systemimportSandboxLocalFileSystemfromare.simulation.tool_utilsimportOperationType,app_toolfromare.simulation.typesimportevent_registered
[docs]classVirtualFileSystem(App):""" A proxy class that delegates file system operations to SandboxLocalFileSystem. This class maintains backward compatibility with the original VirtualFileSystem API while using the more comprehensive SandboxLocalFileSystem implementation. """def__init__(self,name:str|None=None):super().__init__(name)# Create an instance of SandboxLocalFileSystem to delegate operations toself.sandbox_fs=SandboxLocalFileSystem(name)
[docs]@app_tool()@event_registered(operation_type=OperationType.WRITE)defopen(self,path:str,mode:str="rb"):""" Open a file for reading or writing. :param path: path to the file to open :param mode: mode to open the file in :returns: file handle to the opened file """returnself.sandbox_fs.open(path,mode)
[docs]defget_file_content(self,path:str)->bytes:""" Read the contents of a file. :param path: path to the file to read :returns: contents of the file """returnself.sandbox_fs.get_file_content(path)
[docs]@app_tool()@event_registered(operation_type=OperationType.READ)defcat(self,path:str)->bytes:""" Read the contents of a file. :param path: path to the file to read :returns: contents of the file as bytes """returnself.sandbox_fs.cat(path)
[docs]@app_tool()@event_registered(operation_type=OperationType.WRITE)defmkdir(self,path:str,create_recursive:bool=True):""" Create a directory. :param path: path to the directory to create :param create_recursive: if True, create parent directories if they do not exist :returns: None """# Map create_recursive to create_parents for compatibilityreturnself.sandbox_fs.mkdir(path,create_parents=create_recursive)
[docs]@app_tool()@event_registered(operation_type=OperationType.WRITE)defmv(self,path1:str,path2:str):""" Move a file or directory. :param path1: path to the file or directory to move :param path2: path to the destination :returns: None """returnself.sandbox_fs.mv(path1,path2)
[docs]@app_tool()@event_registered(operation_type=OperationType.READ)defls(self,path:str=".",detail:bool=False):""" List the contents of a directory. :param path: path to list, defaults to '.' :param detail: if True, return detailed information about each file :returns: list of files and directories in the directory """returnself.sandbox_fs.ls(path,detail=detail)
[docs]@app_tool()@event_registered(operation_type=OperationType.WRITE)defrm(self,path:str,recursive:bool=False):""" Remove a file or directory. :param path: path to the file or directory to remove :param recursive: if True, remove the directory and all its contents :returns: None """returnself.sandbox_fs.rm(path,recursive=recursive)
[docs]@app_tool()@event_registered(operation_type=OperationType.WRITE)defrmdir(self,path:str):""" Remove a directory. :param path: path to the directory to remove :returns: None """returnself.sandbox_fs.rmdir(path)
[docs]@app_tool()@event_registered(operation_type=OperationType.READ)defexists(self,path:str):""" Check if a file or directory exists. :param path: path to the file or directory to check :returns: True if the file or directory exists, False otherwise """returnself.sandbox_fs.exists(path)
[docs]@app_tool()@event_registered(operation_type=OperationType.READ)definfo(self,path:str,**kwargs:dict[str,Any]):""" Get information about a file or directory. :param path: path to the file or directory to get information about :param kwargs: additional arguments to pass to the file system :returns: information about the file or directory """returnself.sandbox_fs.info(path,**kwargs)
[docs]@app_tool()@event_registered(operation_type=OperationType.READ)deftree(self,path:str=".")->str:""" Generate a string representing the directory and file structure in a tree format. :param path: Path to start the tree view from, defaults to the root of the sandbox :returns: A string representing the directory tree """returnself.sandbox_fs.tree(path)
[docs]defload_file_system_from_path(self,path:str):""" Load file system from a path. :param path: path to load the file system from """returnself.sandbox_fs.load_file_system_from_path(path)
[docs]defget_state(self)->dict[str,Any]:""" Get the current state of the file system in the original VirtualFileSystem format. This maintains backward compatibility with previously stored states. :returns: dictionary representing the current state """returnself.sandbox_fs.get_state()
@propertydeftmpdir(self)->str:""" Get the temporary directory path used by the sandbox file system. For backward compatibility with the SandboxLocalFileSystem API. :returns: path to the temporary directory """returnself.sandbox_fs.tmpdir