google / textfsm

Python module for parsing semi-structured text into python tables.
Apache License 2.0
1.09k stars 168 forks source link

Issue with Textfsm and StringsIO #42

Closed scetron closed 5 years ago

scetron commented 5 years ago

I am seeing an issue where I have a template defined as a string object like this:

`interface = StringIO('''Value Key,Required interface (\w+1\d{1,2}) Value Required avlan ([0-9]+) Value tvlan (([0-9]+,)+)

Start ^\sinterface ${interface} -> Continue ^\sswitchport access vlan ${avlan} -> Continue ^\s*switchport trunk allowed vlan ${tvlan} -> Record''')

template = textfsm.TextFSM(interface) return template.ParseText(config), template`

In an older version, using this as the template for parsing data would work just find, however now I am seeing this issue when I attempt to use a template defined in this way:

File "/usr/local/lib/python3.7/site-packages/jtextfsm.py", line 852, in ParseText lines = text.splitlines() AttributeError: '_io.TextIOWrapper' object has no attribute 'splitlines'

harro commented 4 years ago

To support both PY2 and PY3 our test files use: from six.moves import StringIO

io.StringIO may also be an option.

pinggit commented 3 years ago

I tested this and see different errors. Here is the code:

template = """
Value Time (..:..:..)
Value Timezone (\S+)
Value WeekDay (\w+)
Value Month (\w+)
Value MonthDay (\d+)
Value Year (\d+)

Start
  ^${Time}.* ${Timezone} ${WeekDay} ${Month} ${MonthDay} ${Year} -> Record
"""
f_template = StringIO(template)
re_table = textfsm.TextFSM(f_template)

the last line gave this error:

TextFSMTemplateError: Value '(..:..:..)Value Timezone (\S+)Value WeekDa
y (\w+)Value Month (\w+)Value MonthDay (\d+)Value Year (\d+)Start  ^${T
ime}.* ${Timezone} ${WeekDay} ${Month} ${MonthDay} ${Year} -> Record' m
ust be contained within a '()' pair. Line 1.

if I save the same template string into a file then it works fine.

template_file = 'template_clock.txt'
f_template = open(template_file)
re_table = textfsm.TextFSM(f_template)

@harro are you sure this works? I'm with python3.8.5

pinggit commented 3 years ago

please ignore. it looks like some of the \n in the template were missing here. it works fine now.