Skip to content

Commit

Permalink
Value parse (#68)
Browse files Browse the repository at this point in the history
* Fix parsing of value string to allow mismatched parenthesis inside [] brackets.

* Improve readability of value parse fix.

* Added additional test.

* Soft fail when fcntl is missing, fallback to default screen size.
  • Loading branch information
harro authored Nov 26, 2019
1 parent da837ec commit 8a4eed2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
11 changes: 11 additions & 0 deletions tests/textfsm_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ def testFSMValue(self):
v.Parse,
'Value beer (boo)hoo)')

# Unbalanced parenthesis can exist if within square "[]" braces.
v = textfsm.TextFSMValue(options_class=textfsm.TextFSMOptions)
v.Parse('Value beer (boo[(]hoo)')
self.assertEqual(v.name, 'beer')
self.assertEqual(v.regex, '(boo[(]hoo)')

# Escaped braces don't count.
self.assertRaises(textfsm.TextFSMTemplateError,
v.Parse,
'Value beer (boo\[)\]hoo)')

# String function.
v = textfsm.TextFSMValue(options_class=textfsm.TextFSMOptions)
v.Parse('Value Required beer (boo(hoo))')
Expand Down
2 changes: 1 addition & 1 deletion textfsm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
"""
from textfsm.parser import *

__version__ = '1.1.0'
__version__ = '1.1.1'
4 changes: 3 additions & 1 deletion textfsm/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,10 @@ def Parse(self, value):
raise TextFSMTemplateError(
"Invalid Value name '%s' or name too long." % self.name)

square_brackets = r'[^\]?\[[^]]*\]'
regex_without_brackets = re.sub(square_brackets, '', self.regex)
if (not re.match(r'^\(.*\)$', self.regex) or
self.regex.count('(') != self.regex.count(')')):
regex_without_brackets.count('(') != regex_without_brackets.count(')')):
raise TextFSMTemplateError(
"Value '%s' must be contained within a '()' pair." % self.regex)

Expand Down
8 changes: 6 additions & 2 deletions textfsm/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
from __future__ import print_function
from __future__ import unicode_literals

import fcntl
try:
# Import fails on Windows machines.
import fcntl
except (ImportError, ModuleNotFoundError):
pass
import getopt
import os
import re
Expand Down Expand Up @@ -173,7 +177,7 @@ def TerminalSize():
with open(os.ctermid()) as tty_instance:
length_width = struct.unpack(
'hh', fcntl.ioctl(tty_instance.fileno(), termios.TIOCGWINSZ, '1234'))
except (IOError, OSError):
except (IOError, OSError, NameError):
try:
length_width = (int(os.environ['LINES']),
int(os.environ['COLUMNS']))
Expand Down

0 comments on commit 8a4eed2

Please sign in to comment.