refactor to add a node outside of the main tree builder

This commit is contained in:
Brian S. Stephan 2023-07-01 12:24:31 -05:00
parent 7aee99ef4f
commit 71f6af624e
Signed by: bss
GPG Key ID: 3DE06D3180895FCB

View File

@ -151,7 +151,27 @@ class ConfigEditor(App):
self.config = get_config_from_file(self.config_filename, whole_board=self.whole_board)
tree = self.query_one(Tree)
def add_node(parent_node: TreeNode, parent_config: Message,
tree.root.data = (None, self.config.DESCRIPTOR)
tree.root.set_label(self.config_filename)
for field_descriptor, field_value in sorted(self.config.ListFields(), key=lambda f: f[0].name):
ConfigEditor._add_node(tree.root, self.config, field_descriptor, field_value)
tree.root.expand()
def on_tree_node_selected(self, node_event: Tree.NodeSelected) -> None:
"""Take the appropriate action for this type of node."""
self._modify_node(node_event.node)
def action_save(self) -> None:
"""Save the configuration."""
write_new_config_to_filename(self.config, self.config_filename, inject=self.whole_board)
self.push_screen(MessageScreen(f"Configuration saved to {self.config_filename}."))
def action_quit(self) -> None:
"""Quit the application."""
self.exit()
@staticmethod
def _add_node(parent_node: TreeNode, parent_config: Message,
field_descriptor: descriptor.FieldDescriptor, field_value: object) -> None:
"""Add a node to the overall tree, recursively.
@ -167,34 +187,25 @@ class ConfigEditor(App):
this_node.set_label(pb_field_to_node_label(field_descriptor, field_value))
if field_descriptor.type == descriptor.FieldDescriptor.TYPE_MESSAGE:
# a message has stuff under it, recurse into it
this_config = getattr(parent_config, field_descriptor.name)
if hasattr(field_value, 'add'):
# support repeated
for child in field_value:
ConfigEditor._add_node(this_node, this_config, child.DESCRIPTOR, child)
else:
# a message has stuff under it, recurse into it
missing_fields = [f for f in field_value.DESCRIPTOR.fields
if f not in [fp for fp, vp in field_value.ListFields()]]
for child_field_descriptor, child_field_value in sorted(field_value.ListFields(),
key=lambda f: f[0].name):
add_node(this_node, this_config, child_field_descriptor, child_field_value)
ConfigEditor._add_node(this_node, this_config, child_field_descriptor, child_field_value)
for child_field_descriptor in sorted(missing_fields, key=lambda f: f.name):
ConfigEditor._add_node(this_node, this_config, child_field_descriptor,
getattr(this_config, child_field_descriptor.name))
else:
# leaf node, stop here
this_node.allow_expand = False
tree.root.data = (None, self.config.DESCRIPTOR)
tree.root.set_label(self.config_filename)
for field_descriptor, field_value in sorted(self.config.ListFields(), key=lambda f: f[0].name):
add_node(tree.root, self.config, field_descriptor, field_value)
tree.root.expand()
def on_tree_node_selected(self, node_event: Tree.NodeSelected) -> None:
"""Take the appropriate action for this type of node."""
self._modify_node(node_event.node)
def action_save(self) -> None:
"""Save the configuration."""
write_new_config_to_filename(self.config, self.config_filename, inject=self.whole_board)
self.push_screen(MessageScreen(f"Configuration saved to {self.config_filename}."))
def action_quit(self) -> None:
"""Quit the application."""
self.exit()
def _modify_node(self, node: TreeNode) -> None:
"""Modify the selected node by context of what type of config item it is."""
parent_config, field_descriptor = node.data