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)
This commit is contained in:
Brian S. Stephan 2023-07-01 13:00:48 -05:00
parent 71f6af624e
commit c60a5e784d
Signed by: bss
GPG Key ID: 3DE06D3180895FCB
2 changed files with 7 additions and 6 deletions

View File

@ -29,7 +29,7 @@ class EditScreen(ModalScreen):
"""Save the config field info for later usage.""" """Save the config field info for later usage."""
logger.debug("constructing EditScreen for %s", node.label) logger.debug("constructing EditScreen for %s", node.label)
self.node = node self.node = node
parent_config, field_descriptor = node.data parent_config, field_descriptor, _ = node.data
self.parent_config = parent_config self.parent_config = parent_config
self.field_descriptor = field_descriptor self.field_descriptor = field_descriptor
self.field_value = field_value 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) self.config = get_config_from_file(self.config_filename, whole_board=self.whole_board)
tree = self.query_one(Tree) 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) tree.root.set_label(self.config_filename)
for field_descriptor, field_value in sorted(self.config.ListFields(), key=lambda f: f[0].name): 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) 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 # all nodes relate to their parent and retain info about themselves
this_node = parent_node.add("") 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)) this_node.set_label(pb_field_to_node_label(field_descriptor, field_value))
if field_descriptor.type == descriptor.FieldDescriptor.TYPE_MESSAGE: if field_descriptor.type == descriptor.FieldDescriptor.TYPE_MESSAGE:
@ -208,7 +208,7 @@ class ConfigEditor(App):
def _modify_node(self, node: TreeNode) -> None: def _modify_node(self, node: TreeNode) -> None:
"""Modify the selected node by context of what type of config item it is.""" """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 # don't do anything special with selecting expandable nodes, since the framework already expands them
if (isinstance(field_descriptor, descriptor.Descriptor) or if (isinstance(field_descriptor, descriptor.Descriptor) or
@ -222,7 +222,7 @@ class ConfigEditor(App):
field_value = not field_value field_value = not field_value
logger.debug("...to %s", field_value) logger.debug("...to %s", field_value)
setattr(parent_config, field_descriptor.name, 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)) node.set_label(pb_field_to_node_label(field_descriptor, field_value))
logger.debug(self.config) logger.debug(self.config)
else: else:

View File

@ -20,9 +20,10 @@ async def test_simple_tree_building():
async with app.run_test() as pilot: async with app.run_test() as pilot:
check_node = pilot.app.query_one(Tree).root.children[2] check_node = pilot.app.query_one(Tree).root.children[2]
assert "boardVersion = 'v0.7.2'" in check_node.label 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 parent_config == pilot.app.config
assert field_descriptor == pilot.app.config.DESCRIPTOR.fields_by_name['boardVersion'] assert field_descriptor == pilot.app.config.DESCRIPTOR.fields_by_name['boardVersion']
assert field_value == 'v0.7.2'
@pytest.mark.asyncio @pytest.mark.asyncio