[Tutor] slow html generation code

Luis N tegmine at gmail.com
Thu Mar 3 00:59:58 CET 2005


This code seems a little slow, is there anything in particular that
jumps out as being not quite right.

The idea is that a file is opened that contains path names to other
files, that are appended and outputed into a directory of choice.

I plan to move this off the filesystem into a database when the
concept is worked out, maybe that will help.

#!/usr/local/bin/python

import sys
import os
from string import Template
from textile import textile

def main(files):
    if len(files) < 1:
        print 'Feed me with XHTML, String Templates, and Textile'
    else:
        path = os.path.expanduser('~/')
        publish = os.path.join(path, 'publish')
        pages = os.path.join(publish, 'pages')
        write = os.path.join(path, 'public_html/write')
        try:
            for i in files:
                page = os.path.join(write, i)
                f = open(os.path.join(pages, i), 'r')
                tmp = ""
                for line in f.readlines():
                    line = line.rstrip()
                    structure = open(os.path.join(publish, line))
                    clean = structure.read()
                    tmp = tmp + clean.rstrip()
                    txt = textile(tmp) + '</body></html>'
                    t = Template(txt)
                    s = t.safe_substitute(title='Web-siter: %s' % i[:-5])
                    output = open(page, 'w')
                    output.write('')
                    output.write(s)
        except:
            pass

if __name__ == '__main__':
    main(sys.argv[1:])


More information about the Tutor mailing list