diff --git a/README.rst b/README.rst index f94db35187a6804e055ac3b65f928f45d5765c3f..1447ebb40278758bb8fe919e1d818c0582cd1b7c 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:`to_toml`: Convert to toml. +- :code:`from_toml`: Convert from toml. - :code:`jmespath`: Queries data using the `JMESPath <http://jmespath.org/>`_ query language. @@ -87,4 +89,3 @@ 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 TOML support? diff --git a/setup.py b/setup.py index 897402d55bf233104f1b4995a26614588a551092..c39c646fa2d92a1bce7447e1ebd125c152a53a78 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', 'jmespath'], + install_requires=['Jinja2', 'PyYAML', 'jmespath', 'toml'], extras_require={ 'dev': ['tox'], }, entry_points={ diff --git a/template/filters.py b/template/filters.py index 99447c536efea1cdee81a77f698aff4619b3b688..c2b99563f6ddd5d5f3755cc567ce97691051fcba 100644 --- a/template/filters.py +++ b/template/filters.py @@ -104,6 +104,29 @@ def combine(default, override): return combined +def from_toml(value): + ''' + Returns a data structure from the TOML string given. + Examples: + >>> from_toml('[table]\\nkey = "value"\\n') == {'table': {'key': 'value'}} + True + ''' + from toml import loads + return loads(value) + + +def to_toml(value): + ''' + Returns a string of the TOML representation for the data structure given. + Examples: + >>> import six + >>> to_toml({'key': [1, 2]}) == six.text_type("key = [ 1, 2,]\\n") + True + ''' + from toml import dumps + return dumps(value) + + def jmespath(value, query): ''' Queries the data using the JMESPath query language.