__init__.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import sys
  2. from jmespath import ast
  3. # The unittest module got a significant overhaul
  4. # in 2.7, so if we're in 2.6 we can use the backported
  5. # version unittest2.
  6. if sys.version_info[:2] == (2, 6):
  7. import unittest2 as unittest
  8. import simplejson as json
  9. from ordereddict import OrderedDict
  10. else:
  11. import unittest
  12. import json
  13. from collections import OrderedDict
  14. # Helper method used to create an s-expression
  15. # of the AST to make unit test assertions easier.
  16. # You get a nice string diff on assert failures.
  17. def as_s_expression(node):
  18. parts = []
  19. _as_s_expression(node, parts)
  20. return ''.join(parts)
  21. def _as_s_expression(node, parts):
  22. parts.append("(%s" % (node.__class__.__name__.lower()))
  23. if isinstance(node, ast.Field):
  24. parts.append(" %s" % node.name)
  25. elif isinstance(node, ast.FunctionExpression):
  26. parts.append(" %s" % node.name)
  27. elif isinstance(node, ast.KeyValPair):
  28. parts.append(" %s" % node.key_name)
  29. for child in node.children:
  30. parts.append(" ")
  31. _as_s_expression(child, parts)
  32. parts.append(")")