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

Help with indentation in templates #22

Closed
aninchat opened this issue Sep 10, 2020 · 4 comments
Closed

Help with indentation in templates #22

aninchat opened this issue Sep 10, 2020 · 4 comments

Comments

@aninchat
Copy link

aninchat commented Sep 10, 2020

Hi,

I have the following data I want to parse through, to simply extract the IP address of the interface.

In [68]: print(data)

interface Loopback0
 description Fabric Node Router ID
 ip address 192.2.101.70 255.255.255.255
 ip pim sparse-mode
 ip router isis 
 clns mtu 1400
end

With the assumption that my template will be indented in python code, I have added numerous whitespaces in the template and want to regex match on it to ignore the white spaces. The template I have is:

In [75]: print(show_run_parser_template)

   {{ ignore("\s+") }}ip address {{ ip_address }} 255.255.255.255

This is unable to parse through the data and I always get an empty list back. Any idea what I am doing wrong here?

@dmulyalin
Copy link
Owner

Probably you now invoking ttp parser object properly, this works for me using Python 3.7.7:

import pprint
from ttp import ttp

data = """
interface Loopback0
 description Fabric Node Router ID
 ip address 192.2.101.70 255.255.255.255
 ip pim sparse-mode
 ip router isis 
 clns mtu 1400
end
interface Loopback0
 description Fabric Node Router ID
 ip address 192.2.101.71 255.255.255.255
 ip pim sparse-mode
 ip router isis 
 clns mtu 1400
end
    """
template = """{{ ignore("\s+") }}ip address {{ ip_address }} 255.255.255.255"""
parser = ttp(data, template)
parser.parse()
res = parser.result()
pprint.pprint(res)

# prints:
# [[[{'ip_address': '192.2.101.70'}, {'ip_address': '192.2.101.71'}]]]

@aninchat
Copy link
Author

aninchat commented Sep 10, 2020

I had tried the above method earlier and it works. However, lets say I create the template inside a python function, where it will automatically be indented, like so:

def parse(data):
    template = """
    {{ ignore("\s+") }}ip address {{ ip_address }} 255.255.255.255 <== indented whitespaces
    """
    parser = ttp(data=data, template=template)
    parser.parse()
    print(parser.result())

This is adding extra whitespaces due to the indentation and it breaks the template parsing. For one line templates, I can declare them inline and escape this problem. But for bigger templates, it is challenges and makes readability of the template difficult.

@dmulyalin
Copy link
Owner

Have a look at tests on how to define templates within functions using triple quotes:

def test_anonymous_group_with_vars():

@aninchat
Copy link
Author

Thank you, this works perfectly.

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