dmulyalin / ttp

Template Text Parser
MIT License
351 stars 34 forks source link

How to match an entire line and discard #19

Closed gwoodwa1 closed 4 years ago

gwoodwa1 commented 4 years ago

This may end up being a feature request but I am unsure on how within TTP you match something but then tell the parser to ignore the rest of the line i.e. we don't care about anything on the rest of the line at all. I have found a way to achieve this which is using {{ line }} which is making the match but then I do del="line" in the group to remove it. I've tried using {{ ignore }} but I haven't got it to match the rest of the line. I think what is required is a very simple feature which does the equivalent of {{ line }} del="line" as a broad brush. If this was a inbuilt documented feature then it would help getting templates to match the data much more easily. Hope this makes sense and I have an example script below.

from ttp import ttp
import json

data_to_parse="""
===============================================================================
Single Fiber Mode  : No                         Min Frame Length : 64 Bytes
IfIndex            : 35848192                   Hold time up     : 5 seconds
"""

ttp_template = """
<group name="fibre_check" method="table" del="_line_">
=============================================================================== {{ _start_ }}
Single Fiber Mode  : {{ SMF }}{{_line_}}                  
IfIndex            : {{ IfIndex }}{{ _line_ }}
</group>
"""

parser = ttp(data=data_to_parse, template=ttp_template)
parser.parse()
res = json.loads(parser.result(format='json')[0])
print(res)

Maybe I am over complicating this using my method but it's a very common use case to want to match something but then ignore whatever follows after that for the whole line.

dmulyalin commented 4 years ago

Filtering wise, there are a number of built-in filtering functions available for match variables, not talking about custom macro functions that you can write.

However, could you please elaborate on above code with examples of what you getting and what desired result should be?

gwoodwa1 commented 4 years ago

Sure, my code is working and I am getting the results that I want. It's just whether there is a better way to achieve what I am doing without using {{ line }} and del="line". Looking at this line: Single Fiber Mode : No Min Frame Length : 64 Bytes I am matching the "Single Fiber Mode:" and then capturing the field after that. The rest of the line, I just want to ignore whatever is there and there could be other fields on that same line in future which I will want to ignore as well. I don't want to just discard what is there now but anything at all on that line even if the formatting changed for that line. If I was to describe it would be like having this type of function in TTP {{ ignore | rest_of_line }} which is like what I am doing with {{ line }} del="line"

dmulyalin commented 4 years ago

To ignore portion of the line there is match variable indicator exists called ignore. In essence, this indicator allows to inject any non-capturing regular expression group into regex generated by TTP.

To achieve what you want, this template could be used:

ttp_template = """
<group name="fibre_check">
=============================================================================== {{ _start_ }}
Single Fiber Mode  : {{ SMF }}            {{ ignore(".+") }}                  
IfIndex            : {{ IfIndex }}        {{ ignore(".+") }}
</group>
"""

Regex .+ matching any character repeated one or more times.

One more note - method="table" - do not think is required here, as you already have explicitly marked the start of the group with _start_ indicator.

Also, this line: res = json.loads(parser.result(format='json')[0]) is a bit redundant, if you want to get a python native structure for first template results, just use: res = parser.result()[0]

gwoodwa1 commented 4 years ago

Thanks alot, I know a few people who have asked me about how to do this. Now you mention it, {{ ignore (".+") }} makes sense as the best way of doing it. Also the improvement around parser.result is good too. Absolutely, love the tool but would like to make the maximum use of all the features. Thanks for coming back on this one.