From 27ecedb02ef79ce53bf533e016b00462c44541be Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Tue, 23 May 2023 23:45:12 -0400 Subject: copying from old repo --- extension/examples/filesystem/virtual.py | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 extension/examples/filesystem/virtual.py (limited to 'extension/examples/filesystem/virtual.py') diff --git a/extension/examples/filesystem/virtual.py b/extension/examples/filesystem/virtual.py new file mode 100644 index 00000000..91a10e7d --- /dev/null +++ b/extension/examples/filesystem/virtual.py @@ -0,0 +1,39 @@ +from typing import Dict, List +from filesystem.filesystem import FileSystem + + +class VirtualFileSystem(FileSystem): + """A simulated filesystem from a mapping of filepath to file contents.""" + files: Dict[str, str] + + def __init__(self, files: Dict[str, str]): + self.files = files + + def read(self, path) -> str: + return self.files[path] + + def readlines(self, path) -> List[str]: + return self.files[path].splitlines() + + def write(self, path, content): + self.files[path] = content + + def exists(self, path) -> bool: + return path in self.files + + def rename_file(self, filepath: str, new_filepath: str): + self.files[new_filepath] = self.files[filepath] + del self.files[filepath] + + def rename_directory(self, path: str, new_path: str): + for filepath in self.files: + if filepath.startswith(path): + new_filepath = new_path + filepath[len(path):] + self.files[new_filepath] = self.files[filepath] + del self.files[filepath] + + def delete_file(self, filepath: str): + del self.files[filepath] + + def add_directory(self, path: str): + pass -- cgit v1.2.3-70-g09d2