dmulyalin / ttp

Template Text Parser
MIT License
351 stars 34 forks source link

XML Parser for <macro> error with less than operator #110

Open rrobe53 opened 1 year ago

rrobe53 commented 1 year ago

Trying to make a macro like this, and leverage for pulling bandwidth out of show interface from some Cisco devices with Ansible.

def convertBandwidth(kbps):
    if not isinstance(kbps, float):
        kbps = float(kbps)
    step_unit = 1000.0

    for unit in ['Kbps', 'Mbps', 'Gbps', 'Tbps']:
        if kbps < step_unit:
            return f"{kbps:.0f} {unit}"
        kbps /= step_unit
    return kbps

It functions as pure python, but when moving to a Macro block it doesn't. Through troubleshooting adding I've discovered the if kbps < step_unit is what is hosing things up. Reversing it to if step_unit > kbps: fixes things. So I think it's something to do with the XML parsing thinking its an opening tag, especially based on this output when running via ttp directly.

Traceback (most recent call last):
  File "/home/rroberts/venv/ansible/bin/ttp", line 8, in <module>
    sys.exit(cli_tool())
  File "/home/rroberts/venv/ansible/lib64/python3.9/site-packages/ttp/ttp.py", line 3689, in cli_tool
    parser_Obj.add_template(template=ttp_template)
  File "/home/rroberts/venv/ansible/lib64/python3.9/site-packages/ttp/ttp.py", line 368, in add_template
    template_obj = _template_class(
  File "/home/rroberts/venv/ansible/lib64/python3.9/site-packages/ttp/ttp.py", line 948, in __init__
    self.template = self.handle_extend(template_text)
  File "/home/rroberts/venv/ansible/lib64/python3.9/site-packages/ttp/ttp.py", line 1328, in handle_extend
    template_ET = self.construct_etree(template_text)
  File "/home/rroberts/venv/ansible/lib64/python3.9/site-packages/ttp/ttp.py", line 1391, in construct_etree
    template_ET = ET.XML("<template>\n{}\n</template>".format(template_text))
  File "/usr/lib64/python3.9/xml/etree/ElementTree.py", line 1342, in XML
    parser.feed(text)
dmulyalin commented 10 months ago

Hello, would using &lt; in place of < solves the problem?