diff --git a/README.rst b/README.rst index fb68c2cff54f7baf6c3dff63df3557ed6c3f5244..701e6b272bdbf550eda03cbd7b7ffcf275cb7dd3 100644 --- a/README.rst +++ b/README.rst @@ -8,13 +8,25 @@ Example .. code:: shell + $ template -h + usage: template [-h] [-o OUTPUT] [filename] + + positional arguments: + filename Input filename + + optional arguments: + -h, --help show this help message and exit + -o OUTPUT, --output OUTPUT + Output to filename $ export name='John' $ echo 'Hello {{ name if name is defined else 'world' }}. | template Hello John. - + $ echo '{{ USER }}' > username.j2 + $ template --output username.txt username.j2 + $ cat username + John TODO ---- -- Input/output detection/redirection. - Complex data types (process environment variables, Jinja filters). diff --git a/VERSION b/VERSION index 6c6aa7cb0918dc7a1cfa3635fb7f8792ac4cb218..341cf11faf9a29504168de4e54beaad182c5adc5 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.0 \ No newline at end of file +0.2.0 \ No newline at end of file diff --git a/template/__init__.py b/template/__init__.py index c4817de2a1c4a7bddb6fca92791bee4b8232f712..e34e3eb41eb45a20eb051115038e28379ef16a7f 100755 --- a/template/__init__.py +++ b/template/__init__.py @@ -5,7 +5,9 @@ from __future__ import (absolute_import, division, print_function, unicode_literals) from jinja2 import Template from os import environ -from sys import stdin +from sys import stdin, stdout +import argparse +from argparse import ArgumentParser def render(template): @@ -13,13 +15,19 @@ def render(template): return t.render(environ) -def usage(): - raise NotImplemented - - def main(): - template = stdin.read() - print(render(template)) + parser = ArgumentParser() + parser.add_argument('filename', + help='Input filename', + type=argparse.FileType('r'), + nargs='?') + parser.add_argument('-o', '--output', + help='Output to filename', + type=argparse.FileType('w')) + args = parser.parse_args() + infd = args.filename if args.filename else stdin + outfd = args.output if args.output else stdout + print(render(infd.read()), file=outfd) if __name__ == '__main__': main() diff --git a/template/filters.py b/template/filters.py new file mode 100644 index 0000000000000000000000000000000000000000..355b82095561c684fffe90eb1312768855dad490 --- /dev/null +++ b/template/filters.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python + + +def to_yaml(value): + raise NotImplemented + + +def to_json(value): + raise NotImplemented + + +def from_json(value): + raise NotImplemented + + +def from_yaml(value): + raise NotImplemented + + +def pprint(value): + raise NotImplemented + + +def combine(lefthand, righthand): + raise NotImplemented diff --git a/tests.sh b/tests.sh index 7553718a3b3962703cb3426894af67821d7bab91..474723022216230dad34169f89c8c1e11a89c8a7 100755 --- a/tests.sh +++ b/tests.sh @@ -1,3 +1,10 @@ #!/bin/sh -e -test "$(echo '{{ name }}' | name='Nimrod' template)" = "Nimrod" +export name='John' +test "$(echo 'Hello {{ name if name is defined else 'world' }}.' | template)" = "Hello John." +export infile="$(mktemp)" +export outfile="$(mktemp)" +echo '{{ USER }}' > "$infile" +template --output "$outfile" "$infile" +test "$(cat $outfile)" = "$USER" +rm "$infile" "$outfile"