Debian Codegraph @

cylc/flow/tui/tree.py

raw
1 #!/usr/bin/env python3
2 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
3 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 """Tree utilities for Tui."""
18
19
20 def find_closest_focus(app, old_node, new_node):
21 """Return the position of the old node in the new tree.
22
23 1. Attempts to find the old node in the new tree.
24 2. Otherwise it walks up the old tree until it
25 finds a node which is present in the new tree.
26 3. Otherwise it returns the root node of the new tree.
27
28 Arguments:
29 app (TuiApp):
30 Tui app instance.
31 old_node (MonitorNode):
32 The in-focus node from the deceased tree.
33 new_node (MonitorNode):
34 The root node from the new tree.
35
36 Returns
37 MonitorNode - The closest node.
38
39 """
40 old_key = app.get_node_id(old_node)
41
42 for node in walk_tree(new_node):
43 if old_key == app.get_node_id(node):
44 # (1)
45 return node
46
47 if not old_node._parent:
48 # (3) reset focus
49 return new_node
50
51 # (2)
52 return find_closest_focus(
53 app,
54 old_node._parent,
55 new_node
56 )
57
58
59 def expand_tree(app, tree_node, id_, depth=5, node_types=None):
60 """Expand the Tui tree to the desired level.
61
62 Arguments:
63 app:
64 The Tui application instance.
65 tree_node:
66 The Tui widget representing the tree view.
67 id_:
68 If specified, we will look within the tree for a node matching
69 this ID and the tree below this node will be expanded.
70 depth:
71 The max depth to expand nodes too.
72 node_types:
73 Whitelist of node types to expand, note "task", "job" and "spring"
74 nodes are excluded by default.
75
76 Returns:
77 True, if the node was found in the tree, is loaded and has been
78 expanded.
79
80 Examples:
81 # expand the top three levels of the tree
82 compute_tree(app, node, None, 3)
83
84 # expand the "root" node AND the top five levels of the tree under
85 # ~user/workflow
86 compute_tree(app, node, '~user/workflow')
87
88 """
89 if not node_types:
90 # don't auto-expand job nodes by default
91 node_types = {'root', 'workflow', 'cycle', 'family'}
92
93 root_node = tree_node.get_root()
94 requested_node = root_node
95
96 # locate the "id_" within the tree if specified
97 if id_:
98 for node in walk_tree(root_node):
99 key = app.get_node_id(node)
100 if key == id_:
101 requested_node = node
102 child_keys = node.get_child_keys()
103 if (
104 # if the node only has one child
105 len(child_keys) == 1
106 # and that child is a "#spring" node (i.e. a loading node)
107 and (
108 node.get_child_node(0).get_value()['type_']
109 ) == '#spring'
110 ):
111 # then the content hasn't loaded yet so the node cannot be
112 # expanded
113 return False
114 break
115 else:
116 # the requested node does not exist yet
117 # it might still be loading
118 return False
119
120 # expand the specified nodes
121 for node in (*walk_tree(requested_node, depth), root_node):
122 if node.get_value()['type_'] not in node_types:
123 continue
124 widget = node.get_widget()
125 widget.expanded = True
126 widget.update_expanded_icon(False)
127
128 return True
129
130
131 def translate_collapsing(app, old_node, new_node):
132 """Transfer the collapse state from one tree to another.
133
134 Arguments:
135 app (TuiApp):
136 Tui app instance.
137 old_node (MonitorNode):
138 Any node in the tree you want to copy the
139 collapse/expand state from.
140 new_node (MonitorNode):
141 Any node in the tree you want to copy the
142 collapse/expand state to.
143
144 """
145 old_root = old_node.get_root()
146 new_root = new_node.get_root()
147
148 old_tree = {
149 app.get_node_id(node): node.get_widget().expanded
150 for node in walk_tree(old_root)
151 }
152
153 for node in walk_tree(new_root):
154 key = app.get_node_id(node)
155 if key in old_tree:
156 # this node was present before
157 # => translate its expansion to the new tree
158 expanded = old_tree.get(key)
159 widget = node.get_widget()
160 if widget.expanded != expanded:
161 widget.expanded = expanded
162 widget.update_expanded_icon(False)
163 else:
164 # this node was not present before
165 # => apply the standard expansion logic
166 expand_tree(
167 app,
168 node,
169 key,
170 3,
171 # don't auto-expand workflows, only cycles/families
172 # and the root node to help expand the tree on startup
173 node_types={'root', 'cycle', 'family'}
174 )
175
176
177 def walk_tree(node, depth=None):
178 """Yield nodes in order.
179
180 Arguments:
181 node (urwid.TreeNode):
182 Yield this node and all nodes beneath it.
183 depth:
184 The maximum depth to walk to or None to walk all children.
185
186 Yields:
187 urwid.TreeNode
188
189 """
190 stack = [(node, 1)]
191 while stack:
192 node, _depth = stack.pop()
193 yield node
194 if depth is None or _depth < depth:
195 stack.extend([
196 (node.get_child_node(index), _depth + 1)
197 for index in node.get_child_keys()
198 ])