0
# Utilities
1
2
Utility functions and helper classes for cgroup tree traversal, path manipulation, and content type handling.
3
4
## Capabilities
5
6
### Tree Traversal Utilities
7
8
Core traversal functions for walking through node hierarchies with different ordering strategies.
9
10
```python { .api }
11
def walk_tree(root):
12
"""
13
Pre-order depth-first traversal of a node tree.
14
15
Parameters:
16
- root: Node or NodeControlGroup, starting node for traversal
17
18
Yields:
19
Node objects in pre-order (parent before children)
20
"""
21
22
def walk_up_tree(root):
23
"""
24
Post-order depth-first traversal of a node tree.
25
26
Parameters:
27
- root: Node or NodeControlGroup, starting node for traversal
28
29
Yields:
30
Node objects in post-order (children before parent)
31
"""
32
```
33
34
Usage example:
35
36
```python
37
from cgroupspy.trees import Tree
38
from cgroupspy.utils import walk_tree, walk_up_tree
39
40
# Create tree
41
t = Tree()
42
43
# Walk entire tree pre-order
44
for node in walk_tree(t.root):
45
print(f"Pre-order: {node.path}")
46
47
# Walk entire tree post-order
48
for node in walk_up_tree(t.root):
49
print(f"Post-order: {node.path}")
50
51
# Walk from specific node
52
cpuset = t.get_node_by_path('/cpuset/')
53
for node in walk_tree(cpuset):
54
print(f"Cpuset subtree: {node.path}")
55
```
56
57
### Path Manipulation
58
59
Path parsing and component extraction utilities for cgroup path handling.
60
61
```python { .api }
62
def split_path_components(path):
63
"""
64
Split a filesystem path into individual components.
65
66
Parameters:
67
- path: str or bytes, filesystem path to split
68
69
Returns:
70
List of path components (directories/files)
71
72
Handles both absolute and relative paths, normalizing
73
separators and removing empty components.
74
"""
75
```
76
77
Usage example:
78
79
```python
80
from cgroupspy.utils import split_path_components
81
82
# Split absolute path
83
components = split_path_components('/sys/fs/cgroup/memory/docker')
84
print(components) # ['sys', 'fs', 'cgroup', 'memory', 'docker']
85
86
# Split relative path
87
components = split_path_components('docker/container-id')
88
print(components) # ['docker', 'container-id']
89
90
# Handle various path formats
91
components = split_path_components('//memory//test//')
92
print(components) # ['memory', 'test']
93
```
94
95
### Content Type System
96
97
Base classes and utilities for handling cgroup file content with automatic type conversion.
98
99
```python { .api }
100
class BaseContentType:
101
"""Abstract base class for cgroup content type conversion."""
102
103
def __str__(self) -> str:
104
"""Convert content to cgroup-compatible string format."""
105
106
def __repr__(self) -> str:
107
"""Developer-friendly representation."""
108
109
@classmethod
110
def from_string(cls, value: str):
111
"""
112
Parse content from cgroup file string format.
113
114
Parameters:
115
- value: str, raw string from cgroup file
116
117
Returns:
118
Instance of the content type
119
"""
120
```
121
122
Usage example:
123
124
```python
125
from cgroupspy.contenttypes import DeviceAccess
126
127
# Create device access object
128
access = DeviceAccess(
129
dev_type=DeviceAccess.TYPE_CHAR,
130
major=1, minor=5,
131
access=DeviceAccess.ACCESS_READ | DeviceAccess.ACCESS_WRITE
132
)
133
134
# Convert to cgroup format
135
print(str(access)) # "c 1:5 rw"
136
137
# Parse from cgroup format
138
parsed = DeviceAccess.from_string("c 1:5 rw")
139
print(parsed.dev_type) # "c"
140
print(parsed.major) # 1
141
print(parsed.minor) # 5
142
print(parsed.can_read) # True
143
print(parsed.can_write) # True
144
```
145
146
## Types
147
148
```python { .api }
149
# Tree traversal function signatures
150
WalkFunction = Callable[[Any], Generator[Any, None, None]]
151
152
# Path component handling
153
PathComponents = List[str]