Skip to content

Commit 451c013

Browse files
committed
Refactored combine function and added comments.
1 parent 53aed63 commit 451c013

File tree

1 file changed

+26
-35
lines changed

1 file changed

+26
-35
lines changed

src/cisco_gnmi/client.py

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -419,43 +419,35 @@ def parse_xpath_to_gnmi_path(self, xpath, origin=None):
419419
path.elem.extend(path_elems)
420420
return path
421421

422-
def combine_configs(self, payload, last_xpath, xpath, config):
422+
def combine_configs(self, payload, last_xpath, cfg):
423423
"""Walking from end to finish 2 xpaths merge so combine them
424-
425-
|---last xpath config
426-
----|
427-
|---this xpath config
428-
424+
|--config
425+
|---last xpath config--|
426+
----| |--config
427+
|
428+
| pick these up --> |--config
429+
|---this xpath config--|
430+
|--config
429431
Parameters
430432
----------
431433
payload: dict of partial payload
432434
last_xpath: last xpath that was processed
433435
xpath: colliding xpath
434436
config: dict of values associated to colliding xpath
435437
"""
436-
last_set = set(last_xpath.split('/'))
437-
curr_diff = set(xpath.split('/')) - last_set
438-
if len(curr_diff) > 1:
439-
print('combine_configs() error1')
440-
return payload
441-
index = curr_diff.pop()
442-
curr_xpath = xpath[xpath.find(index):]
443-
curr_xpath = curr_xpath.split('/')
444-
curr_xpath.reverse()
445-
for seg in curr_xpath:
446-
config = {seg: config}
447-
448-
last_diff = last_set - set(xpath.split('/'))
449-
if len(last_diff) > 1:
450-
print('combine_configs() error2')
451-
return payload
452-
last_xpath = last_xpath[last_xpath.find(last_diff.pop()):]
453-
last_xpath = last_xpath.split('/')
454-
last_xpath.reverse()
455-
for seg in last_xpath:
456-
if seg not in payload:
457-
payload = {seg: payload}
458-
payload.update(config)
438+
xpath, config, is_key = cfg
439+
lp = last_xpath.split('/')
440+
xp = xpath.split('/')
441+
base = []
442+
top = ''
443+
for i, seg in enumerate(zip(lp, xp)):
444+
if seg[0] != seg[1]:
445+
top = seg[1]
446+
break
447+
base = '/' + '/'.join(xp[i:])
448+
cfg = (base, config, False)
449+
extended_payload = {top: self.xpath_to_json([cfg])}
450+
payload.update(extended_payload)
459451
return payload
460452

461453
def xpath_to_json(self, configs, last_xpath='', payload={}):
@@ -474,11 +466,11 @@ def xpath_to_json(self, configs, last_xpath='', payload={}):
474466
for i, cfg in enumerate(configs, 1):
475467
xpath, config, is_key = cfg
476468
if last_xpath and xpath not in last_xpath:
477-
# Branched config here
478-
# |---last xpath config
479-
# --|
469+
# Branched config here |---config
470+
# |---last xpath config--|
471+
# --| |---config
480472
# |---this xpath config
481-
payload = self.combine_configs(payload, last_xpath, xpath, config)
473+
payload = self.combine_configs(payload, last_xpath, cfg)
482474
return self.xpath_to_json(configs[i:], xpath, payload)
483475
xpath_segs = xpath.split('/')
484476
xpath_segs.reverse()
@@ -733,7 +725,6 @@ def xml_path_to_path_elem(self, request):
733725
if __name__ == '__main__':
734726
from pprint import pprint as pp
735727
import grpc
736-
from cisco_gnmi import Client
737728
from cisco_gnmi.auth import CiscoAuthPlugin
738729
channel = grpc.secure_channel(
739730
'127.0.0.1:9339',
@@ -790,7 +781,7 @@ def xml_path_to_path_elem(self, request):
790781
}
791782
]
792783
}
793-
modules, message, origin = client.xpath_to_path_elem(request)
784+
modules, message, origin = client.xml_path_to_path_elem(request)
794785
pp(modules)
795786
pp(message)
796787
pp(origin)

0 commit comments

Comments
 (0)