dmulyalin / ttp

Template Text Parser
MIT License
350 stars 34 forks source link

Possible bug when using add_vars variable and referencing it to use in a regex with re(MY_REGEX_VARIABLE) #124

Open bodleytunes opened 2 months ago

bodleytunes commented 2 months ago

Hi,

I'm hitting a problem where it could be a bug but could be how Im using TTP with a regex string variable.

If I defined/hard code under and create a variable POLICY_RE like so:

POLICY_RE = ".*MANAGEMENT.*"

And using it to scrape a juniper

<group name="policy_options.{{ policy }}" itemize="network">
{{ ignore("\s*") }}set policy-options prefix-list {{ policy | re(POLICY_RE) }} {{ network }}
</group>

It works fine.

But if I try this

<group name="policy_options.{{ policy }}" itemize="network">
{{ ignore("\s*") }}set policy-options prefix-list {{ policy | re(MY_CUSTOM_REGEX) }} {{ network }}
</group>

    search_policies = ".*MANAGEMENT.*"

    parser = ttp(data=config, template=ttp_template)
    parser.add_vars({"MY_CUSTOM_REGEX": search_policies})
    parser.parse()

It doesn't work and when I step through the library code I see that the variable for parserObj.groups.0.start_re

is showing the name of the variable rather than the contents of the variable, the regex string like so:

'\\n(?:\\s*)set[ \\t] policy\\-options[ \\t] prefix\\-list[ \\t] (?P<policy>(?:MY_CUSTOM_REGEX))[ \\t] (?P<network>(?:\\S ))[\\t ]*(?=\\n|\\r\\n)'

whereas in the working case with hardcoding vars in template im getting the full regex string appearing

parserObj.groups.0.start_re

'\\n(?:\\s*)set[ \\t] policy\\-options[ \\t] prefix\\-list[ \\t] (?P<policy>(?:.*MANAGEMENT.*))[ \\t] (?P<network>(?:\\S ))[\\t ]*(?=\\n|\\r\\n)'

Is this by design or am I using it incorrectly?

Many thanks,

Jon.