summaryrefslogtreecommitdiff
path: root/src/agents/03-logs/vfs.py
blob: a521676635b05edd7374d5978dda8b6d295a033f (plain)
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
# vfs.py

class VirtualFileSystem:
    def __init__(self):
        self.fs = {"/": {}}

    def _get_path(self, path):
        parts = path.strip("/").split("/")
        node = self.fs["/"]
        for part in parts:
            if part == "":
                continue
            if part not in node:
                return None
            node = node[part]
        return node

    def list_files(self, path="."):
        if path == ".":
            path = "/"
        node = self._get_path(path)
        if node is None or not isinstance(node, dict):
            return "Path not found or not a directory."
        return list(node.keys())

    def create_file(self, file_path, content):
        parts = file_path.strip("/").split("/")
        filename = parts.pop()
        if not filename:
            return "Invalid file path."
            
        dir_path = "/" + "/".join(parts)
        node = self._get_path(dir_path)
        
        if node is None or not isinstance(node, dict):
            # For simplicity, let's automatically create the directory if it doesn't exist.
            node = self.fs["/"]
            for part in parts:
                if part == "":
                    continue
                if part not in node:
                    node[part] = {}
                node = node[part]

        node[filename] = content
        return f"File '{file_path}' created successfully."

    def read_file(self, file_path):
        node = self._get_path(file_path)
        if node is None or isinstance(node, dict):
            return "File not found or is a directory."
        return node

# For a single, shared instance of the VFS
vfs_instance = VirtualFileSystem()