From 6cea160bf9065b855cd38ed08c6dce9f6cde8890 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Wed, 9 Nov 2016 08:04:52 +0200
Subject: [PATCH] - TOML filters (WIP).

---
 README.rst          |  2 ++
 setup.py            |  2 +-
 template/filters.py | 10 ++++++++++
 tests.sh            |  8 ++++++++
 4 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/README.rst b/README.rst
index 10ea16a..994373e 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 68c110a..de9bd4e 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 018c9ac..bac6b5b 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 ee620fb..0a7db48 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"
-- 
GitLab