From 381dfb4023dce829f8c4d8fa0662dbdef7a8a6f0 Mon Sep 17 00:00:00 2001 From: Adar Nimrod <nimrod@shore.co.il> Date: Mon, 14 Mar 2016 23:31:06 +0200 Subject: [PATCH] - Added some filters, updated tests and documentation accordingly. - Bumped version to 0.3.0. --- README.rst | 35 ++++++++++++++++++++++++++++++----- VERSION | 2 +- setup.py | 2 +- template/__init__.py | 11 ++++++++--- template/filters.py | 19 +++++++++++++------ tests.sh | 34 ++++++++++++++++++++++++++++++++-- 6 files changed, 85 insertions(+), 18 deletions(-) diff --git a/README.rst b/README.rst index 701e6b2..b65a8de 100644 --- a/README.rst +++ b/README.rst @@ -3,8 +3,8 @@ Template A CLI tool for generating files from Jinja2 templates and environment variables. -Example -------- +Examples +-------- .. code:: shell @@ -26,7 +26,32 @@ Example $ cat username John -TODO ----- -- Complex data types (process environment variables, Jinja filters). +Jinja filters +------------- + +The following Jinja filters were added: + +- to_yaml: Convert to yaml. +- from_yaml: Convert from yaml. +- to_json: Convert to json. +- from_json: Convert from json. +- pprint: Pretty print variable. +- combine: Combine 2 dictionaries. + +Example usage can be seen in :code:`tests.sh`. + + +License +------- + +This software is licensed under the AGPL 3+ license (see the :code:`LICENSE.txt` +file). + +Author +------ + +Nimrod Adar, `contact me <nimrod@shore.co.il>`_ or visit my `website +<https://www.shore.co.il/>`_. Patches are welcome via `git send-email +<http://git-scm.com/book/en/v2/Git-Commands-Email>`_. The repository is located +at: https://www.shore.co.il/git/. diff --git a/VERSION b/VERSION index 341cf11..9325c3c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.0 \ No newline at end of file +0.3.0 \ No newline at end of file diff --git a/setup.py b/setup.py index 7a70257..68c110a 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ setup( ], keywords='config configuration jinja template environment', packages=find_packages(), - install_requires=['Jinja2'], + install_requires=['Jinja2', 'PyYAML'], extras_require={ 'dev': ['tox'], }, entry_points={ diff --git a/template/__init__.py b/template/__init__.py index e34e3eb..f476a5c 100755 --- a/template/__init__.py +++ b/template/__init__.py @@ -3,15 +3,20 @@ from __future__ import (absolute_import, division, print_function, unicode_literals) -from jinja2 import Template +from jinja2 import Environment from os import environ from sys import stdin, stdout import argparse from argparse import ArgumentParser +import template.filters -def render(template): - t = Template(template) +def render(template_string): + env = Environment() + # Add all functions in template.filters as Jinja filters. + for tf in filter(lambda x: not x.startswith('_'), dir(template.filters)): + env.filters[tf] = template.filters.__getattribute__(tf) + t = env.from_string(template_string) return t.render(environ) diff --git a/template/filters.py b/template/filters.py index 355b820..4fb5250 100644 --- a/template/filters.py +++ b/template/filters.py @@ -2,24 +2,31 @@ def to_yaml(value): - raise NotImplemented + from yaml import safe_dump + return safe_dump(value) def to_json(value): - raise NotImplemented + from json import dumps + return dumps(value) def from_json(value): - raise NotImplemented + from json import loads + return loads(value) def from_yaml(value): - raise NotImplemented + from yaml import safe_load + return safe_load(value) def pprint(value): - raise NotImplemented + from pprint import pformat + return pformat(value) def combine(lefthand, righthand): - raise NotImplemented + combined = lefthand.copy() + combined.update(righthand) + return combined diff --git a/tests.sh b/tests.sh index 4747230..2566714 100755 --- a/tests.sh +++ b/tests.sh @@ -1,10 +1,40 @@ #!/bin/sh -e -export name='John' -test "$(echo 'Hello {{ name if name is defined else 'world' }}.' | template)" = "Hello John." export infile="$(mktemp)" export outfile="$(mktemp)" + +echo Basic test. +export name='John' +test "$(echo 'Hello {{ name if name is defined else 'world' }}.' | template)" = "Hello John." + +echo Testing arguments and reading/ writing to file. echo '{{ USER }}' > "$infile" template --output "$outfile" "$infile" test "$(cat $outfile)" = "$USER" + +echo Testing JSON parsing. +export json='{"a": 1, "b": 2}' +echo '{{ (json|from_json)["a"] }}' > "$infile" +test "$(template $infile)" = "1" + +echo Testing JSON output. +echo '{{ [1, 1+2, 3] | to_json }}' > "$infile" +test "$(template $infile)" = '[1, 3, 3]' + +echo Testing YAML parsing. +export yaml='a: 1 +b: 2' +echo '{{ (yaml|from_yaml)["a"] }}' > "$infile" +test "$(template $infile)" = "1" + +echo Testing YAML output. +echo '{{ [1, 1+2, 3] | to_yaml }}' > "$infile" +test "$(template $infile)" = '[1, 3, 3]' + +echo Testing pprint. +echo '{{ [1, ] + [2, ] }}' > "$infile" +test "$(template $infile)" = "[1, 2]" + +echo Testing combining dictionaries. + rm "$infile" "$outfile" -- GitLab