Files
task-69de7223f309a98be0007e09/virtual_fs.py
T
2026-06-04 19:19:40 +00:00

40 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Virtual file system used by the deep agent.
It stores files in memory and can be dumped to the real file system.
"""
import os
from pathlib import Path
from typing import Dict, Optional
class VirtualFileSystem:
"""A simple inmemory file system.
Files are stored as a mapping from a relative path to its content.
"""
def __init__(self, base_dir: str = "virtual_fs"):
self.base_dir = Path(base_dir)
self.files: Dict[str, str] = {}
def write_file(self, path: str, content: str) -> None:
norm_path = Path(path).as_posix()
self.files[norm_path] = content
def read_file(self, path: str) -> Optional[str]:
norm_path = Path(path).as_posix()
return self.files.get(norm_path)
def list_files(self) -> Dict[str, str]:
return dict(self.files)
def dump_to_real_fs(self, real_root: str = "real_fs") -> None:
real_root_path = Path(real_root)
for rel_path, content in self.files.items():
file_path = real_root_path / rel_path
file_path.parent.mkdir(parents=True, exist_ok=True)
file_path.write_text(content, encoding="utf-8")
def __repr__(self) -> str:
return f"VirtualFileSystem(files={list(self.files.keys())})"