From a01a2a633c33d70b1f449996d5f3a9008466253b Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Mon, 14 Mar 2016 17:19:21 +0200
Subject: [PATCH] - Added input/output file redirection arguments. - Updated
 tests and documentation. - Bumped version to 0.2.0.

---
 README.rst           | 16 ++++++++++++++--
 VERSION              |  2 +-
 template/__init__.py | 22 +++++++++++++++-------
 template/filters.py  | 25 +++++++++++++++++++++++++
 tests.sh             |  9 ++++++++-
 5 files changed, 63 insertions(+), 11 deletions(-)
 create mode 100644 template/filters.py

diff --git a/README.rst b/README.rst
index fb68c2c..701e6b2 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 6c6aa7c..341cf11 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 c4817de..e34e3eb 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 0000000..355b820
--- /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 7553718..4747230 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"
-- 
GitLab