From ba03e7bc81022676aa441d7d7fc963dc175064c1 Mon Sep 17 00:00:00 2001 From: Network to Code Date: Thu, 23 Dec 2021 12:47:50 +0100 Subject: [PATCH 1/5] fix value parser --- netcompare/utils/jmspath_parsers.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/netcompare/utils/jmspath_parsers.py b/netcompare/utils/jmspath_parsers.py index 31863f1..0a47ac1 100644 --- a/netcompare/utils/jmspath_parsers.py +++ b/netcompare/utils/jmspath_parsers.py @@ -11,15 +11,12 @@ def jmspath_value_parser(path: str): Return: "result[0].vrfs.default.peerList[*].[prefixesReceived]" """ - regex_match_value = re.search(r"\$.*\$\.|\$.*\$,|,\$.*\$", path) - - if not regex_match_value: - return path + regex_match_ref_key = re.search(r"\$.*\$\.|\$.*\$,|,\$.*\$", path) + # $peers$. --> peers - regex_normalized_value = re.search(r"\$.*\$", regex_match_value.group()) - if regex_normalized_value: - normalized_value = regex_match_value.group().split("$")[1] - return path.replace(regex_normalized_value.group(), normalized_value) + if regex_match_ref_key: + reference_key = regex_match_ref_key.group() + return path.replace(reference_key, '') return path From 3e8c999072908c2e10794550a80372dbd334b158 Mon Sep 17 00:00:00 2001 From: Network to Code Date: Thu, 23 Dec 2021 14:56:22 +0100 Subject: [PATCH 2/5] fix value parser --- netcompare/utils/jmspath_parsers.py | 22 ++++++++++++++++------ tests/test_jmspath_parsers.py | 6 +++--- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/netcompare/utils/jmspath_parsers.py b/netcompare/utils/jmspath_parsers.py index 0a47ac1..6eedbc7 100644 --- a/netcompare/utils/jmspath_parsers.py +++ b/netcompare/utils/jmspath_parsers.py @@ -4,20 +4,30 @@ def jmspath_value_parser(path: str): """ - Get the JMSPath value path from 'path'. + Get the JMSPath value path from 'path'. Two combinations are possible based on wherenreference key is defined. See example below. Args: path: "result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesReceived]" + path: "result[0].$vrfs$.default.peerList[*].[peerAddress, prefixesReceived]" + Return: "result[0].vrfs.default.peerList[*].[prefixesReceived]" + "result[0].vrfs.default.peerList[*].[peerAddress, prefixesReceived]" """ regex_match_ref_key = re.search(r"\$.*\$\.|\$.*\$,|,\$.*\$", path) - - # $peers$. --> peers + s_path = path.split(".") if regex_match_ref_key: - reference_key = regex_match_ref_key.group() - return path.replace(reference_key, '') - + if re.search(r"\$.*\$\.|\$.*\$,|,\$.*\$", s_path[-1]): + # [$peerAddress$,prefixesReceived] --> [prefixesReceived] + if regex_match_ref_key: + reference_key = regex_match_ref_key.group() + return path.replace(reference_key, "") + else: + # result[0].$vrfs$.default... --> result[0].vrfs.default.... + regex_normalized_value = re.search(r"\$.*\$", regex_match_ref_key.group()) + if regex_normalized_value: + normalized_value = regex_match_ref_key.group().split("$")[1] + return path.replace(regex_normalized_value.group(), normalized_value) return path diff --git a/tests/test_jmspath_parsers.py b/tests/test_jmspath_parsers.py index 7d225f3..a54c683 100644 --- a/tests/test_jmspath_parsers.py +++ b/tests/test_jmspath_parsers.py @@ -6,15 +6,15 @@ value_parser_case_1 = ( "result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesReceived]", - "result[0].vrfs.default.peerList[*].[peerAddress,prefixesReceived]", + "result[0].vrfs.default.peerList[*].[prefixesReceived]", ) value_parser_case_2 = ( "result[0].vrfs.default.peerList[*].[peerAddress,$prefixesReceived$]", - "result[0].vrfs.default.peerList[*].[peerAddress,prefixesReceived]", + "result[0].vrfs.default.peerList[*].[peerAddress]", ) value_parser_case_3 = ( "result[0].vrfs.default.peerList[*].[interfaceCounters,$peerAddress$,prefixesReceived]", - "result[0].vrfs.default.peerList[*].[interfaceCounters,peerAddress,prefixesReceived]", + "result[0].vrfs.default.peerList[*].[interfaceCounters,prefixesReceived]", ) value_parser_case_4 = ( "result[0].$vrfs$.default.peerList[*].[peerAddress,prefixesReceived]", From 92c3099ba29e0b8bf215ff440fbb540b6f467366 Mon Sep 17 00:00:00 2001 From: Network to Code Date: Thu, 23 Dec 2021 15:33:17 +0100 Subject: [PATCH 3/5] integrate comments from PR --- netcompare/utils/jmspath_parsers.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/netcompare/utils/jmspath_parsers.py b/netcompare/utils/jmspath_parsers.py index 6eedbc7..3faa749 100644 --- a/netcompare/utils/jmspath_parsers.py +++ b/netcompare/utils/jmspath_parsers.py @@ -4,7 +4,8 @@ def jmspath_value_parser(path: str): """ - Get the JMSPath value path from 'path'. Two combinations are possible based on wherenreference key is defined. See example below. + Get the JMSPath value path from 'path'. + Two combinations are possible based on where reference key is defined. See example below. Args: path: "result[0].vrfs.default.peerList[*].[$peerAddress$,prefixesReceived]" @@ -15,9 +16,9 @@ def jmspath_value_parser(path: str): "result[0].vrfs.default.peerList[*].[peerAddress, prefixesReceived]" """ regex_match_ref_key = re.search(r"\$.*\$\.|\$.*\$,|,\$.*\$", path) - s_path = path.split(".") + path_suffix = path.split(".")[-1] if regex_match_ref_key: - if re.search(r"\$.*\$\.|\$.*\$,|,\$.*\$", s_path[-1]): + if re.search(r"\$.*\$\.|\$.*\$,|,\$.*\$", path_suffix[-1]): # [$peerAddress$,prefixesReceived] --> [prefixesReceived] if regex_match_ref_key: reference_key = regex_match_ref_key.group() From dbbd62e8231eba8eee995239064bbafbed33293b Mon Sep 17 00:00:00 2001 From: Network to Code Date: Thu, 23 Dec 2021 15:35:30 +0100 Subject: [PATCH 4/5] fix pycodestyle --- netcompare/utils/jmspath_parsers.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/netcompare/utils/jmspath_parsers.py b/netcompare/utils/jmspath_parsers.py index 3faa749..5e7a1c5 100644 --- a/netcompare/utils/jmspath_parsers.py +++ b/netcompare/utils/jmspath_parsers.py @@ -5,6 +5,7 @@ def jmspath_value_parser(path: str): """ Get the JMSPath value path from 'path'. + Two combinations are possible based on where reference key is defined. See example below. Args: @@ -18,7 +19,7 @@ def jmspath_value_parser(path: str): regex_match_ref_key = re.search(r"\$.*\$\.|\$.*\$,|,\$.*\$", path) path_suffix = path.split(".")[-1] if regex_match_ref_key: - if re.search(r"\$.*\$\.|\$.*\$,|,\$.*\$", path_suffix[-1]): + if re.search(r"\$.*\$\.|\$.*\$,|,\$.*\$", path_suffix): # [$peerAddress$,prefixesReceived] --> [prefixesReceived] if regex_match_ref_key: reference_key = regex_match_ref_key.group() From c145a57348351502daae88e9fe776b4f2d7e97ca Mon Sep 17 00:00:00 2001 From: Network to Code Date: Thu, 23 Dec 2021 16:00:05 +0100 Subject: [PATCH 5/5] integrate comments from PR --- netcompare/utils/jmspath_parsers.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/netcompare/utils/jmspath_parsers.py b/netcompare/utils/jmspath_parsers.py index 5e7a1c5..4be6fe6 100644 --- a/netcompare/utils/jmspath_parsers.py +++ b/netcompare/utils/jmspath_parsers.py @@ -16,20 +16,21 @@ def jmspath_value_parser(path: str): "result[0].vrfs.default.peerList[*].[prefixesReceived]" "result[0].vrfs.default.peerList[*].[peerAddress, prefixesReceived]" """ - regex_match_ref_key = re.search(r"\$.*\$\.|\$.*\$,|,\$.*\$", path) + regex_ref_key = re.compile(r"\$.*\$\.|\$.*\$,|,\$.*\$") + regex_match_ref_key = regex_ref_key.search(path) path_suffix = path.split(".")[-1] + if regex_match_ref_key: - if re.search(r"\$.*\$\.|\$.*\$,|,\$.*\$", path_suffix): + if regex_ref_key.search(path_suffix): # [$peerAddress$,prefixesReceived] --> [prefixesReceived] - if regex_match_ref_key: - reference_key = regex_match_ref_key.group() - return path.replace(reference_key, "") - else: - # result[0].$vrfs$.default... --> result[0].vrfs.default.... - regex_normalized_value = re.search(r"\$.*\$", regex_match_ref_key.group()) - if regex_normalized_value: - normalized_value = regex_match_ref_key.group().split("$")[1] - return path.replace(regex_normalized_value.group(), normalized_value) + reference_key = regex_match_ref_key.group() + return path.replace(reference_key, "") + + # result[0].$vrfs$.default... --> result[0].vrfs.default.... + regex_normalized_value = re.search(r"\$.*\$", regex_match_ref_key.group()) + if regex_normalized_value: + normalized_value = regex_match_ref_key.group().split("$")[1] + return path.replace(regex_normalized_value.group(), normalized_value) return path