changeset 39:a2cdce0108e1

get directory substitution sorta working; start stubbing mixed case
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 01 Jan 2011 23:00:59 -0800
parents 9739212a63c3
children 6b4c8f23192f
files examples/directory-example/{{subdir}}/{{bar}}.txt examples/doctest.txt makeitso/makeitso.py
diffstat 3 files changed, 39 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/directory-example/{{subdir}}/{{bar}}.txt	Sat Jan 01 23:00:59 2011 -0800
@@ -0,0 +1,1 @@
+{{foo}}
--- a/examples/doctest.txt	Sat Jan 01 22:34:29 2011 -0800
+++ b/examples/doctest.txt	Sat Jan 01 23:00:59 2011 -0800
@@ -42,6 +42,18 @@
     >>> template = makeitso.PolyTemplate([exampledir], output=tempdir, interactive=False)
     >>> sorted(template.missing())
     ['bar', 'foo', 'subdir']
-    >>> template.substitute(foo='myfoo', bar='mybar', subdir='mysubdir')
-    >>> os.listdir(tempdir)
-    
+    >>> template.substitute(foo='It', bar='life', subdir='mysubdir')
+    >>> sorted(os.listdir(tempdir))
+    ['foo.txt', 'mysubdir']
+    >>> file(os.path.join(tempdir, 'foo.txt')).read().strip()
+    'It is a wonderful life'
+    >>> os.listdir(os.path.join(tempdir, 'mysubdir'))
+    ['life.txt']
+    >>> file(os.path.join(tempdir, 'mysubdir', 'life.txt')).read().strip()
+    'It'
+    >>> shutil.rmtree(tempdir)
+
+Mixed case:
+
+    >>> template = makeitso.PolyTemplate([example, exampledir])
+
--- a/makeitso/makeitso.py	Sat Jan 01 22:34:29 2011 -0800
+++ b/makeitso/makeitso.py	Sat Jan 01 23:00:59 2011 -0800
@@ -245,6 +245,8 @@
         return missing
 
     def _substitute(self, **variables):
+        # TODO: do this with recursion instead of os.walk so that
+        # per-directory control may be asserted
 
         # make output directory if necessary
         output = self.output
@@ -255,14 +257,30 @@
             
             # interpolate directory names
             for d in dirnames:
-                path = os.path.join(dirname, d)                
-                interpolated = ContentTemplate(path).substitute(**variables)                
-                if os.path.exists(interpolated):
+                path = os.path.join(dirname, d)
+                interpolated = ContentTemplate(path).substitute(**variables)
+                target = os.path.join(self.output, interpolated.split(self.name, 1)[-1].strip(os.path.sep))
+                
+                if os.path.exists(target):
                     # ensure its a directory
-                    pass
+                    # TODO: check this first before interpolation is in progress
+                    assert os.path.isdir(target), "Can't substitute a directory on top of the file"
                 else:
-                    os.makedirs(interpolated)
+                    os.makedirs(target)
+
+            # interpolate files
+            for filename in filenames:
+                path = os.path.join(dirname, filename)
+                interpolated = ContentTemplate(path).substitute(**variables)
+                target = os.path.join(self.output, interpolated.split(self.name, 1)[-1].strip(os.path.sep))
                 
+                if os.path.exists(target):
+                    # ensure its a directory
+                    # TODO: check this first before interpolation is in progress
+                    assert os.path.isfile(target), "Can't substitute a file on top of a directory"
+                template = URITemplate(path, output=target, interactive=False)
+                template.substitute(**variables)
+
 
 class PolyTemplate(ContentTemplate):
     """