-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
54 lines (42 loc) · 1.44 KB
/
Copy pathmodels.py
File metadata and controls
54 lines (42 loc) · 1.44 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
"""Data models used by FolderVisualizer's folder scanner."""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any
@dataclass
class FileNode:
"""A file or directory in the scanned tree."""
name: str
path: str
is_directory: bool
size: int = 0
children: list["FileNode"] = field(default_factory=list)
error: str | None = None
def to_dict(self) -> dict[str, Any]:
"""Return a JSON-serializable representation of this node."""
data: dict[str, Any] = {
"name": self.name,
"path": self.path,
"type": "directory" if self.is_directory else "file",
"size": self.size,
"children": [child.to_dict() for child in self.children],
}
if self.error is not None:
data["error"] = self.error
return data
@dataclass
class ScanResult:
"""The tree and summary produced by one folder scan."""
root: FileNode
total_size: int
file_count: int
directory_count: int
errors: list[str] = field(default_factory=list)
def to_dict(self) -> dict[str, Any]:
"""Return a JSON-serializable representation of the scan result."""
return {
"root": self.root.to_dict(),
"total_size": self.total_size,
"file_count": self.file_count,
"directory_count": self.directory_count,
"errors": list(self.errors),
}