Template Text Parser (TTP)

0.10.1 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates parsing network interface configuration. It initializes the `ttp` parser with input data and a template, then parses the data and prints the results in JSON format. The template uses `group` tags to define the output structure and `{{ variable }}` for data extraction, including optional fields using `_start_` and `_end_` tags.

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)

view raw JSON →