Skip to content

Commit fa1efd8

Browse files
committed
organized tree code
1 parent dc75727 commit fa1efd8

File tree

1 file changed

+26
-33
lines changed
  • core/libs/commonwealth/src/commonwealth/utils

1 file changed

+26
-33
lines changed
Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,21 @@
1-
from typing import Any, Callable, List, Dict
1+
from typing import Any, Callable, Dict, List
2+
3+
import zenoh
24

35

46
class TreeNode:
57
def __init__(self, segment: str):
68
self.segment = segment
7-
self.children: Dict[str, 'TreeNode'] = {}
9+
self.children: Dict[str, "TreeNode"] = {}
810
self.is_valid = False
911
self.methods: Dict[str, Callable[..., Any]] = {}
10-
11-
def __str__(self, level=0, prefix="", is_last=True):
12-
ret = prefix
13-
if level > 0:
14-
ret += "|___ " if is_last else "|--- "
15-
ret += f"{self.segment}"
16-
if self.is_valid:
17-
ret += " [Valid]"
18-
ret += "\n"
19-
20-
prefix += " " if is_last else "| "
21-
children_list = list(self.children.values())
22-
for i, child in enumerate(children_list):
23-
is_last_child = (i == len(children_list) - 1)
24-
ret += child.__str__(level + 1, prefix, is_last_child)
25-
26-
return ret
27-
28-
def add_child(self, child: 'TreeNode') -> 'TreeNode':
12+
13+
def add_child(self, child: "TreeNode") -> "TreeNode":
2914
if child.segment in self.children:
3015
return self.children[child.segment]
3116
self.children[child.segment] = child
3217
return child
33-
34-
def is_valid(self) -> bool:
35-
return self.is_valid
36-
18+
3719
def get_methods(self) -> Dict[str, Callable[..., Any]]:
3820
return self.methods
3921

@@ -45,20 +27,20 @@ def add_node(self, segments: List[str], method: str, func: Callable[..., Any]) -
4527
self.is_valid = True
4628
self.methods[method] = func
4729
return
48-
30+
4931
child = self.add_child(TreeNode(segments[1]))
5032
child.add_node(segments[1:], method, func)
51-
33+
5234
def process_path(self, path: str, method: str, func: Callable[..., Any]) -> None:
53-
segments = path.split('/')
35+
segments = path.split("/")
5436
self.add_node(segments, method, func)
5537

56-
def get_match(self, path: str) -> tuple[str, Dict[str, Callable[..., Any]]] | None:
57-
segments = path.split('/')
38+
def get_match(self, path: str) -> tuple[str, "TreeNode"] | None:
39+
segments = path.split("/")
5840

5941
if self.segment != segments[0]:
6042
return None
61-
43+
6244
node = self
6345
matched_path = node.segment
6446

@@ -72,8 +54,19 @@ def get_match(self, path: str) -> tuple[str, Dict[str, Callable[..., Any]]] | No
7254
matched_path += "/" + node.segment
7355
except KeyError:
7456
return None
75-
7657

7758
if node.is_valid:
78-
return matched_path, node.get_methods()
59+
return matched_path, node
7960
return None
61+
62+
def get_corresponding_method(self, sampleKind: zenoh.SampleKind) -> Callable[..., Any] | None:
63+
if sampleKind == zenoh.SampleKind.DELETE:
64+
keyword = "DELETE"
65+
else:
66+
methods = [method for method, _ in self.methods.items() if method != "DELETE"]
67+
keyword = methods[0]
68+
69+
try:
70+
return self.methods[keyword]
71+
except KeyError:
72+
return None

0 commit comments

Comments
 (0)