changeset 26:044ac9e0b29c

add a non-working example with an include clause
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 13 Dec 2010 21:03:39 -0800
parents fbf19940fa97
children ac44c36da885
files examples/include-example.txt makeitso/makeitso.py
diffstat 2 files changed, 25 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/include-example.txt	Mon Dec 13 21:03:39 2010 -0800
@@ -0,0 +1,5 @@
+#!/usr/bin/env makeitso
+
+I am including a file:
+
+{{include(example.txt)}}
--- a/makeitso/makeitso.py	Tue Nov 30 09:23:46 2010 -0800
+++ b/makeitso/makeitso.py	Mon Dec 13 21:03:39 2010 -0800
@@ -21,7 +21,6 @@
 tempita_location = 'http://bitbucket.org/ianb/tempita/raw-file/tip/tempita/'
 
 def cleanup():
-
     # remove temporary net module directory
     if 'tempdir' in globals():
         shutil.remove(tempdir)
@@ -80,6 +79,19 @@
             cmdstr = ' '.join(command)
         raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code))
 
+def base_uri(uri):
+    if '://' in uri:
+        return 'uri'.rsplit('/', 1)[0]
+    return os.path.dirname(os.path.abspath('me'))
+
+def include(uri):
+    f, headers = urllib.urlretrieve(uri)
+    return file(f).read()
+
+### functions for variables
+
+defaults = {'include': include}
+
 def get_missing(name_error):
     """
     This is a horrible hack because python doesn't do the proper thing
@@ -126,7 +138,7 @@
     if shebang_re.match(content):
         content = os.linesep.join(content.splitlines()[1:])
 
-    variables = variables or {}
+    variables = variables or defaults.copy()
     template = tempita.Template(content)
     missing = missing_variables(template, variables)
     if missing:
@@ -180,7 +192,7 @@
         return
 
     # template variables
-    variables = {}
+    variables = defaults.copy()
     _vars = []
     _args = []
     for arg in args:
@@ -197,15 +209,16 @@
             for arg in args:
                 print invocation(arg, **variables)
         else:
-            print invocation('[URL]', **variables)
+            print invocation('[URI]', **variables)
         return
 
     # get the content
     if args:
         for arg in args:
-            f, headers = urllib.urlretrieve(arg)
-            content = file(f).read()
-            print substitute(content, variables=variables)
+            var_copy = variables.copy()
+            var_copy['here'] = base_uri(arg)
+            content = include(arg)
+            print substitute(content, variables=var_copy)
     else:
         content = sys.stdin.read()
         print substitute(content, variables=variables)