Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rrobe53 opened this issue Jun 29, 2023 · 1 comment
Open

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

rrobe53 opened this issue Jun 29, 2023 · 1 comment

Comments

@rrobe53
Copy link

rrobe53 commented Jun 29, 2023

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants