Skip to content

Commit 3de1609

Browse files
committed
Convenient wrappers for system formatted ASCII text (CLI) - IOS-XR
1 parent df05d81 commit 3de1609

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/cisco_gnmi/client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,19 @@ def parse_xpath_to_gnmi_path(self, xpath, origin=None):
334334
raise Exception("Unfinished elements in XPath parsing!")
335335
path.elem.extend(path_elems)
336336
return path
337+
338+
def parse_cli_command_to_gnmi_path(self, cli_command):
339+
"""Parses a CLI command to proto.gnmi_pb2.Path.
340+
This function should be overridden by any child classes for origin logic.
341+
342+
The CLI command becomes a path element.
343+
"""
344+
if not isinstance(cli_command, string_types):
345+
raise Exception("cli_command must be a string!")
346+
347+
path = proto.gnmi_pb2.Path()
348+
curr_elem = proto.gnmi_pb2.PathElem()
349+
curr_elem.name = cli_command
350+
path.elem.extend([curr_elem])
351+
352+
return path

src/cisco_gnmi/xr.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,42 @@ def parse_xpath_to_gnmi_path(self, xpath, origin=None):
342342
# module name
343343
origin, xpath = xpath.split(":", 1)
344344
return super(XRClient, self).parse_xpath_to_gnmi_path(xpath, origin)
345+
346+
def get_cli_commands(self, cli_commands):
347+
"""A convenience wrapper for get() which forms proto.gnmi_pb2.Path from supplied CLI commands.
348+
349+
Parameters
350+
----------
351+
cli_commands : iterable of str or str
352+
An iterable of CLI commands as strings to request data of
353+
If simply a str, wraps as a list for convenience
354+
355+
Returns
356+
-------
357+
get()
358+
"""
359+
gnmi_path = None
360+
if isinstance(cli_commands, (list, set)):
361+
gnmi_path = map(self.parse_cli_command_to_gnmi_path, set(cli_commands))
362+
elif isinstance(cli_commands, string_types):
363+
gnmi_path = [self.parse_cli_command_to_gnmi_path(cli_commands)]
364+
else:
365+
raise Exception(
366+
"cli_commands must be a single CLI command string or iterable of CLI commands as strings!"
367+
)
368+
return self.get(gnmi_path, encoding='ASCII')
369+
370+
def parse_cli_command_to_gnmi_path(self, cli_command):
371+
"""A convenience wrapper for parse_cli_command_to_gnmi_path() that creates the proto.gnmi_pb2.Path for the
372+
supplied CLI command.
373+
374+
Parameters
375+
----------
376+
cli_command : str
377+
A CLI command as str
378+
379+
Returns
380+
-------
381+
parse_cli_command_to_gnmi_path()
382+
"""
383+
return super(XRClient, self).parse_cli_command_to_gnmi_path(cli_command)

0 commit comments

Comments
 (0)