Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow suppressing tree leaves #139

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion tests/config/test_cmd_def_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ def test_printout_6(self):
self.cmd_tree.children["acs"].add_child(mgm_0_node)
update_cfg = CmdTreeNode("update_cfg", "Update Configuration")
self.cmd_tree.children["acs"]["mgm_0"].add_child(update_cfg)
print(self.cmd_tree["acs"]["mgm_0"]["update_cfg"].hide_children_for_print)
tcs_ctrl = CmdTreeNode(
"tcs_ctrl", "TCS Controller", hide_children_for_print=True
)
Expand Down Expand Up @@ -290,3 +289,49 @@ def test_printout_6(self):
f"└── ping{os.linesep}"
),
)

def test_printout_suppressed_leaves(self):
self.base_tree()
self.cmd_tree.children["acs"].add_child(
CmdTreeNode("acs_ctrl", "ACS Controller")
)
mgm_0_node = CmdTreeNode("mgm_0", "MGM 0")
self.cmd_tree.children["acs"].add_child(mgm_0_node)
update_cfg = CmdTreeNode("update_cfg", "Update Configuration")
self.cmd_tree.children["acs"]["mgm_0"].add_child(update_cfg)
tcs_ctrl = CmdTreeNode(
"tcs_ctrl", "TCS Controller", hide_children_for_print=True
)
tcs_ctrl.add_child(CmdTreeNode("set_param", "Set Parameter"))
self.cmd_tree.children["tcs"].add_child(tcs_ctrl)
pt1000_node = CmdTreeNode("pt1000_0", "PT1000 0", hide_children_for_print=True)
pt1000_node.add_child(CmdTreeNode("set_mode", "Set Mode"))
self.cmd_tree.children["tcs"].add_child(pt1000_node)
self.cmd_tree.children["tcs"].add_child(CmdTreeNode("heaters", "Heaters"))
self.cmd_tree.add_child(CmdTreeNode("ping", "Ping Command"))
self.cmd_tree.children["acs"].hide_children_which_are_leaves = True
self.cmd_tree.children["tcs"].hide_children_which_are_leaves = True
self.cmd_tree["ping"].hide_children_which_are_leaves = True
self.cmd_tree["ping"].add_child(CmdTreeNode("event", "Event Test"))
self.cmd_tree["ping"]["event"].add_child(CmdTreeNode("0", "Event 0"))

print(self.cmd_tree)
self.assertEqual(
str(self.cmd_tree),
(
f"/{os.linesep}"
f"├── acs{os.linesep}"
f"│ ├── mgm_0{os.linesep}"
f"│ │ └── update_cfg{os.linesep}"
f"│ └── ... (cut-off, leaves are hidden){os.linesep}"
f"├── tcs{os.linesep}"
f"│ ├── tcs_ctrl{os.linesep}"
f"│ │ └── ... (cut-off, children are hidden){os.linesep}"
f"│ ├── pt1000_0{os.linesep}"
f"│ │ └── ... (cut-off, children are hidden){os.linesep}"
f"│ └── ... (cut-off, leaves are hidden){os.linesep}"
f"└── ping{os.linesep}"
f" └── event{os.linesep}"
f" └── 0{os.linesep}"
),
)
75 changes: 61 additions & 14 deletions tmtccmd/config/tmtc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import enum
import copy
import os
from typing import Any, Dict, List, Optional, Set, Tuple, Union

Expand Down Expand Up @@ -47,12 +48,14 @@ def __init__(
description: str,
parent: Optional[CmdTreeNode] = None,
hide_children_for_print: bool = False,
hide_children_which_are_leaves: bool = False,
) -> None:
self.name = name
self.description = description
self.parent: Optional[CmdTreeNode] = parent
self.children: Dict[str, CmdTreeNode] = {}
self.hide_children_for_print = hide_children_for_print
self.hide_children_which_are_leaves = hide_children_which_are_leaves

@classmethod
def root_node(cls) -> CmdTreeNode:
Expand All @@ -65,6 +68,10 @@ def add_child(self, child: CmdTreeNode):
child.parent = self
self.children.update({child.name: child})

def is_leaf(self) -> bool:
"""A leaf is a node which has no children."""
return len(self.children) == 0

def contains_path(self, path: str) -> bool:
"""Check whether a full slash separated command path is contained within
the command tree."""
Expand Down Expand Up @@ -179,30 +186,70 @@ def _handle_children_printout(
depth=depth_info.depth + 1,
last_child=False,
max_depth=depth_info.max_depth,
layer_is_last_set=depth_info.set_of_layers_where_child_is_last,
layer_is_last_set=copy.copy(depth_info.set_of_layers_where_child_is_last),
)
if self.hide_children_for_print and len(self.children) > 0:
if len(self.children) == 1:
child_depth_info.last_child = True
child_depth_info.set_layer_is_last_child(depth_info.depth)
line = CmdTreeNode._create_leading_tree_part(child_depth_info)
line += f"... (cut-off, children are hidden){os.linesep}"
string += line
CmdTreeNode._this_is_the_last_child(child_depth_info, depth_info)
string += CmdTreeNode._create_supressed_children_line(
child_depth_info, "children are hidden"
)
else:
if self.hide_children_which_are_leaves:
str_if_there_are_leaves = (
self._handle_children_output_suppressed_leaves(
with_description, depth_info, child_depth_info
)
)
if str_if_there_are_leaves is not None:
return string + str_if_there_are_leaves
for idx, child in enumerate(self.children.values()):
if idx == len(self.children) - 1:
child_depth_info.last_child = True
child_depth_info.set_layer_is_last_child(depth_info.depth)
CmdTreeNode._this_is_the_last_child(child_depth_info, depth_info)
# Use recursion here to get the string for the subtree.
string += child.__str_for_depth(with_description, child_depth_info)
# The layer set is reused during the whole recursion, when we're going up the recursion
# again, we need to clear the set.
if depth_info.last_child and depth_info.is_layer_for_last_child(
depth_info.depth - 1
):
depth_info.clear_layer_is_last_child(depth_info.depth - 1)
return string

def _handle_children_output_suppressed_leaves(
self,
with_description: bool,
depth_info: DepthInfo,
child_depth_info: DepthInfo,
) -> Optional[str]:
string = ""
children_which_are_not_leaves = []
some_children_which_are_leaves = False
# The children need to be sorted first: Children which are not leaves come first.
for child in self.children.values():
if child.is_leaf():
some_children_which_are_leaves = True
continue
children_which_are_not_leaves.append(child.name)
if not some_children_which_are_leaves:
return None
for child in children_which_are_not_leaves:
string += self.children[child].__str_for_depth(
with_description, child_depth_info
)
CmdTreeNode._this_is_the_last_child(child_depth_info, depth_info)
string += CmdTreeNode._create_supressed_children_line(
child_depth_info, "leaves are hidden"
)
return string

@staticmethod
def _this_is_the_last_child(child_depth_info: DepthInfo, depth_info: DepthInfo):
child_depth_info.last_child = True
child_depth_info.set_layer_is_last_child(depth_info.depth)

@staticmethod
def _create_supressed_children_line(
child_depth_info: DepthInfo, reason: str
) -> str:
line = CmdTreeNode._create_leading_tree_part(child_depth_info)
line += f"... (cut-off, {reason}){os.linesep}"
return line

def __str__(self) -> str:
return self.str_for_tree(False)

Expand Down