From 7feaea9553a81ef24931b804226881774f7d61cf Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Thu, 19 Nov 2020 13:32:59 -0600 Subject: ifupdown-ng NMSA: Make all keys case-insensitive --- ncserver/module/nms_ifupdownng.py | 58 ++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 22 deletions(-) (limited to 'ncserver/module/nms_ifupdownng.py') diff --git a/ncserver/module/nms_ifupdownng.py b/ncserver/module/nms_ifupdownng.py index b4e1c1f..70fadd9 100644 --- a/ncserver/module/nms_ifupdownng.py +++ b/ncserver/module/nms_ifupdownng.py @@ -88,7 +88,7 @@ def _save_unlocked(): buf = "iface " + iface + "\n" for item in _CONFIG[iface]: for key, val in item.items(): - if key == 'auto' and \ + if key.casefold() == 'auto'.casefold() and \ (val is True or val.lower()[0] == 't'): buf = "auto " + iface + "\n" + buf continue @@ -118,8 +118,9 @@ def _find_one(iface: str, key: str): return None for item in _CONFIG[iface]: - if key in item.keys(): - return item[key] + for candidate in item.keys(): + if key.casefold() == candidate.casefold(): + return item[candidate] return None @@ -132,12 +133,39 @@ def _find_many(iface: str, key: str) -> list: return None for item in _CONFIG[iface]: - if key in item.keys(): - ret.append(item[key]) + for candidate in item.keys(): + if key.casefold() == candidate.casefold(): + ret.append(item[candidate]) return ret +def _replace_one(iface: str, key: str, value: str): + """Replace a single instance of +key+ for +iface+ with +value+.""" + iface_cfg = _CONFIG[iface] + for item in iface_cfg: + for candidate in item.keys(): + if key.casefold() == candidate.casefold(): + item[candidate] = value + _save() + return + + iface_cfg.append({key: value}) + _save() + return + + +def _remove_one(iface: str, key: str): + """Remove a single instance of +key+ from +iface+.""" + iface_cfg = _CONFIG[iface] + for item in iface_cfg: + for candidate in item.keys(): + if key.casefold() == candidate.casefold(): + iface_cfg.remove(item) + _save() + return + + ############################# # P A R A M E T E R S # ############################# @@ -145,31 +173,17 @@ def _find_many(iface: str, key: str) -> list: def get_desc(iface: str): """Retrieve the description for the specified interface.""" - return _find_one(iface, "netconf-description") + return _find_one(iface, 'netconf-description') def set_desc(iface: str, value: str): """Set the description for the specified interface.""" - iface_cfg = _CONFIG[iface] - for item in iface_cfg: - if 'netconf-description' in item.keys(): - item['netconf-description'] = value - _save() - return - - iface_cfg.append({'netconf-description': value}) - _save() - return + _replace_one(iface, 'netconf-description', value) def unset_desc(iface: str): """Unset the description for the specified interface.""" - iface_cfg = _CONFIG[iface] - for item in iface_cfg: - if 'netconf-description' in item.keys(): - iface_cfg.remove(item) - _save() - return + _remove_one(iface, 'netconf-description') _PARAMETERS = { -- cgit v1.2.3-60-g2f50