dmulyalin / ttp

Template Text Parser
MIT License
349 stars 34 forks source link

Using of group.to_int leads to errors #109

Open amyasnikov opened 1 year ago

amyasnikov commented 1 year ago

Python version: 3.10.12 TTP version: 0.9.4

Example of the error:

import ttp

template = '''
<group name="interfaces" to_int="mask">
interface {{name}}
 ip address {{address}}/{{mask}}
</group>
'''

data = '''
interface Ethernet0/1
 ip address 1.1.1.1/24
interface Ethernet0/2
 no ip address
'''

parser = ttp.ttp(template=template, data=data)
parser.parse()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 449, in parse
    self.__parse_in_one_process()
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 518, in __parse_in_one_process
    parserObj.parse(groups_indexes=input_obj.groups_indexes)
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 2628, in parse
    RSLTSOBJ.make_results(self.vars, raw_results, main_results=self.main_results)
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 2838, in make_results
    if self.processgrp() is not False:
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 3251, in processgrp
    self.record["result"], flags = self._ttp_["group"][func_name](
  File "/usr/local/lib/python3.10/site-packages/ttp/group/to_converters.py", line 19, in to_int
    v = data[k]
KeyError: 'mask'

Expected behavior: TTP ignores the absence of "mask" key for Ethernet0/2 and outputs the result without an error

amyasnikov commented 1 year ago

And another one example

import ttp

template = '''
<group name="interfaces" to_int="trunk_vlan">
interface {{ name }}
 switchport trunk allowed vlan {{ trunk_vlan | split(',') }}
</group>
'''

data = '''
interface Ethernet0/1
 switchport trunk allowed vlan 5,10,15
'''

parser = ttp.ttp(template=template, data=data)
parser.parse()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 449, in parse
    self.__parse_in_one_process()
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 518, in __parse_in_one_process
    parserObj.parse(groups_indexes=input_obj.groups_indexes)
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 2628, in parse
    RSLTSOBJ.make_results(self.vars, raw_results, main_results=self.main_results)
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 2838, in make_results
    if self.processgrp() is not False:
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 3251, in processgrp
    self.record["result"], flags = self._ttp_["group"][func_name](
  File "/usr/local/lib/python3.10/site-packages/ttp/ttp.py", line 69, in __call__
    return self._ttp_[self.parent_dir][self.function_name](*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/ttp/group/to_converters.py", line 21, in to_int
    data[k] = int(v)
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'

Maybe it's a misuse of to_int, but it will be very useful to get a list of integers from a list of strings via this clause.

dmulyalin commented 1 year ago

Hello, thank you for raising this issue, both requests should be addressed in 0.9.5, feel free to test and let me know how it goes.

dmulyalin commented 1 year ago

Updated documentation with an example of how to use intlist variable to convert list of string into list of integers - https://ttp.readthedocs.io/en/latest/Groups/Functions.html#to-int