From 150c40ba798d2caaef89343828559aaa30976b6b Mon Sep 17 00:00:00 2001 From: angely-dev Date: Mon, 13 Mar 2023 10:55:36 +0100 Subject: [PATCH 1/3] Add contextual diff computation mode --- napalm_huawei_vrp/huawei_vrp.py | 15 ++++++++++++++- requirements.txt | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/napalm_huawei_vrp/huawei_vrp.py b/napalm_huawei_vrp/huawei_vrp.py index ea03d3e..6d3a0f5 100644 --- a/napalm_huawei_vrp/huawei_vrp.py +++ b/napalm_huawei_vrp/huawei_vrp.py @@ -32,6 +32,7 @@ import napalm.base.constants as c from datetime import datetime +from diffplus import IndentedConfig, IncrementalDiff from napalm.base import NetworkDriver from napalm.base.netmiko_helpers import netmiko_args from napalm.base.exceptions import ( @@ -136,6 +137,9 @@ def __init__(self, hostname, username, password, timeout=60, optional_args=None) # Track whether 'file prompt quiet' is known to be configured self.prompt_quiet_configured = None + # Contextual diff computation mode + self.contextual_diff = optional_args.get('contextual_diff', False) + # verified def open(self): """Open a connection to the device. @@ -473,6 +477,8 @@ def compare_config(self): """Compare candidate config with running.""" if self.loaded: if not self.replace: + if self.contextual_diff: + return self._get_contextual_diff() return self._get_merge_diff() # return self.merge_candidate diff = self._get_diff(self.replace_file.split('/')[-1]) @@ -1849,6 +1855,13 @@ def _get_merge_diff(self): diff.append(line) return '\n'.join(diff) + def _get_contextual_diff(self): + running_config = self.get_config(retrieve='running')['running'] + running_config = IndentedConfig(running_config, sanitize=True) + merge_candidate = IndentedConfig(self.merge_candidate, sanitize=True) + diff = IncrementalDiff(merge_candidate, running_config) + return str(diff) + def _get_diff(self, filename=None): """Get a diff between running config and a proposed file.""" if filename is None: @@ -2025,4 +2038,4 @@ def interface_bw_conversion(bandwidth): raise ValueError( "BW Conversion: Unexpected Bandwidth for GigabitEthernet: {}".format(bandwidth) - ) \ No newline at end of file + ) diff --git a/requirements.txt b/requirements.txt index 8fb310a..0f6cac1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ napalm>=3.0.0 +diffplus From a8a8bf73e63673dd9f651113a286214edda6f1a3 Mon Sep 17 00:00:00 2001 From: angely-dev Date: Sat, 9 Dec 2023 15:36:22 +0100 Subject: [PATCH 2/3] Reformat huawei_vrp.py using Black code formatter --- napalm_huawei_vrp/huawei_vrp.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/napalm_huawei_vrp/huawei_vrp.py b/napalm_huawei_vrp/huawei_vrp.py index e7825ba..fc8fc80 100644 --- a/napalm_huawei_vrp/huawei_vrp.py +++ b/napalm_huawei_vrp/huawei_vrp.py @@ -139,7 +139,7 @@ def __init__(self, hostname, username, password, timeout=60, optional_args=None) self.prompt_quiet_configured = None # Contextual diff computation mode - self.contextual_diff = optional_args.get('contextual_diff', False) + self.contextual_diff = optional_args.get("contextual_diff", False) # verified def open(self): @@ -2078,7 +2078,7 @@ def _get_merge_diff(self): return "\n".join(diff) def _get_contextual_diff(self): - running_config = self.get_config(retrieve='running')['running'] + running_config = self.get_config(retrieve="running")["running"] running_config = IndentedConfig(running_config, sanitize=True) merge_candidate = IndentedConfig(self.merge_candidate, sanitize=True) diff = IncrementalDiff(merge_candidate, running_config) @@ -2271,4 +2271,4 @@ def interface_bw_conversion(bandwidth): "BW Conversion: Unexpected Bandwidth for GigabitEthernet: {}".format( bandwidth ) - ) \ No newline at end of file + ) From 4877340e7f1c64983048e2e853231228f7b9e5e1 Mon Sep 17 00:00:00 2001 From: angely-dev Date: Sat, 9 Dec 2023 16:38:18 +0100 Subject: [PATCH 3/3] Ignore mypy missing imports for diffplus module --- mypy.ini | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mypy.ini b/mypy.ini index 5c86515..29d795d 100644 --- a/mypy.ini +++ b/mypy.ini @@ -47,3 +47,6 @@ ignore_missing_imports = True [mypy-pytest] ignore_missing_imports = True + +[mypy-diffplus] +ignore_missing_imports = True