~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/tornado/website/website.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# Copyright 2009 Bret Taylor
 
4
#
 
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
 
6
# not use this file except in compliance with the License. You may obtain
 
7
# a copy of the License at
 
8
#
 
9
#     http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
11
# Unless required by applicable law or agreed to in writing, software
 
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
14
# License for the specific language governing permissions and limitations
 
15
# under the License.
 
16
 
 
17
import markdown
 
18
import os
 
19
import os.path
 
20
import time
 
21
import tornado.web
 
22
import tornado.wsgi
 
23
import wsgiref.handlers
 
24
 
 
25
 
 
26
class ContentHandler(tornado.web.RequestHandler):
 
27
    def get(self, path):
 
28
        paths = ("documentation", "index")
 
29
        if not path: path = "index"
 
30
        if path not in paths:
 
31
            raise tornado.web.HTTPError(404)
 
32
        self.render(path + ".html", markdown=self.markdown)
 
33
 
 
34
    def markdown(self, path, toc=False):
 
35
        if not hasattr(ContentHandler, "_md") or self.settings.get("debug"):
 
36
            ContentHandler._md = {}
 
37
        if path not in ContentHandler._md:
 
38
            full_path = os.path.join(self.settings["template_path"], path)
 
39
            f = open(full_path, "r")
 
40
            contents = f.read().decode("utf-8")
 
41
            f.close()
 
42
            if toc: contents = u"[TOC]\n\n" + contents
 
43
            md = markdown.Markdown(extensions=["toc"] if toc else [])
 
44
            ContentHandler._md[path] = md.convert(contents).encode("utf-8")
 
45
        return ContentHandler._md[path]
 
46
 
 
47
 
 
48
settings = {
 
49
    "template_path": os.path.join(os.path.dirname(__file__), "templates"),
 
50
    "xsrf_cookies": True,
 
51
    "debug": os.environ.get("SERVER_SOFTWARE", "").startswith("Development/"),
 
52
}
 
53
application = tornado.wsgi.WSGIApplication([
 
54
    (r"/([a-z]*)", ContentHandler),
 
55
], **settings)
 
56
 
 
57
 
 
58
def main():
 
59
    wsgiref.handlers.CGIHandler().run(application)
 
60
 
 
61
 
 
62
if __name__ == "__main__":
 
63
    main()