From 62966a0e060a094cb89d96efa7a8fe9c00605a5b Mon Sep 17 00:00:00 2001 From: kat-git-hub Date: Tue, 1 Sep 2026 13:48:18 +0200 Subject: [PATCH] Fix find_children_w_parents to only include actual descendants of matched child (#836) --- changes/836.fixed.md | 1 + netutils/config/parser.py | 24 +++++++++---------- .../cisco_ios/flat_siblings_args.json | 5 ++++ .../cisco_ios/flat_siblings_received.txt | 2 ++ .../cisco_ios/flat_siblings_sent.txt | 5 ++++ .../cisco_ios/ios_full_received.txt | 1 - 6 files changed, 24 insertions(+), 14 deletions(-) create mode 100644 changes/836.fixed.md create mode 100644 tests/unit/mock/config/parser/find_children_w_parents/cisco_ios/flat_siblings_args.json create mode 100644 tests/unit/mock/config/parser/find_children_w_parents/cisco_ios/flat_siblings_received.txt create mode 100644 tests/unit/mock/config/parser/find_children_w_parents/cisco_ios/flat_siblings_sent.txt diff --git a/changes/836.fixed.md b/changes/836.fixed.md new file mode 100644 index 00000000..92632240 --- /dev/null +++ b/changes/836.fixed.md @@ -0,0 +1 @@ +Fixed `find_children_w_parents` to only return the matched child line and its actual descendants, instead of every sibling line under the same top-level parent. diff --git a/netutils/config/parser.py b/netutils/config/parser.py index 09c9f6e0..334fd3b6 100644 --- a/netutils/config/parser.py +++ b/netutils/config/parser.py @@ -416,20 +416,18 @@ def find_children_w_parents( >>> print(bgp_conf) [' address-family ipv4 unicast', ' neighbor 192.168.1.2 activate', ' network 172.17.1.0 mask'] """ - config = [] - potential_parents = [ - elem.parents[0] - for elem in self.build_config_relationship() + relationships = self.build_config_relationship() + matched_lines = { + elem.config_line + for elem in relationships if self._match_type_check(elem.config_line, child_pattern, match_type) - ] - for cfg_line in self.build_config_relationship(): - parents = cfg_line.parents[0] if cfg_line.parents else None - if parents in potential_parents and self._match_type_check( - parents, # type: ignore[arg-type] - parent_pattern, - match_type, - ): - config.append(cfg_line.config_line) + and elem.parents + and self._match_type_check(elem.parents[0], parent_pattern, match_type) + } + config = [] + for elem in relationships: + if elem.config_line in matched_lines or any(parent in matched_lines for parent in elem.parents): + config.append(elem.config_line) return config diff --git a/tests/unit/mock/config/parser/find_children_w_parents/cisco_ios/flat_siblings_args.json b/tests/unit/mock/config/parser/find_children_w_parents/cisco_ios/flat_siblings_args.json new file mode 100644 index 00000000..3230e500 --- /dev/null +++ b/tests/unit/mock/config/parser/find_children_w_parents/cisco_ios/flat_siblings_args.json @@ -0,0 +1,5 @@ +{ + "parent_pattern": "context local", + "child_pattern": " logging", + "match_type": "regex" +} diff --git a/tests/unit/mock/config/parser/find_children_w_parents/cisco_ios/flat_siblings_received.txt b/tests/unit/mock/config/parser/find_children_w_parents/cisco_ios/flat_siblings_received.txt new file mode 100644 index 00000000..8070ddaf --- /dev/null +++ b/tests/unit/mock/config/parser/find_children_w_parents/cisco_ios/flat_siblings_received.txt @@ -0,0 +1,2 @@ + logging something + logging something else \ No newline at end of file diff --git a/tests/unit/mock/config/parser/find_children_w_parents/cisco_ios/flat_siblings_sent.txt b/tests/unit/mock/config/parser/find_children_w_parents/cisco_ios/flat_siblings_sent.txt new file mode 100644 index 00000000..6419896e --- /dev/null +++ b/tests/unit/mock/config/parser/find_children_w_parents/cisco_ios/flat_siblings_sent.txt @@ -0,0 +1,5 @@ +context local + something not logging + logging something + logging something else + something else not logging diff --git a/tests/unit/mock/config/parser/find_children_w_parents/cisco_ios/ios_full_received.txt b/tests/unit/mock/config/parser/find_children_w_parents/cisco_ios/ios_full_received.txt index e7a4b1be..4fe99aa6 100644 --- a/tests/unit/mock/config/parser/find_children_w_parents/cisco_ios/ios_full_received.txt +++ b/tests/unit/mock/config/parser/find_children_w_parents/cisco_ios/ios_full_received.txt @@ -1,4 +1,3 @@ - contact-email-addr sch-smart-licensing@cisco.com profile "CiscoTAC-1" active destination transport-method http \ No newline at end of file