Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Exit value indicates if processed data matched expected result.
with open(args[2], 'r') as f:
ref_table = f.read()
if ref_table != result:
print('Data mis-match!')
return 1
else:
print('Data match!')
if __name__ == '__main__':
help_msg = '%s [--help] template [input_file [output_file]]\n' % sys.argv[0]
try:
sys.exit(main())
except Usage as err:
print(err, file=sys.stderr)
print('For help use --help', file=sys.stderr)
sys.exit(2)
except (IOError, TextFSMError, TextFSMTemplateError) as err:
print(err, file=sys.stderr)
sys.exit(2)
if argv is None:
argv = sys.argv
try:
opts, args = getopt.getopt(argv[1:], 'h', ['help'])
except getopt.error as msg:
raise Usage(msg)
for opt, _ in opts:
if opt in ('-h', '--help'):
print(__doc__)
print(help_msg)
return 0
if not args or len(args) > 4:
raise Usage('Invalid arguments.')
# If we have an argument, parse content of file and display as a template.
# Template displayed will match input template, minus any comment lines.
with open(args[0], 'r') as template:
fsm = TextFSM(template)
print('FSM Template:\n%s\n' % fsm)
if len(args) > 1:
# Second argument is file with example cli input.
# Prints parsed tabular result.
with open(args[1], 'r') as f:
cli_input = f.read()
table = fsm.ParseText(cli_input)
print('FSM Table:')
result = str(fsm.header) + '\n'
def main(argv=None):
"""Validate text parsed with FSM or validate an FSM via command line."""
if argv is None:
argv = sys.argv
try:
opts, args = getopt.getopt(argv[1:], 'h', ['help'])
except getopt.error as msg:
raise Usage(msg)
for opt, _ in opts:
if opt in ('-h', '--help'):
print(__doc__)
print(help_msg)
return 0
if not args or len(args) > 4:
raise Usage('Invalid arguments.')
# If we have an argument, parse content of file and display as a template.
# Template displayed will match input template, minus any comment lines.
with open(args[0], 'r') as template:
fsm = TextFSM(template)
print('FSM Template:\n%s\n' % fsm)