Template Text Parser (TTP)
TTP is a Python library designed for fast and flexible parsing of semi-structured text data using templates. It was initially developed for processing CLI output from network devices but is versatile enough for any text with repetitive patterns. TTP offers features such as groups for results hierarchy, dynamic regex parsing, on-the-fly data processing with built-in or custom functions (macros), various output formats, and an input system. The library is actively maintained, with its latest version being 0.10.1.
Warnings
- breaking Python 2.7 support has been removed, and the minimum required Python version is now 3.9.
- breaking The 'input sources' feature, including Nornir and Netmiko sources, was removed in version 0.9.0. This was due to better integration methods with Netmiko and updates to Nornir.
- gotcha When using `lookup` functions where one group's results are used for a lookup in another group, those groups must process separate inputs. TTP combines results on a per-input basis, which can lead to unexpected behavior if lookups cross shared input boundaries.
- gotcha Output results often come as deeply nested lists, which can be unintuitive for new users. For example, `parser.result()` returns `[[[...]]]`, requiring indexing like `parser.result()[0]` or `parser.result(format='json')[0]` to access the main results.
Install
-
pip install ttp -
pip install ttp[full]
Imports
- ttp
from ttp import ttp
Quickstart
from ttp import ttp
import pprint
data = """
interface Loopback0
description Router-id-loopback
ip address 192.168.0.113/24
!
interface Vlan778
description CPE_Acces_Vlan
ip address 2002::fd37/124
ip vrf CPE1
!
"""
template = """
<group>
interface {{ interface }}
description {{ description }}
ip address {{ ip }}/{{ mask }}
{{ _start_ -}} ip vrf {{ vrf }}{{ -_end_ }}
</group>
"""
parser = ttp(data=data, template=template)
parser.parse()
results = parser.result(format='json')[0]
pprint.pprint(results)