Skip to content
Snippets Groups Projects
Commit 256a1439 authored by nimrod's avatar nimrod
Browse files

- Removed six as a runtime requirement, now only required for testing.

- Added JMESPath support, updated docs, TODO list accordingly.
- Added docs to the locally run Tox environments (otherwise when I run
Tox locally this test won't be performed).
parent b0817717
Branches
Tags
No related merge requests found
...@@ -45,6 +45,8 @@ The following Jinja filters were added: ...@@ -45,6 +45,8 @@ The following Jinja filters were added:
- :code:`from_json`: Convert from json. - :code:`from_json`: Convert from json.
- :code:`pprint`: Pretty print variable. - :code:`pprint`: Pretty print variable.
- :code:`combine`: Combine 2 dictionaries. - :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 Example usage can be seen in :code:`tests.sh` and for specific filters in the
docstrings in :code:`template/filters.py`. docstrings in :code:`template/filters.py`.
...@@ -85,5 +87,4 @@ TODO ...@@ -85,5 +87,4 @@ TODO
- Release on tagged commits to PyPI in Travis CI - Release on tagged commits to PyPI in Travis CI
(https://docs.travis-ci.com/user/deployment/pypi/ and (https://docs.travis-ci.com/user/deployment/pypi/ and
https://docs.travis-ci.com/user/encryption-keys/). https://docs.travis-ci.com/user/encryption-keys/).
- Add JMESPath support.
- Add TOML support? - Add TOML support?
...@@ -21,7 +21,7 @@ setup( ...@@ -21,7 +21,7 @@ setup(
], ],
keywords='config configuration jinja template environment', keywords='config configuration jinja template environment',
packages=find_packages(), packages=find_packages(),
install_requires=['Jinja2', 'PyYAML', 'six'], install_requires=['Jinja2', 'PyYAML', 'jmespath'],
extras_require={ extras_require={
'dev': ['tox'], }, 'dev': ['tox'], },
entry_points={ entry_points={
......
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import (absolute_import, division, from __future__ import (absolute_import, division,
print_function, unicode_literals) print_function, unicode_literals)
import six # noqa: F401
def to_yaml(value): def to_yaml(value):
...@@ -45,6 +44,7 @@ def from_json(value): ...@@ -45,6 +44,7 @@ def from_json(value):
Returns native data structure from the given JSON string. Returns native data structure from the given JSON string.
Examples: Examples:
>>> import six
>>> from_json('[1, 2, 3]') >>> from_json('[1, 2, 3]')
[1, 2, 3] [1, 2, 3]
>>> from_json('"a"') == six.text_type('a') >>> from_json('"a"') == six.text_type('a')
...@@ -78,6 +78,7 @@ def pprint(value): ...@@ -78,6 +78,7 @@ def pprint(value):
Examples: Examples:
>>> pprint(1) >>> pprint(1)
'1' '1'
>>> import six
>>> output = pprint([{'first_name': 'John', 'last_name': 'Doe'}, {'first_name': 'Jane', 'last_name': 'Doe'}]) # noqa: E501 >>> output = pprint([{'first_name': 'John', 'last_name': 'Doe'}, {'first_name': 'Jane', 'last_name': 'Doe'}]) # noqa: E501
>>> if six.PY3: >>> if six.PY3:
... output == "[{'first_name': 'John', 'last_name': 'Doe'},\\n {'first_name': 'Jane', 'last_name': 'Doe'}]" ... output == "[{'first_name': 'John', 'last_name': 'Doe'},\\n {'first_name': 'Jane', 'last_name': 'Doe'}]"
...@@ -101,3 +102,25 @@ def combine(default, override): ...@@ -101,3 +102,25 @@ def combine(default, override):
combined = default.copy() combined = default.copy()
combined.update(override) combined.update(override)
return combined 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)
[tox] [tox]
envlist = py{2,3} envlist = py{2,3},docs
[travis] [travis]
python = python =
...@@ -17,6 +17,7 @@ deps = ...@@ -17,6 +17,7 @@ deps =
check-manifest check-manifest
readme_renderer readme_renderer
flake8 flake8
six
commands = commands =
check-manifest --ignore tox.ini,tests* check-manifest --ignore tox.ini,tests*
flake8 . flake8 .
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment