dmulyalin / ttp

Template Text Parser
MIT License
351 stars 34 forks source link

Group functions filter out tail match results #103

Open dmulyalin opened 1 year ago

dmulyalin commented 1 year ago

When having this template and data:

data = """
interface Gig123
 description foo
 mtu 1234
 ipv4 address 1.2.3.4 255.255.255.0
 arp timeout 123
 mac-address 123.123.1
!
interface Gig321
 description foo
 mtu 4321
 ipv4 address 1.2.3.5 255.255.255.0
 arp-timeout 4321
 mac-address 123.123.2
!
"""
template = """
<group name="interfaces*" contains="name">
interface {{ name }}
 description {{ description | re(".*") }}
 mtu {{ mtu | to_int }}
 mac-address {{ mac_address }}
 arp timeout {{ arp_timeout }}

 <group name="ipv4*" method="table">
 ipv4 address {{ ip | _exact_ }} {{ mask }}
 </group>

!{{ _end_ }}
</group>
"""

Parsing gives these results:

[[{'interfaces': [{'description': 'foo',
                   'ipv4': [{'ip': '1.2.3.4', 'mask': '255.255.255.0'}],
                   'mtu': 1234,
                   'name': 'Gig123'},
                  {'description': 'foo',
                   'ipv4': [{'ip': '1.2.3.5', 'mask': '255.255.255.0'}],
                   'mtu': 4321,
                   'name': 'Gig321'}]}]]

this is due to the fact that mac_address matched after ipv4 group matched, and by the time we process mac_address match group functions results does not have name attribute in them. Removing contains="name" produces expected result:

[[{'interfaces': [{'arp_timeout': '123',
                   'description': 'foo',
                   'ipv4': [{'ip': '1.2.3.4', 'mask': '255.255.255.0'}],
                   'mac_address': '123.123.1',
                   'mtu': 1234,
                   'name': 'Gig123'},
                  {'description': 'foo',
                   'ipv4': [{'ip': '1.2.3.5', 'mask': '255.255.255.0'}],
                   'mac_address': '123.123.2',
                   'mtu': 4321,
                   'name': 'Gig321'}]}]]
dmulyalin commented 1 year ago

Introduced fix in latest commit, fix logic is weak but simplest to fix this issue, better approach required to collect parsing results in general but will require significant code refactoring.

ffhan commented 1 year ago

@dmulyalin first of all thank you so much for this project, it is amazing.

Unfortunately, this bug is still not fixed even in version 0.9.5. I can provide the minimal example to reproduce if needed.