import os
import errno
from shutil import copyfile
from quik import Template, FileLoader
import urllib

def mkdirp(dirname):
    try:
        os.mkdir(dirname)
    except OSError as exc:
        if exc.errno == errno.EEXIST and os.path.isdir(dirname):
            print "dir already exists {}".format(dirname)
            pass

class DirectoryRender:
    def __init__(self, basedir):
        self.loader = FileLoader(basedir)
        self.basedir = basedir

    def render(self, destdir, inputhash, currpath = ""):
        """
        Transforms all templates and downloads all files in the directory
        supplied with the input values supplied. Output goes in destdir.

        Keyword arguments:
            destdir -- the directory to put the rendered files into
            inputhash -- the dictionary of input values
        """
        items = os.listdir(os.path.join(self.basedir, currpath))

        for filename in items:
            relpath = os.path.join(currpath, filename)
            cur = os.path.join(self.basedir, relpath)
            dest = os.path.join(destdir, relpath)
            if os.path.isdir(cur):
                print "Making directory {}".format(dest)
                mkdirp(dest)
                self.render(destdir, inputhash, relpath)
            else:
                print "cur {} is not a directory".format(cur)
                if filename.endswith('.tpl'):
                    template = self.loader.load_template(relpath)
                    output = template.render(inputhash, loader=self.loader)
                    with open(dest.rstrip('.tpl'), 'w') as outfile:
                        outfile.write(output)
                elif filename.endswith('.download'):
                    with open(cur, 'r') as downloadfile:
                        (url, checksum) = downloadfile.read().split(None, 3)

                        print "Downloading {} to {}".format(url, dest)
                        urllib.urlretrieve(url, dest.rstrip('.download'))
                else:
                    copyfile(cur, dest)



@@ -321,16 +321,601 @@
   pass%0A%0A
+def ext_template(loader, inputhash, relpath, cur, dest):%0A    template = loader.load_template(relpath)%0A    output = template.render(inputhash, loader=loader)%0A    with open(dest.rstrip('.tpl'), 'w') as outfile:%0A        outfile.write(output)%0A%0Adef ext_download(loader, inputhash, relpath, cur, dest):%0A    with open(cur, 'r') as downloadfile:%0A        (url, checksum) = downloadfile.read().split(None, 3)%0A%0A        print %22Downloading %7B%7D to %7B%7D%22.format(url, dest)%0A        urllib.urlretrieve(url, dest.rstrip('.download'))%0A%0Aextension_handles = %7B'tpl' : ext_template, 'download' : ext_download%7D%0A%0A
 class Di
@@ -1950,292 +1950,62 @@
     
-if filename.endswith('.tpl'):%0A                    template = self.loader.load_template(relpath)%0A                    output = template.render(inputhash, loader=self.loader)%0A                    with open(dest.rstrip('.tpl'), 'w') as outfile:%0A                        outfile.write(output
+ext = os.path.splitext(filename)%5B1%5D%5B1:%5D.strip().lower(
 )%0A  
@@ -2022,323 +2022,122 @@
     
-el
 if 
-filename.endswith('.download'):%0A                    with open(cur, 'r') as downloadfile:%0A                        (url, checksum) = downloadfile.read().split(None, 3)%0A%0A                        print %22Downloading %7B%7D to %7B%7D%22.format(url, dest)%0A                        urllib.urlretrieve(url, dest.rstrip('.download')
+ext in extension_handles:%0A                    extension_handles%5Bext%5D(self.loader, inputhash, relpath, cur, dest
 )%0A  

Move to handler model for extensions