| 1 | from config import template_path | 1 | from config import template_path |
|---|
| 2 | | 2 | |
|---|
| 3 | from Cheetah.Template import Template | 3 | from Cheetah.Template import Template |
|---|
| 4 | import os, cgi, re | 4 | import os, cgi, re |
|---|
| 5 | | 5 | |
|---|
| 6 | def dispatch (actions, input = None, environ = os.environ): | 6 | def dispatch (actions, input = None, environ = os.environ): |
|---|
| 7 | if input is None: input = cgi.FieldStorage() | 7 | if input is None: input = cgi.FieldStorage() |
|---|
| 8 | pathinfo = '' | 8 | pathinfo = '' |
|---|
| 9 | if environ.has_key("PATH_INFO"): | 9 | if environ.has_key("PATH_INFO"): |
|---|
| 10 | pathinfo = input.getvalue("path", environ["PATH_INFO"]) | 10 | pathinfo = input.getvalue("path", environ["PATH_INFO"]) |
|---|
| 11 | reqmethod = environ["REQUEST_METHOD"] | 14 | reqmethod = environ["REQUEST_METHOD"] |
|---|
| 12 | script_name = environ["SCRIPT_NAME"] | 15 | script_name = environ["SCRIPT_NAME"] |
|---|
| 13 | host = environ["HTTP_HOST"] | 16 | host = environ["HTTP_HOST"] |
|---|
| 14 | args = pathinfo[1:].split("/") | 17 | args = pathinfo[1:].split("/") |
|---|
| 15 | if hasattr(actions, "__getitem__"): | 18 | if hasattr(actions, "__getitem__"): |
|---|
| 16 | classobj = actions[args[0]] | 19 | classobj = actions[args[0]] |
|---|
| 17 | else: | 20 | else: |
|---|
| 18 | classobj = getattr(actions, args[0]) | 21 | classobj = getattr(actions, args[0]) |
|---|
| 19 | handler = classobj(script_name, input, host, reqmethod) | 22 | handler = classobj(script_name, input, host, reqmethod) |
|---|
| 20 | method = getattr(handler, reqmethod) | 23 | method = getattr(handler, reqmethod) |
|---|
| 21 | content = method(*args[1:]) | 24 | content = method(*args[1:]) |
|---|
| 22 | for header in handler.headers.iteritems(): | 25 | for header in handler.headers.iteritems(): |
|---|
| 23 | print "%s: %s" % header | 26 | print "%s: %s" % header |
|---|
| 24 | print "" | 27 | print "" |
|---|
| 25 | print content | 28 | print content |
|---|
| 26 | | 29 | |
|---|
| 27 | class Document: | 30 | class Document: |
|---|
| 28 | def __init__ (self, script_name, input, host, req_method): | 31 | def __init__ (self, script_name, input, host, req_method): |
|---|
| 29 | self.script_name = script_name | 32 | self.script_name = script_name |
|---|
| 30 | self.input = input | 33 | self.input = input |
|---|
| 31 | self.host = host | 34 | self.host = host |
|---|
| 32 | self.req_method = req_method | 35 | self.req_method = req_method |
|---|
| 33 | self.headers = {'Content-type': 'text/plain; charset=utf-8'} | 36 | self.headers = {'Content-type': 'text/plain; charset=utf-8'} |
|---|
| 34 | self.template_path = template_path + "/" | 37 | self.template_path = template_path + "/" |
|---|
| 35 | | 38 | |
|---|
| 36 | def template (self, name, **args): | 39 | def template (self, name, **args): |
|---|
| 38 | return Template( file = filename, searchList = [args, self] ) | 41 | return Template( file = filename, searchList = [args, self] ) |
|---|
| 39 | | 42 | |
|---|
| 40 | def output (self, name, **args): | 43 | def output (self, name, **args): |
|---|
| 41 | return self.template(name, **args) | 44 | return self.template(name, **args) |
|---|
| 42 | #return self.template("base", page = page) | 45 | #return self.template("base", page = page) |
|---|
| 43 | | 46 | |
|---|
| 44 | | 47 | |
|---|