Skip to content

Parse

ntc_templates.parse

ntc_templates.parse.

ParsingException

Bases: Exception

Error that is raised when TextFSM hits an Error state.

Source code in ntc_templates/parse.py
class ParsingException(Exception):
    """Error that is raised when TextFSM hits an `Error` state."""

parse_output(platform=None, command=None, data=None)

Return the structured data based on the output from a network device.

Source code in ntc_templates/parse.py
def parse_output(platform=None, command=None, data=None):
    """Return the structured data based on the output from a network device."""
    if not HAS_CLITABLE:
        msg = """
The TextFSM library is not currently supported on Windows. If you are NOT using Windows
you should be able to 'pip install textfsm' to fix this issue. If you are using Windows
then you will need to install the patch referenced here:

https://github.com/google/textfsm/pull/82

"""
        raise ImportError(msg)

    template_dir = _get_template_dir()
    cli_table = clitable.CliTable("index", template_dir)

    attrs = {"Command": command, "Platform": platform}
    try:
        cli_table.ParseCmd(data, attrs)
        structured_data = _clitable_to_dict(cli_table)
    except clitable.CliTableError as err:
        raise ParsingException(f'Unable to parse command "{command}" on platform {platform} - {str(err)}') from err
        # Invalid or Missing template
        # module.fail_json(msg='parsing error', error=str(e))
        # rather than fail, fallback to return raw text
        # structured_data = [data]

    return structured_data