Skip to content

Inode

Bases: Inode

Source code in nava/platform/util/files/inode.py
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
@dataclass
class DirNode(Inode):
    children: dict[str, Inode] = field(default_factory=dict)

    def is_file(self) -> bool:
        return False

    def add_file(self, path: Path) -> None:
        """Add a file to the inode tree at the given path."""
        if len(path.parts) < 1:
            return

        node = self
        for i, part in enumerate(path.parts[:-1]):
            if part not in node.children:
                subpath = Path(*path.parts[: i + 1])
                node.children[part] = DirNode(subpath)
            child = node.children[part]
            if not isinstance(child, DirNode):
                raise ValueError(
                    f"Cannot add path {path}. {node.children[part].path} is not a directory"
                )
            node = child
        part = path.parts[-1]
        node.children[part] = FileNode(path)

    @staticmethod
    def build_tree_from_paths(paths: list[Path]) -> "DirNode":
        root = DirNode(Path("."))
        for path in paths:
            root.add_file(path)
        return root

Add a file to the inode tree at the given path.

Source code in nava/platform/util/files/inode.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def add_file(self, path: Path) -> None:
    """Add a file to the inode tree at the given path."""
    if len(path.parts) < 1:
        return

    node = self
    for i, part in enumerate(path.parts[:-1]):
        if part not in node.children:
            subpath = Path(*path.parts[: i + 1])
            node.children[part] = DirNode(subpath)
        child = node.children[part]
        if not isinstance(child, DirNode):
            raise ValueError(
                f"Cannot add path {path}. {node.children[part].path} is not a directory"
            )
        node = child
    part = path.parts[-1]
    node.children[part] = FileNode(path)