From c60a5e784d95e4e37b900fedde70f2a0869bb344 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Sat, 1 Jul 2023 13:00:48 -0500 Subject: [PATCH] refactor to retain field_value in case it is a message we can save some config lookups in this case (in particular in the case where we are working with repeateds and the lookup against an iterable won't work) --- gp2040ce_bintools/gui.py | 10 +++++----- tests/test_gui.py | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/gp2040ce_bintools/gui.py b/gp2040ce_bintools/gui.py index 9f86691..15ff84f 100644 --- a/gp2040ce_bintools/gui.py +++ b/gp2040ce_bintools/gui.py @@ -29,7 +29,7 @@ class EditScreen(ModalScreen): """Save the config field info for later usage.""" logger.debug("constructing EditScreen for %s", node.label) self.node = node - parent_config, field_descriptor = node.data + parent_config, field_descriptor, _ = node.data self.parent_config = parent_config self.field_descriptor = field_descriptor self.field_value = field_value @@ -151,7 +151,7 @@ class ConfigEditor(App): self.config = get_config_from_file(self.config_filename, whole_board=self.whole_board) tree = self.query_one(Tree) - tree.root.data = (None, self.config.DESCRIPTOR) + tree.root.data = (None, self.config.DESCRIPTOR, self.config) 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) @@ -183,7 +183,7 @@ class ConfigEditor(App): """ # all nodes relate to their parent and retain info about themselves this_node = parent_node.add("") - this_node.data = (parent_config, field_descriptor) + this_node.data = (parent_config, field_descriptor, field_value) this_node.set_label(pb_field_to_node_label(field_descriptor, field_value)) if field_descriptor.type == descriptor.FieldDescriptor.TYPE_MESSAGE: @@ -208,7 +208,7 @@ class ConfigEditor(App): 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 + parent_config, field_descriptor, _ = node.data # don't do anything special with selecting expandable nodes, since the framework already expands them if (isinstance(field_descriptor, descriptor.Descriptor) or @@ -222,7 +222,7 @@ class ConfigEditor(App): field_value = not field_value logger.debug("...to %s", field_value) setattr(parent_config, field_descriptor.name, field_value) - node.data = (parent_config, field_descriptor) + node.data = (parent_config, field_descriptor, field_value) node.set_label(pb_field_to_node_label(field_descriptor, field_value)) logger.debug(self.config) else: diff --git a/tests/test_gui.py b/tests/test_gui.py index 9ec900d..43be52e 100644 --- a/tests/test_gui.py +++ b/tests/test_gui.py @@ -20,9 +20,10 @@ async def test_simple_tree_building(): async with app.run_test() as pilot: check_node = pilot.app.query_one(Tree).root.children[2] assert "boardVersion = 'v0.7.2'" in check_node.label - parent_config, field_descriptor = check_node.data + parent_config, field_descriptor, field_value = check_node.data assert parent_config == pilot.app.config assert field_descriptor == pilot.app.config.DESCRIPTOR.fields_by_name['boardVersion'] + assert field_value == 'v0.7.2' @pytest.mark.asyncio