ttp icon indicating copy to clipboard operation
ttp copied to clipboard

Request: linenumber match function

Open Yoyasp opened this issue 2 years ago • 0 comments

First of all thank you soo much for creating and maintaining this repo.

I am looking for a match function/indicator that saves the linenumber of a matching variable.

My usecase is compliancy checking where i would like to highlight configuration lines in the original data.

from ttp import ttp

data_to_parse = """
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
!
"""

ttp_template = """
interface {{ interface | linenumber('INTERFACE_LINENUMBER')}}
 ip address {{ ip }}/{{ mask }}
 description {{ description }}
 ip vrf {{ vrf | linenumber('VRF_LINENUMBER') }}
"""

# create parser object and parse data using template:
parser = ttp(data=data_to_parse, template=ttp_template)
parser.parse()

would result in:

[
    [
        {
            "description": "Router-id-loopback",
            "interface": "Loopback0",
            "INTERFACE_LINENUMBER": 1,
            "ip": "192.168.0.113",
            "mask": "24"
        },
        {
            "description": "CPE_Acces_Vlan",
            "interface": "Vlan778",
            "ip": "2002::fd37",
            "mask": "124",
            "vrf": "CPE1",
            "VRF_LINENUMBER": 8,
            "INTERFACE_LINENUMBER": 5
        }
    ]
]

Yoyasp avatar Jan 20 '23 13:01 Yoyasp