# HG changeset patch # User Jeff Hammel # Date 1293951659 28800 # Node ID a2cdce0108e11e199fdc4de0367ae7d750397e6e # Parent 9739212a63c3fd181755a522bd8ff4af7206df46 get directory substitution sorta working; start stubbing mixed case diff -r 9739212a63c3 -r a2cdce0108e1 examples/directory-example/{{subdir}}/{{bar}}.txt --- /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}} diff -r 9739212a63c3 -r a2cdce0108e1 examples/doctest.txt --- 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]) + diff -r 9739212a63c3 -r a2cdce0108e1 makeitso/makeitso.py --- 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): """