Skip to content
Snippets Groups Projects
Commit 381dfb40 authored by nimrod's avatar nimrod
Browse files

- Added some filters, updated tests and documentation accordingly.

- Bumped version to 0.3.0.
parent a01a2a63
No related branches found
No related tags found
No related merge requests found
......@@ -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/.
0.2.0
\ No newline at end of file
0.3.0
\ No newline at end of file
......@@ -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={
......
......@@ -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)
......
......@@ -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
#!/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"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment