summaryrefslogtreecommitdiff
path: root/extension/examples/filesystem
diff options
context:
space:
mode:
authorNate Sesti <sestinj@gmail.com>2023-05-23 23:45:12 -0400
committerNate Sesti <sestinj@gmail.com>2023-05-23 23:45:12 -0400
commitf53768612b1e2268697b5444e502032ef9f3fb3c (patch)
tree4ed49b73e6bd3c2f8fceffa9643973033f87af95 /extension/examples/filesystem
downloadsncontinue-f53768612b1e2268697b5444e502032ef9f3fb3c.tar.gz
sncontinue-f53768612b1e2268697b5444e502032ef9f3fb3c.tar.bz2
sncontinue-f53768612b1e2268697b5444e502032ef9f3fb3c.zip
copying from old repo
Diffstat (limited to 'extension/examples/filesystem')
-rw-r--r--extension/examples/filesystem/filesystem.py45
-rw-r--r--extension/examples/filesystem/real.py34
-rw-r--r--extension/examples/filesystem/virtual.py39
3 files changed, 118 insertions, 0 deletions
diff --git a/extension/examples/filesystem/filesystem.py b/extension/examples/filesystem/filesystem.py
new file mode 100644
index 00000000..eb5c2043
--- /dev/null
+++ b/extension/examples/filesystem/filesystem.py
@@ -0,0 +1,45 @@
+from abc import ABC, abstractmethod
+from typing import List
+
+
+class FileSystem(ABC):
+ """An abstract filesystem that can read/write from a set of files."""
+ @abstractmethod
+ def read(self, path) -> str:
+ raise NotImplementedError
+
+ @abstractmethod
+ def readlines(self, path) -> List[str]:
+ raise NotImplementedError
+
+ @abstractmethod
+ def write(self, path, content):
+ raise NotImplementedError
+
+ @abstractmethod
+ def exists(self, path) -> bool:
+ raise NotImplementedError
+
+ @abstractmethod
+ def rename_file(self, filepath: str, new_filepath: str):
+ raise NotImplementedError
+
+ @abstractmethod
+ def walk(self, path: str) -> List[str]:
+ raise NotImplementedError
+
+ @abstractmethod
+ def rename_directory(self, path: str, new_path: str):
+ raise NotImplementedError
+
+ @abstractmethod
+ def delete_file(self, filepath: str):
+ raise NotImplementedError
+
+ @abstractmethod
+ def delete_directory(self, path: str):
+ raise NotImplementedError
+
+ @abstractmethod
+ def add_directory(self, path: str):
+ raise NotImplementedError
diff --git a/extension/examples/filesystem/real.py b/extension/examples/filesystem/real.py
new file mode 100644
index 00000000..ec7aa693
--- /dev/null
+++ b/extension/examples/filesystem/real.py
@@ -0,0 +1,34 @@
+import os
+from typing import List
+from filesystem.filesystem import FileSystem
+
+
+class RealFileSystem(FileSystem):
+ """A filesystem that reads/writes from the actual filesystem."""
+
+ def read(self, path) -> str:
+ with open(path, "r") as f:
+ return f.read()
+
+ def readlines(self, path) -> List[str]:
+ with open(path, "r") as f:
+ return f.readlines()
+
+ def write(self, path, content):
+ with open(path, "w") as f:
+ f.write(content)
+
+ def exists(self, path) -> bool:
+ return os.path.exists(path)
+
+ def rename_file(self, filepath: str, new_filepath: str):
+ os.rename(filepath, new_filepath)
+
+ def rename_directory(self, path: str, new_path: str):
+ os.rename(path, new_path)
+
+ def delete_file(self, filepath: str):
+ os.remove(filepath)
+
+ def add_directory(self, path: str):
+ os.makedirs(path)
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