diff --git a/README.rst b/README.rst index 10ea16a40b25875a381f72f22bb61b51eb1f9a48..994373e329b61c5e7ad1307cb7a7edf21c01ff85 100644 --- a/README.rst +++ b/README.rst @@ -42,6 +42,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. Example usage can be seen in :code:`tests.sh`. diff --git a/setup.py b/setup.py index 68c110afa8b5485f2ca86cd5ae5c449e9f3fddd2..de9bd4e97030c70463a97c7ee37bd6651fb01de1 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'], + install_requires=['Jinja2', 'PyYAML', 'toml'], extras_require={ 'dev': ['tox'], }, entry_points={ diff --git a/template/filters.py b/template/filters.py index 018c9ac0422ab8a6a5ebf2e5949a4ce689a088ee..bac6b5b5162ebdf61f096b78f6efe2f796d88027 100644 --- a/template/filters.py +++ b/template/filters.py @@ -30,3 +30,13 @@ def combine(default, override): combined = default.copy() combined.update(override) return combined + + +def from_toml(value): + from toml import loads + return loads(value) + + +def to_toml(value): + from toml import dumps + return dumps(value) diff --git a/tests.sh b/tests.sh index ee620fb687634d1330a7d075e54322061cd59c96..0a7db480cb8c416cc7a72d05ce2112444c1bbc92 100755 --- a/tests.sh +++ b/tests.sh @@ -39,4 +39,12 @@ echo Testing combining dictionaries. echo '{{ {"a": 1, "b": 2}|combine({"a": 11, "c": 33}) }}' > "$infile" test "$(template $infile)" = "{'a': 11, 'c': 33, 'b': 2}" +echo Testing TOML parsing. +echo '{{ "[table]\n key = value" | from_toml }}' > "$infile" +test "$(template $infile)" = "table = {'key': 'value'}" + +echo Testing TOML output. +echo "{{ {'key': [1, 2]} | to_toml }}" > "$infile" +test "$(template $infile)" = "key = [ 1, 2,]" + rm "$infile" "$outfile"