-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.py
More file actions
95 lines (71 loc) · 2.42 KB
/
Copy pathFileSystem.py
File metadata and controls
95 lines (71 loc) · 2.42 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from __future__ import annotations
from dataclasses import dataclass, field
from pathlib import Path
from typing import List, Optional
import uuid
@dataclass
class Node:
name: str
type: str # folder, archive, file
path: Path = Path()
id: Optional[uuid.UUID] = None
parent: Optional["Node"] = None
children: List["Node"] = field(default_factory=list)
def get_path(self, path: Path) -> "Node": # pyright: ignore[reportReturnType] # Not working
"""Gets the Node at the passed path
Args:
path (Path): path to the node
Returns:
Node: the node at passed path
"""
if len(path.parts) == 1:
return self
for child in self.children:
if child.name == path.parts[1]:
return child.get_path(Path(*path.parts[1:]))
def remove(self, path: Optional[Path] = None):
"""Removes the node at passed path
Args:
path (Path): path to the node
Raises:
NotImplementedError
"""
if path is None:
node: "Node" = self
else:
node: "Node" = self.get_path(path)
node.parent._remove_child(node) # pyright: ignore[reportOptionalMemberAccess]
def _remove_child(self, node: "Node"):
"""Removes the child node
Args:
node (Node): node to remove
"""
self.children.remove(node)
def add(self, child: "Node", path: Optional[Path] = None):
"""Adds the passed Node at the passed path
Args:
path (Path): the new node parent directory
child (Node): the node to be added
Raises:
NotImplementedError: _description_
"""
if path is None:
parent = self
else:
parent: "Node" = self.get_path(path)
if parent.type == "file":
raise Exception("Cannot add child node to a type `file`")
if child.path == Path():
child.path = self.path / child.name
child.parent = self
parent._add_child(child)
return child
def _add_child(self, node: "Node"):
"""Adds the passed Node to the children
Args:
node (Node): the node to be added
"""
self.children.append(node)
def __str__(self) -> str:
"""Returns the path of the node as a string"""
return f"{self.path}"