dmulyalin / ttp

Template Text Parser
MIT License
348 stars 34 forks source link

resuball functions will execution many times #105

Closed matrix273 closed 1 year ago

matrix273 commented 1 year ago

I want use "resuball" functions to replace "R" to "Router","r" to "Repeater" ,but the result does't match I wanted.

Data

Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge
                  S - Switch, H - Host, I - IGMP, r - Repeater

Device ID        Local Intrfce     Holdtme    Capability  Platform  Port ID
device1.cisco.com   Eth 0/1        122          T S r     WS-C2900  2/11
device2.cisco.com   Eth 0/1        179           R        4500      Eth 0

Template

<vars>
cap_resub = {
'Router': ['R'],
'Trans Bridge':['T'],
'Switch':['S'],
'Repeater':['r']
}
</vars>

<group name="results*">
{{device}} {{ interface |ORPHRASE }} {{holdtime|DIGIT}} {{capability|ORPHRASE|resuball('cap_resub')}} {{platform}} {{port_id|ORPHRASE}}
</group>

Result

[
    {
        "results": [
            {
                "capability": "TRepeaterans BRepeateridge Switch Repeater",
                "device": "device1.cisco.com",
                "holdtime": "122",
                "interface": "Eth 0/1",
                "platform": "WS-C2900",
                "port_id": "2/11"
            },
            {
                "capability": "RouteRepeater",
                "device": "device2.cisco.com",
                "holdtime": "179",
                "interface": "Eth 0/1",
                "platform": "4500",
                "port_id": "Eth 0"
            }
        ]
    }
]

As you see,the resuball functions execution many times on "capability". does't match I wanted.

How do I get the result below

[
    {
        "results": [
            {
                "capability": "Trans Bridge Switch Repeater",
                "device": "device1.cisco.com",
                "holdtime": "122",
                "interface": "Eth 0/1",
                "platform": "WS-C2900",
                "port_id": "2/11"
            },
            {
                "capability": "Router",
                "device": "device2.cisco.com",
                "holdtime": "179",
                "interface": "Eth 0/1",
                "platform": "4500",
                "port_id": "Eth 0"
            }
        ]
    }
]
dmulyalin commented 1 year ago

Hello, resuball iterates over all patterns and performs replacement for each of them, it would not work well for this case, probably easier to use macro function e.g.:

<macro>
def add_capab(match):
    res = []
    items = match.split(" ")
    for item in items:
        if item == "T":
             res.append("Trans Bridge")
        elif item == "S":
             res.append("Switch")
        elif item == "r":
             res.append("Repeater")
        elif item == "R":
             res.append("Router")
        else:
             res.append(item)   
    return " ".join(res)
</macro>

<group name="results*">
{{device}} {{ interface |ORPHRASE }} {{holdtime|DIGIT}} {{capability|ORPHRASE|macro("add_capab")}} {{platform}} {{port_id|ORPHRASE}}
</group>
matrix273 commented 1 year ago

Thanks for you reply,

dmulyalin @.***> 于2023年4月30日周日 07:33写道:

Hello, resuball iterates over all patterns and performs replacement for each of them, it would not work well for this case, probably easier to use macro function e.g.:

def add_capab(match): res = [] items = match.split(" ") for item in items: if item == "T": res.append("Trans Bridge") elif item == "S": res.append("Switch") elif item == "r": res.append("Repeater") elif item == "R": res.append("Router") else: res.append(item) return " ".join(res) {{device}} {{ interface |ORPHRASE }} {{holdtime|DIGIT}} {{capability|ORPHRASE|macro("add_capab")}} {{platform}} {{port_id|ORPHRASE}}

— Reply to this email directly, view it on GitHub https://github.com/dmulyalin/ttp/issues/105#issuecomment-1528896157, or unsubscribe https://github.com/notifications/unsubscribe-auth/AF5IDZCYRCBY4EBBKEVXIXLXDWQMHANCNFSM6AAAAAAXK3I5HE . You are receiving this because you authored the thread.Message ID: @.***>

matrix273 commented 1 year ago

I will use macro to replace it.