Repository navigation
Expand file tree
/
Copy pathmodels.py
More file actions
65 lines (57 loc) · 2.18 KB
/
Copy pathmodels.py
File metadata and controls
65 lines (57 loc) · 2.18 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
from dataclasses import dataclass, field
from typing import Optional
_STYLE_FIELDS = {
"bg", "fg", "branch",
"shape", "branch_style", "branch_pattern",
"bold", "font_size", "italic", "strikethrough",
"border_color", "border_width", "border_pattern",
}
@dataclass
class Style:
# Colors
bg: Optional[str] = None # node background color (hex #RRGGBB)
fg: Optional[str] = None # text color (hex #RRGGBB)
branch: Optional[str] = None # branch line color (hex #RRGGBB)
# Shape: roundedRect | rect | underline | ellipserect
shape: Optional[str] = None
# Branch line
# branch_style: curve | roundedElbow | elbow | straight | arrowedCurve | bight | roundedfold
branch_style: Optional[str] = None
# branch_pattern / border_pattern: solid | dash | handdrawn-dash | none
branch_pattern: Optional[str] = None
# Text
bold: Optional[bool] = None # bold text
italic: Optional[bool] = None # italic text
strikethrough: Optional[bool] = None # strikethrough text
font_size: Optional[str] = None # font size, e.g. "14" or "14pt"
# Border
border_color: Optional[str] = None # border line color (hex #RRGGBB)
border_width: Optional[str] = None # border line width, e.g. "2pt"
border_pattern: Optional[str] = None
@dataclass
class Node:
id: str
title: str
style: Optional[Style] = None
children: list["Node"] = field(default_factory=list)
def to_dict(self) -> dict:
d = {"id": self.id, "title": self.title}
if self.style:
style_dict = {
k: v for k, v in self.style.__dict__.items() if v is not None
}
if style_dict:
d["style"] = style_dict
d["children"] = [c.to_dict() for c in self.children]
return d
@staticmethod
def from_dict(d: dict) -> "Node":
style = None
if "style" in d:
style = Style(**{k: v for k, v in d["style"].items() if k in _STYLE_FIELDS})
return Node(
id=d["id"],
title=d["title"],
style=style,
children=[Node.from_dict(c) for c in d.get("children", [])],
)