diff --git a/README.rst b/README.rst index ffb12a41ed08f44cc88537fe6d26332a1f7422da..f94db35187a6804e055ac3b65f928f45d5765c3f 100644 --- a/README.rst +++ b/README.rst @@ -45,6 +45,8 @@ The following Jinja filters were added: - :code:`from_json`: Convert from json. - :code:`pprint`: Pretty print variable. - :code:`combine`: Combine 2 dictionaries. +- :code:`jmespath`: Queries data using the `JMESPath <http://jmespath.org/>`_ + query language. Example usage can be seen in :code:`tests.sh` and for specific filters in the docstrings in :code:`template/filters.py`. @@ -85,5 +87,4 @@ TODO - Release on tagged commits to PyPI in Travis CI (https://docs.travis-ci.com/user/deployment/pypi/ and https://docs.travis-ci.com/user/encryption-keys/). -- Add JMESPath support. - Add TOML support? diff --git a/setup.py b/setup.py index 7abfd3bcff29369202de230eacb8355750b384e8..897402d55bf233104f1b4995a26614588a551092 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ setup( ], keywords='config configuration jinja template environment', packages=find_packages(), - install_requires=['Jinja2', 'PyYAML', 'six'], + install_requires=['Jinja2', 'PyYAML', 'jmespath'], extras_require={ 'dev': ['tox'], }, entry_points={ diff --git a/template/filters.py b/template/filters.py index a98d0ef30e6acb243ec0aade71c357699128adb5..99447c536efea1cdee81a77f698aff4619b3b688 100644 --- a/template/filters.py +++ b/template/filters.py @@ -1,7 +1,6 @@ #!/usr/bin/env python from __future__ import (absolute_import, division, print_function, unicode_literals) -import six # noqa: F401 def to_yaml(value): @@ -45,6 +44,7 @@ def from_json(value): Returns native data structure from the given JSON string. Examples: + >>> import six >>> from_json('[1, 2, 3]') [1, 2, 3] >>> from_json('"a"') == six.text_type('a') @@ -78,6 +78,7 @@ def pprint(value): Examples: >>> pprint(1) '1' + >>> import six >>> output = pprint([{'first_name': 'John', 'last_name': 'Doe'}, {'first_name': 'Jane', 'last_name': 'Doe'}]) # noqa: E501 >>> if six.PY3: ... output == "[{'first_name': 'John', 'last_name': 'Doe'},\\n {'first_name': 'Jane', 'last_name': 'Doe'}]" @@ -101,3 +102,25 @@ def combine(default, override): combined = default.copy() combined.update(override) return combined + + +def jmespath(value, query): + ''' + Queries the data using the JMESPath query language. + Examples: + >>> import six + >>> locations = [{'name': 'Seattle', 'state': 'WA'}, + ... {"name": "New York", "state": "NY"}, + ... {"name": "Bellevue", "state": "WA"}, + ... {"name": "Olympia", "state": "WA"}] + >>> query = "[?state == 'WA'].name | sort(@) | {WashingtonCities: join(', ', @)}" # noqa: E501 + >>> WACities = jmespath(locations, query) + >>> if six.PY2: + ... WACities == {u'WashingtonCities': u'Bellevue, Olympia, Seattle'} + ... elif six.PY3: + ... WACities == {'WashingtonCities': 'Bellevue, Olympia, Seattle'} + ... + True + ''' + import jmespath + return jmespath.search(query, value) diff --git a/tox.ini b/tox.ini index 4bd8d644c8ef0caf346721f085c88929fd92108d..772e7c959ff9ff451888b69dcb978d573d9a8b95 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py{2,3} +envlist = py{2,3},docs [travis] python = @@ -17,6 +17,7 @@ deps = check-manifest readme_renderer flake8 + six commands = check-manifest --ignore tox.ini,tests* flake8 .