Code

90c8f4996d1e447b1bf55bc7ac4709bf07be92ea
[roundup.git] / roundup / templatebuilder.py
1 # $Id: templatebuilder.py,v 1.7 2001-07-30 00:06:52 richard Exp $
2 import errno
4 preamble = """ 
5 # Do Not Edit (Unless You Want To)
6 # This file automagically generated by roundup.htmldata.makeHtmlBase
7
8 """
10 def makeHtmlBase(templateDir):
11     """ make a htmlbase.py file in the given templateDir, from the
12         contents of templateDir/html """
13     import os, glob, re
14     print "packing up templates in", templateDir
15     filelist = glob.glob(os.path.join(templateDir, 'html', '*'))
16     filelist = filter(os.path.isfile, filelist) # only want files
17     filelist.sort()
18     fd = open(os.path.join(templateDir, 'htmlbase.py'), 'w')
19     fd.write(preamble)
20     for file in filelist:
21         mangled_name = os.path.basename(re.sub(r'\.', 'DOT', file))
22         fd.write('%s = """'%mangled_name)
23         fd.write(open(file).read())
24         fd.write('"""\n\n')
25     fd.close()
27 def installHtmlBase(template, installDir):
28     """ passed a template package and an installDir, unpacks the html files into
29       the installdir """
30     import os,sys,re
32     tdir = __import__('roundup.templates.%s.htmlbase'%template).templates
33     if hasattr(tdir, template):
34         tmod = getattr(tdir, template)
35     else:
36         raise "TemplateError", "couldn't find roundup.template.%s.htmlbase"%template
37     htmlbase = tmod.htmlbase
38     installDir = os.path.join(installDir, 'html')
39     try:
40         os.makedirs(installDir)
41     except OSError, error:
42         if error.errno != errno.EEXIST: raise
44 #    print "installing from", htmlbase.__file__, "into", installDir
45     modulecontents = dir(htmlbase)
46     for mangledfile in modulecontents:
47         if mangledfile[0] == "_": 
48             continue
49         filename = re.sub('DOT', '.', mangledfile)
50         outfile = os.path.join(installDir, filename)
51         outfd = open(outfile, 'w')
52         data = getattr(htmlbase, mangledfile)
53         outfd.write(data)
54     
57 if __name__ == "__main__":
58     import sys
59     if len(sys.argv) == 2:
60         makeHtmlBase(sys.argv[1])
61     elif len(sys.argv) == 3:
62         installHtmlBase(sys.argv[1], sys.argv[2])
63     else:
64         raise "what you talkin about willis?"
66 #
67 # $Log: not supported by cvs2svn $
68 # Revision 1.6  2001/07/29 07:01:39  richard
69 # Added vim command to all source so that we don't get no steenkin' tabs :)
70 #
71 #
72 #
73 # vim: set filetype=python ts=4 sw=4 et si