Skip to content

Commit eb636ef

Browse files
add dependency handler class
1 parent 2ae3aad commit eb636ef

File tree

2 files changed

+149
-1
lines changed

2 files changed

+149
-1
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
from typing import Optional, Dict, List, Iterator
2+
from dependency_node import DependencyNode
3+
4+
5+
class DependencyHandler:
6+
"""
7+
Manages a collection of DependencyNode objects with no duplicates.
8+
9+
Ensures that no two nodes with the same name can be added and provides
10+
methods to check readiness and retrieve specific nodes.
11+
12+
Example usage:
13+
# Create a handler
14+
handler = DependencyHandler()
15+
16+
# Create some dependency nodes
17+
node1 = DependencyNode(name="node1")
18+
node1.add_field("field1", str)
19+
node1.set_field_value("field1", "value1")
20+
21+
node2 = DependencyNode(name="node2")
22+
node2.add_field("field1", int)
23+
24+
# Add nodes to the handler
25+
handler.add_node(node1)
26+
handler.add_node(node2)
27+
28+
# Check if a specific node exists
29+
print(handler.has_node("node1")) # True
30+
31+
# Get a reference to a node and modify it
32+
node = handler.get_node("node2")
33+
node.set_field_value("field1", 42)
34+
35+
# Check if all nodes are ready
36+
print(handler.is_ready) # False (node2 is ready, but node1 isn't)
37+
"""
38+
39+
def __init__(self):
40+
# Using a dictionary with node names as keys ensures name uniqueness
41+
# and provides efficient lookups
42+
self._nodes: Dict[str, DependencyNode] = {}
43+
44+
def add_node(self, node: DependencyNode) -> bool:
45+
"""
46+
Add a dependency node to the handler.
47+
48+
Args:
49+
node: The DependencyNode to add
50+
51+
Returns:
52+
bool: True if the node was added, False if a node with the same name already exists
53+
54+
Raises:
55+
TypeError: If the provided object is not a DependencyNode
56+
"""
57+
if not isinstance(node, DependencyNode):
58+
raise TypeError(f"Expected DependencyNode, got {type(node).__name__}")
59+
60+
# Check if a node with this name already exists
61+
if node.name in self._nodes:
62+
return False
63+
64+
self._nodes[node.name] = node
65+
return True
66+
67+
@property
68+
def is_ready(self) -> bool:
69+
"""
70+
Check if all nodes are ready.
71+
72+
Returns:
73+
bool: True if all nodes are ready (or if there are no nodes), False otherwise
74+
"""
75+
if not self._nodes:
76+
return True
77+
78+
return all(node.is_ready for node in self._nodes.values())
79+
80+
def has_node(self, name: str) -> bool:
81+
"""
82+
Check if a node with the given name exists.
83+
84+
Args:
85+
name: The name to check
86+
87+
Returns:
88+
bool: True if a node with the given name exists, False otherwise
89+
"""
90+
return name in self._nodes
91+
92+
def get_node(self, name: str) -> Optional[DependencyNode]:
93+
"""
94+
Get a node by name for manipulation.
95+
96+
Args:
97+
name: The name of the node to retrieve
98+
99+
Returns:
100+
Optional[DependencyNode]: The node with the given name, or None if not found
101+
"""
102+
return self._nodes.get(name)
103+
104+
def remove_node(self, node_or_name) -> bool:
105+
"""
106+
Remove a node by name or reference.
107+
108+
Args:
109+
node_or_name: The node to remove or its name
110+
111+
Returns:
112+
bool: True if the node was removed, False if not found
113+
"""
114+
if isinstance(node_or_name, DependencyNode):
115+
name = node_or_name.name
116+
else:
117+
name = node_or_name
118+
119+
if name in self._nodes:
120+
del self._nodes[name]
121+
return True
122+
return False
123+
124+
def get_all_nodes(self) -> List[DependencyNode]:
125+
"""
126+
Get all nodes stored in the handler.
127+
128+
Returns:
129+
List[DependencyNode]: List of all nodes
130+
"""
131+
return list(self._nodes.values())
132+
133+
def __iter__(self) -> Iterator[DependencyNode]:
134+
"""
135+
Iterate over all nodes.
136+
137+
Returns:
138+
Iterator[DependencyNode]: Iterator over all nodes
139+
"""
140+
return iter(self._nodes.values())
141+
142+
def __len__(self) -> int:
143+
"""
144+
Get the number of nodes in the handler.
145+
146+
Returns:
147+
int: The number of nodes
148+
"""
149+
return len(self._nodes)

pythonbpf/vmlinux_parser/dependency_tree.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)