miron0xff / vyatta-conf-parser

Config parser for Vyatta/VyOS
MIT License
20 stars 11 forks source link

Error parsing named section config with missing {} curly brackets #7

Closed sidhoah8 closed 5 years ago

sidhoah8 commented 5 years ago

Hi @hedin,

I'm working with AT&T Vyatta vRouter configs and have an issue with some named sections that don't have any curly brackets/braces, like "{" and "}". I assume these are left over config statements that haven't been fully cleaned up... but I don't have control over the devices to always get such statements removed. For example with the following config statement (note "vti vti10"):

vti vti10
vti vti11 {
  address 1.1.1.1/28
  description Tunnel number 1
}
vti vti12 {
  address 2.2.2.2/28
  description Tunnel number 2
}

will result in the following error:

File "./myenv/lib/python3.7/site-packages/vyattaconfparser/parser.py", line 149, in parse_conf
  c, headers = parse_node(c, line, n, headers)
File "./myenv/lib/python3.7/site-packages/vyattaconfparser/parser.py", line 113, in parse_node
  update_tree(config, path, {key: value}, val_type='value')
File "./myenv/lib/python3.7/site-packages/vyattaconfparser/parser.py", line 73, in update_tree
  t.update(val)
AttributeError: 'str' object has no attribute 'update'

If I change the order so that the config statement without brackets in not the first entry, then it seems to be ok (see now "vti vti11"):

vti vti10 {
  address 1.1.1.1/28
  description Tunnel number 1
}
vti vti11
vti vti12 {
  address 2.2.2.2/28
  description Tunnel number 2
}

By inserting the following at line 73:

            if isinstance(t, str):
                t = {}

my scripts work again... though I'll be honest, I haven't tested to see if I lose that non-bracketed statement... but I don't need it either.

Maybe you can find a way to catch this issue? I'll also have a look as well.. though I'm new to Python and finding it difficult to follow how the parser works :-(

Let me know if you need anything more from me.

sidhoah8 commented 5 years ago

Thanks a lot @hedin. Works great now!

miron0xff commented 5 years ago

First, thanks for the issue, @sidhoah8 . I've updated code, added test for your case and uploaded new version on pypi.

You are difficult to follow how parser works not because you are new to Python. Parser was written long time ago in a great hurry. So instead of write grammar for config and parser for it regular expressions were used.

Every half a year when new issue appears it is difficult for me too to read and update the parser. Actually I don't know how it works now. I just write test, set a breakpoint near line where exception occurs and patch it until exception disappears and all tests pass.

sidhoah8 commented 5 years ago

Ok, thanks. That's reassuring! It has saved me a lot of time writing my scripts so I'm grateful that you wrote and shared it :-)