#!/usr/bin/env python
import os
import time
import sys
import string
import cgi
import ssx
import imp
def handle_module(xml, module, path = ['.'], **kw):
if len(module) and module[0] == '/': module = module[1:]
request = module
if len(module) and module[-1] == '/': module = module[:-1]
module = string.replace(module, '/', '.')
module = module or 'index'
#XXX: When fd returned by find_module have to be closed?
try:
mh = imp.find_module(module, path)
except ImportError, e:
os.stat(request)
raise e
sub = imp.load_module(module, *mh)
return sub.handler(xml, **kw)
def main(docbase = '',xsl = 'ssx/ssx.xslt', xmlns = {}, xmlextra = "", output = 'xslt', xmlbase = None, **kw):
xml = ssx.SSXML()
xml.data("\n" % (docbase, xsl))
xml.push("ssx:doc", """
xmlns='http://www.w3.org/1999/xhtml'
xmlns:xi='http://www.w3.org/2001/XInclude'
xmlns:ssx='%s'
%s
""" % (ssx.xmlns, "\n".join(map(lambda x: "xmlns:%s='%s'" % x, xmlns.items()))))
if xmlbase: xml.data("%s\n" % xmlbase)
module = os.environ.get("PATH_INFO", "index")
timer = time.time()
xml.push("ssx:body")
xml.data(xmlextra)
try:
try:
xml = handle_module(xml, module, **kw) or xml
finally:
xml.pop("ssx:body")
except ssx.SSXRedirect, e:
sys.stdout.write("Status: 301 Moved Permanantly\n")
sys.stdout.write("Location: %s\n\n" % (e.url))
return
except ImportError, e:
sys.stdout.write("Status: 302 Found\n")
#sys.stdout.write("Status: 301 Moved Permanantly\n")
sys.stdout.write("Location: %s%s\n\n" % (docbase, module))
return
except RuntimeError, e:
xml.data(""+ ssx.xmlq(e) +"\n");
except Exception, e:
#xml.data("Internal error\n");
import traceback
xml.data("%s%s
" % (ssx.xmlq(e), ssx.xmlq(traceback.format_exc())))
except:
xml.data("Internal error\n");
xml.push("ssx:meta")
xml.data(""+time.ctime()+"\n");
#xml.data("%.3f" % (timer - req.request_time))
xml.data("%.3f" % (time.time() - timer))
#if output != "xml":
xml.data("")
# sep = "?"
#if req.unparsed_uri.find('?') > 0:
# sep = '&'
#xml.data("xml\n" % (ssx.xmlq(req.unparsed_uri), sep))
xml.pop()
timer = time.time()
if output == "xml":
sys.stdout.write("Content-Type: text/xml; charset=UTF-8\n\n")
sys.stdout.write(xml.text());
return
xslt = ssx.SSXSLT(xsl)
#xslt = ssx.SSXSLT(document_root + "/" + xsl)
tmp = xslt.xslt(xml = xml.xml(xi = True))
tmp = string.replace(tmp, "", "%.3f" % (time.time() - timer))
sys.stdout.write("Content-Type: text/xml\n\n")
sys.stdout.write(tmp)
if __name__ == "__main__":
try:
main("xslt/mgp.xslt", "xslt")
except Exception, e:
sys.stdout.write("Content-Type: text/plain\n\n")
sys.stdout.write(str(e))
import traceback
s