Skip to content

Instantly share code, notes, and snippets.

@cvubrugier
Last active December 22, 2015 19:49
Show Gist options
  • Save cvubrugier/6522643 to your computer and use it in GitHub Desktop.
Save cvubrugier/6522643 to your computer and use it in GitHub Desktop.
The purpose of this script is to ensure that configshell behaves the same way when simpleparse is replaced with pyparsing. Currently, configshell uses simpleparse to parse the commands, but I intend to replace it with pyparsing which is more actively maintained and more widely used.
#!/usr/bin/python
#
# The purpose of this script is to ensure that configshell behaves the
# same way when simpleparse is replaced with pyparsing. Currently,
# configshell uses simpleparse to parse the commands, but I intend to
# replace it with pyparsing which is more actively maintained and more
# widely used.
#
# This script reads the command history of targetcli and verifies that
# the commands are parsed the same way with simpleparse and pyparsing.
#
# Usage: cat history.txt | python simpleparse-pyparsing-cmp.py
from pyparsing import Empty, Group, OneOrMore, Optional, Regex, Suppress, Word
from pyparsing import ParseResults
from pyparsing import alphanums
import simpleparse.parser
import sys
import unittest
class ParsingTestCase(unittest.TestCase):
"""
Check if parsing with pyparsing gives the same result as simpleparse
"""
def __init__(self, test_input):
"""
Allow to pass a parameter to the test case
http://stackoverflow.com/questions/2798956/python-unittest-generate-multiple-tests-programmatically/3772008#3772008
"""
super(ParsingTestCase, self).__init__()
self.test_input = test_input
def runTest(self):
self.assertEqual(parse_simpleparse(self.test_input),
parse_pyparsing(self.test_input))
def __suite():
"""
Generate a test suite from the commands read from standard input
"""
suite = unittest.TestSuite()
for line in sys.stdin.readlines():
cmd = line.rstrip('\n')
suite.addTest(ParsingTestCase(cmd))
return suite
def parse_simpleparse(line):
"""
Original function used in configshell to parse the command line
Copy-pasted from shell._parse_cmdline()
"""
grammar = \
r'''
<eol> := '\n'
<space> := ' '+
line := (linepath)/(space?, command), parameters?, eol?
>linepath< := space?, path, space?, command?
command := [a-zA-Z0-9_]+
>parameters< := (space, kparam/pparam)+
pparam := var
kparam := keyword, '=', value
>keyword< := [a-zA-Z0-9_\-]+
>value< := var?
path := bookmark/pathstd/[0-9]+/'..'/'.'/'*'
<pathstd> := ([a-zA-Z0-9:_.-]*, '/', [a-zA-Z0-9:_./-]*, '*'?)
<var> := [a-zA-Z0-9_\\+/.<>~@:-%]+
<bookmark> := '@', var?
'''
parser = simpleparse.parser.Parser(grammar, root='line')
path = ''
command = ''
pparams = []
kparams = {}
(success, result_trees, next_character) = parser.parse(line)
if success:
for tree in result_trees:
token = tree[0]
value = line[tree[1]:tree[2]]
if token == 'path':
path = value
elif token == 'command':
command = value
elif token == 'pparam':
pparams.append(value)
elif token == 'kparam':
(keyword, sep, value) = value.partition('=')
kparams[keyword] = value
return "path='%s' command='%s' " % (path, command) \
+ "pparams=%s " % str(pparams) \
+ "kparams=%s" % str(kparams)
# Helper for pyparsing to group the location of a token and its value
# http://stackoverflow.com/questions/18706631/pyparsing-get-token-location-in-results-name
locator = Empty().setParseAction(lambda s,l,t: l)
def locatedExpr(expr):
return Group(locator('location') + expr('value'))
def parse_pyparsing(line):
"""
New function that uses pyparsing instead of simpleparse
"""
command = locatedExpr(Word(alphanums + '_'))('command')
var = Word(alphanums + '_\+/.<>()~@:-%]')
value = var
keyword = Word(alphanums + '_\-')
kparam = locatedExpr(keyword + Suppress('=') + Optional(value, default=''))('kparams*')
pparam = locatedExpr(var)('pparams*')
parameter = kparam | pparam
parameters = OneOrMore(parameter)
bookmark = Regex('@([A-Za-z0-9:_.]|-)+')
pathstd = Regex('([A-Za-z0-9:_.]|-)*' + '/' + '([A-Za-z0-9:_./]|-)*') \
| '..' | '.'
path = locatedExpr(bookmark | pathstd | '*')('path')
parser = Optional(path) + Optional(command) + Optional(parameters)
parse_results = parser.parseString(line)
path = parse_results.path
path = ''
command = ''
pparams = []
kparams = {}
if isinstance(parse_results.path, ParseResults):
path = parse_results.path.value
if isinstance(parse_results.command, ParseResults):
command = parse_results.command.value
if isinstance(parse_results.pparams, ParseResults):
pparams = [pparam.value for pparam in parse_results.pparams]
if isinstance(parse_results.kparams, ParseResults):
kparams = dict([kparam.value for kparam in parse_results.kparams])
return "path='%s' command='%s' " % (path, command) \
+ "pparams=%s " % str(pparams) \
+ "kparams=%s" % str(kparams)
if __name__ == '__main__':
unittest.TextTestRunner().run(__suite())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment