Code

9f96673b9cf0367acefe3fcf7ef3ab32815a4cbf
[roundup.git] / roundup / templatebuilder.py
1 preamble = """ 
2 # Do Not Edit (Unless You Want To)
3 # This file automagically generated by roundup.htmldata.makeHtmlBase
4
5 """
7 def makeHtmlBase(templateDir):
8     """ make a htmlbase.py file in the given templateDir, from the
9         contents of templateDir/html """
10     import os, glob, re
11     print "packing up templates in", templateDir
12     filelist = glob.glob(os.path.join(templateDir, 'html', '*'))
13     filelist = filter(os.path.isfile, filelist) # only want files
14     filelist.sort()
15     fd = open(os.path.join(templateDir, 'htmlbase.py'), 'w')
16     fd.write(preamble)
17     for file in filelist:
18         mangled_name = os.path.basename(re.sub(r'\.', 'DOT', file))
19         fd.write('%s = """'%mangled_name)
20         fd.write(open(file).read())
21         fd.write('"""\n\n')
22     fd.close()
24 def installHtmlBase(template, installDir):
25     """ passed a template package and an installDir, unpacks the html files into
26       the installdir """
27     import os,sys,re
29     tdir = __import__('roundup.templates.%s.htmlbase'%template).templates
30     if hasattr(tdir, template):
31         tmod = getattr(tdir, template)
32     else:
33         raise "TemplateError", \
34                 "couldn't find roundup.template.%s.htmlbase"%template
35     htmlbase = tmod.htmlbase
36     installDir = os.path.join(installDir, 'html')
37     os.makedirs(installDir)
39     print "installing from", htmlbase.__file__, "into", installDir
40     modulecontents = dir(htmlbase)
41     for mangledfile in modulecontents:
42         if mangledfile[0] == "_": 
43             continue
44         filename = re.sub('DOT', '.', mangledfile)
45         outfile = os.path.join(installDir, filename)
46         outfd = open(outfile, 'w')
47         data = getattr(htmlbase, mangledfile)
48         outfd.write(data)
49     
52 if __name__ == "__main__":
53     import sys
54     if len(sys.argv) == 2:
55         makeHtmlBase(sys.argv[1])
56     elif len(sys.argv) == 3:
57         installHtmlBase(sys.argv[1], sys.argv[2])
58     else:
59         raise "what you talkin about willis?"