TTP Templates
ttp-templates is a Python library that provides a curated collection of Text Template Parser (TTP) templates, primarily focused on parsing network device CLI output. It extends the core TTP parser by offering an easy way to access and apply pre-built templates for various vendors and commands, including "getter" templates for normalized data. The current version is 0.5.1, with releases occurring periodically to add new templates and features or address issues.
Common errors
-
ValueError: No template-locating argument provided. Please specify 'template_name', 'template_path', or 'template_string'.
cause Attempting to call `TTPTemplates().parse_output()` without any argument to specify which template to use (e.g., no `template_name`, `template_path`, or `template_string`).fixProvide a valid template identifier, for example: `parser.parse_output(data=my_data, template_name='misc/N2G/cli_ip_data/arista_eos.txt')` -
ModuleNotFoundError: No module named 'ttp_templates'
cause The `ttp-templates` Python package is not installed in the current environment.fixInstall the package using pip: `pip install ttp-templates` -
TTPTemplates: failed to find template 'your_template_name.txt'
cause The specified `template_name` does not correspond to any bundled template, or the path is incorrect for a custom template.fixVerify the `template_name` against the list of available templates (e.g., using `TTPTemplates().list_templates()`) or ensure `template_path` points to an existing file.
Warnings
- breaking Calling `parse_output` without a valid template-locating argument (e.g., `template_name`, `template_path`, `template_string`) now raises a `ValueError`. Previously, it silently forwarded `None` to the TTP constructor, leading to confusing downstream errors.
- breaking The `get` results return now includes only the parsing result for the matched template. This streamlines the output but might change the structure if prior versions returned additional metadata around the result.
- gotcha While `ttp-templates` installs the underlying `ttp` parser library automatically, users often confuse the two. `ttp-templates` provides the template files and a simplified API to access them, but `ttp` is the core engine for parsing.
Install
-
pip install ttp-templates
Imports
- TTPTemplates
from ttp_templates import TTPTemplates
Quickstart
from ttp_templates import TTPTemplates
# Initialize TTPTemplates
parser = TTPTemplates()
# Example device output for `get/inventory.txt` on Cisco NX-OS
# This output structure is a simplified representation of `show inventory`
device_output = """
NAME: "Chassis", DESCR: "Nexus9000 C9372PX Chassis"
PID: N9K-C9372PX, VID: V01, SN: FOC12345678
NAME: "Slot 1", DESCR: "Supervisor"
PID: N9K-SUP-A, VID: V01, SN: JAE12345678
"""
# Parse the output using the 'get/inventory.txt' template
# The 'platform' argument is often crucial for getter templates to select the right internal logic
result = parser.parse_output(
data=device_output,
template_name="get/inventory.txt",
platform="cisco_nxos"
)
# Print the normalized result
# 'result' is typically a list of lists, where inner list contains dictionaries
# In v0.5.1, it returns only the parsing result for the matched template.
if result and result[0]:
print("Parsed Inventory Data:")
for item in result[0]:
print(item)
else:
print("No data parsed or template did not match.")