import kid import os from config import config import logging import mimeparse import md5 HTML_SER = kid.HTMLSerializer(doctype=("HTML",), encoding='utf-8', inject_type=False) extensions = { 'html': ('text/html; charset=utf-8', HTML_SER), 'xhtml': ('text/html; charset=utf-8', HTML_SER), 'atom': ('application/atom+xml; charset=utf-8', 'xml'), 'svc': ('application/atomsvc+xml; charset=utf-8', 'xml') } matching = { 'text/html; charset=utf-8': 'html', 'text/html; charset=utf-8': 'xhtml' } def etag_from_raw_etag(raw_etag, template_file): file=os.path.join(config.get("templates", "dir"), template_file) last_modified = str(os.stat(file).st_mtime) hash = md5.new(raw_etag) hash.update(last_modified) return '"%s"' % hash.hexdigest() def render(environ, start_response, template_file, vars, headers={}, status="200 Ok", raw_etag=None): if raw_etag: etag = etag_from_raw_etag(raw_etag, template_file) headers['etag'] = etag if etag == environ.get('HTTP_IF_NONE_MATCH', ''): return http304(environ, start_response) (contenttype, serialization) = ('text/html; charset=utf-8', 'html') ext = template_file.rsplit(".") if len(ext) > 1 and (ext[1] in extensions): (contenttype, serialization) = extensions[ext[1]] file=os.path.join(config.get("templates", "dir"), template_file) template = kid.Template(file, **vars) body = template.serialize(output=serialization, encoding='utf-8') headers['Content-Type'] = contenttype start_response(status, list(headers.iteritems())) return [body] def expand_uri_template(uri, params): # Add braces around each of the keys and %-expand all the values quoted_params = dict([("{%s}" % key, urllib.quote(value.encode('utf-8'))) for key, value in params.iteritems()]) # define a function that we will later use to do the # regular expression substitution. Replace matches # only if they have valus in quoted_uri_parameters. def replace(match): return quoted_params.get(match.group(0), match.group(0)) return re.sub(r"{.*?}", replace, uri) def render_json(start_response, struct): import simplejson body = simplejson.dumps(struct) start_response("200 OK", [('Content-Type', 'text/plain')]) return [body] def http404(environ, start_response): logging.warning("404: %s" % environ.get('PATH_INFO', '')) start_response("404 Not Found", [('Content-Type', "text/html")]) return ["

File Not Found

"] def http304(environ, start_response): logging.info("304: %s" % environ.get('PATH_INFO', '')) start_response("304 Not Modified", []) return [] def http405(environ, start_response): start_response("405 Method Not Allowed", [('Content-Type', "text/html")]) return ["

That action is not allowed on this resource.

"] def http403(environ, start_response): start_response("403 Forbidden", [('Content-Type', "text/html")]) return ["

You are unauthorized to modify that resource.

"] def http415(environ, start_response, message="The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method."): start_response("415 Unsupported Media Type", [('Content-Type', "text/html")]) return [message]