Spaces:
Sleeping
Sleeping
File size: 1,933 Bytes
c5997d8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | # actions/file_actions.py
from environment.file_system import FileSystem
def create_folder(fs: FileSystem, name: str) -> dict:
"""Action: Create a new folder."""
return fs.create_folder(name)
def move_file(fs: FileSystem, filename: str, destination: str) -> dict:
"""Action: Move a file to a destination folder."""
return fs.move_file(filename, destination)
def rename_file(fs: FileSystem, old_name: str, new_name: str) -> dict:
"""Action: Rename a file."""
return fs.rename_file(old_name, new_name)
def delete_file(fs: FileSystem, filename: str) -> dict:
"""Action: Delete a file."""
return fs.delete_file(filename)
def zip_folder(fs: FileSystem, folder_name: str) -> dict:
"""Action: Zip a folder to compress it."""
return fs.zip_folder(folder_name)
def inspect_file(fs: FileSystem, filename: str) -> dict:
"""Action: Inspect a file's metadata."""
info = fs.get_file_info(filename)
if info:
return {"success": True, "file_info": info}
return {"success": False, "message": f"File '{filename}' not found."}
def list_all_files(fs: FileSystem) -> dict:
"""Action: List all files in the filesystem."""
files = fs.list_files()
return {"success": True, "files": files, "total": len(files)}
# Registry mapping action name → callable
ACTION_REGISTRY = {
"create_folder": create_folder,
"move_file": move_file,
"rename_file": rename_file,
"delete_file": delete_file,
"zip_folder": zip_folder,
"inspect_file": inspect_file,
"list_all_files": list_all_files,
}
def execute_action(fs, action_type, params):
"""
Dispatch action using registry.
"""
if action_type not in ACTION_REGISTRY:
raise ValueError(
f"Unknown action: {action_type}"
)
handler = ACTION_REGISTRY[action_type]
# Call handler with parameters
return handler(
fs,
**params
) |