diff --git a/README.rst b/README.rst index b65a8ded8366ba5c42f68dff3c24382cc676c0ab..10ea16a40b25875a381f72f22bb61b51eb1f9a48 100644 --- a/README.rst +++ b/README.rst @@ -1,7 +1,8 @@ Template ######## -A CLI tool for generating files from Jinja2 templates and environment variables. +A CLI tool for generating files from Jinja2 templates and environment +variables. Examples -------- @@ -11,6 +12,9 @@ Examples $ template -h usage: template [-h] [-o OUTPUT] [filename] + A CLI tool for generating files from Jinja2 templates and environment + variables. + positional arguments: filename Input filename @@ -23,7 +27,7 @@ Examples Hello John. $ echo '{{ USER }}' > username.j2 $ template --output username.txt username.j2 - $ cat username + $ cat username.txt John @@ -32,12 +36,12 @@ 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. +- :code:`to_yaml`: Convert to yaml. +- :code:`from_yaml`: Convert from yaml. +- :code:`to_json`: Convert to json. +- :code:`from_json`: Convert from json. +- :code:`pprint`: Pretty print variable. +- :code:`combine`: Combine 2 dictionaries. Example usage can be seen in :code:`tests.sh`. diff --git a/template/__init__.py b/template/__init__.py index f476a5cdb163df62c18a565e4ed83f15fff5331d..3b0e5674ab6a6aa65de92ce9e9ccfadd062f2c06 100755 --- a/template/__init__.py +++ b/template/__init__.py @@ -21,7 +21,9 @@ def render(template_string): def main(): - parser = ArgumentParser() + parser = ArgumentParser( + description='''A CLI tool for generating files from Jinja2 templates + and environment variables.''') parser.add_argument('filename', help='Input filename', type=argparse.FileType('r'), diff --git a/template/filters.py b/template/filters.py index 4fb5250089437896ba1ce20d3b9361c7fda780a6..018c9ac0422ab8a6a5ebf2e5949a4ce689a088ee 100644 --- a/template/filters.py +++ b/template/filters.py @@ -26,7 +26,7 @@ def pprint(value): return pformat(value) -def combine(lefthand, righthand): - combined = lefthand.copy() - combined.update(righthand) +def combine(default, override): + combined = default.copy() + combined.update(override) return combined diff --git a/tests.sh b/tests.sh index 256671433eedfa85fbd984211895af92d57aa2ad..fce6dc9113217902b4d93e15c5d0a304d0ca30ab 100755 --- a/tests.sh +++ b/tests.sh @@ -1,4 +1,5 @@ -#!/bin/sh -e +#!/bin/sh +set -eu export infile="$(mktemp)" export outfile="$(mktemp)" @@ -36,5 +37,7 @@ echo '{{ [1, ] + [2, ] }}' > "$infile" test "$(template $infile)" = "[1, 2]" echo Testing combining dictionaries. +echo '{{ {"a": 1, "b": 2}|combine({"a": 11, "c": 33}) }}' > "$infile" +test "$(template $infile)" = "{'a': 11, 'c': 33, 'b': 2}" rm "$infile" "$outfile"