Search code examples
python-3.xnetwork-programmingtext-parsing

Unable to access previous templates variables in TTP (template text parser) python


While going through ttp (template text parser), I came across the lookup function in match variables and groups. There it's mentioned that we can use lookup on previous template variables, but even the example code won't give proper results in my compiler.

from ttp import ttp

template = '''
<template name="interfaces">
<input load="text">
interface FastEthernet2.13
 description Customer CPE interface
 ip address 10.12.13.1 255.255.255.0
 vrf forwarding CPE-VRF
!
interface GigabitEthernet2.13
 description Customer CPE interface
 ip address 10.12.14.1 255.255.255.0
 vrf forwarding CUST1
!
</input>

<group name="{{ interface }}">
interface {{ interface }}
 description {{ description | ORPHRASE }}
 ip address {{ subnet | PHRASE | to_ip | network | to_str }}
 vrf forwarding {{ vrf }}
</group>
</template>

<template name="arp">
<input load="text">
Protocol  Address     Age (min)  Hardware Addr   Type   Interface
Internet  10.12.13.2        98   0950.5785.5cd1  ARPA   FastEthernet2.13
Internet  10.12.14.3       131   0150.7685.14d5  ARPA   GigabitEthernet2.13
</input>

<group lookup="interface, template='interfaces', update=True">
Internet  {{ ip }}  {{ age | DIGIT }}   {{ mac }}  ARPA   {{ interface }}
</group>
</template>
'''
parser = ttp()
parser.add_template(template)
parser.parse()
print(parser.result(format = "json")[0])

This should give me the result as shown below, but I don't get the "ARP TABLE OUTPUT" part as mentioned in the code below (which is actually where we are using lookup) :-

[[{'FastEthernet2.13': {'description': 'Customer CPE interface',
                        'subnet': '10.12.13.0/24',
                        'vrf': 'CPE-VRF'},
   'GigabitEthernet2.13': {'description': 'Customer CPE interface',
                           'subnet': '10.12.14.0/24',
                           'vrf': 'CUST1'}}],
 [[{'age': '98', ## ARP TABLE OUTPUT 1
    'description': 'Customer CPE interface',
    'interface': 'FastEthernet2.13',
    'ip': '10.12.13.2',
    'mac': '0950.5785.5cd1',
    'subnet': '10.12.13.0/24',
    'vrf': 'CPE-VRF'},
   {'age': '131', ## ARP TABLE OUTPUT 2
    'description': 'Customer CPE interface',
    'interface': 'GigabitEthernet2.13',
    'ip': '10.12.14.3',
    'mac': '0150.7685.14d5',
    'subnet': '10.12.14.0/24',
    'vrf': 'CUST1'}]]]

Any help would be appreciated.


Solution

  • Running your template with TTP 0.8.0 gives me the output you expected:

    from ttp import ttp
    import pprint
    
    def test_lookup_crosstemplates():
        template = """
    <template name="interfaces">
    <input load="text">
    interface FastEthernet2.13
     description Customer CPE interface
     ip address 10.12.13.1 255.255.255.0
     vrf forwarding CPE-VRF
    !
    interface GigabitEthernet2.13
     description Customer CPE interface
     ip address 10.12.14.1 255.255.255.0
     vrf forwarding CUST1
    !
    </input>
    
    <group name="{{ interface }}">
    interface {{ interface }}
     description {{ description | ORPHRASE }}
     ip address {{ subnet | PHRASE | to_ip | network | to_str }}
     vrf forwarding {{ vrf }}
    </group>
    </template>
    
    <template name="arp">
    <input load="text">
    Protocol  Address     Age (min)  Hardware Addr   Type   Interface
    Internet  10.12.13.2        98   0950.5785.5cd1  ARPA   FastEthernet2.13
    Internet  10.12.14.3       131   0150.7685.14d5  ARPA   GigabitEthernet2.13
    </input>
    
    <group lookup="interface, template='interfaces', update=True">
    Internet  {{ ip }}  {{ age | DIGIT }}   {{ mac }}  ARPA   {{ interface }}
    </group>
    </template>
        """
        parser = ttp()
        parser.add_template(template)
        parser.parse()
        
        res = parser.result()
        # pprint.pprint(res)
        assert res == [[{'FastEthernet2.13': {'description': 'Customer CPE interface',
                            'subnet': '10.12.13.0/24',
                            'vrf': 'CPE-VRF'},
       'GigabitEthernet2.13': {'description': 'Customer CPE interface',
                               'subnet': '10.12.14.0/24',
                               'vrf': 'CUST1'}}],
     [[{'age': '98',
        'description': 'Customer CPE interface',
        'interface': 'FastEthernet2.13',
        'ip': '10.12.13.2',
        'mac': '0950.5785.5cd1',
        'subnet': '10.12.13.0/24',
        'vrf': 'CPE-VRF'},
       {'age': '131',
        'description': 'Customer CPE interface',
        'interface': 'GigabitEthernet2.13',
        'ip': '10.12.14.3',
        'mac': '0150.7685.14d5',
        'subnet': '10.12.14.0/24',
        'vrf': 'CUST1'}]]]
    
    test_lookup_crosstemplates()
    

    Curious what version of TTP are you using, group lookups introduced starting with version 0.4.0.

    If that is not the issue here, please share example of parsing results you getting from TTP together with exact template and sample of data in addition to the expected results you already shared originally.