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

- Added input/output file redirection arguments.

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