summaryrefslogtreecommitdiff
path: root/sandbox/py-rest-doc/sphinx/web/application.py
diff options
context:
space:
mode:
authorblackbird <blackbird@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2007-05-22 10:20:26 +0000
committerblackbird <blackbird@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2007-05-22 10:20:26 +0000
commita566bb8f36bd66041b4ad2013c1bf1b1ab671600 (patch)
treed8514028f29e0190cbf40b0f0ac4507d0e787571 /sandbox/py-rest-doc/sphinx/web/application.py
parent40324a6a49bc5a241f8139f94203e820bf90e6f2 (diff)
downloaddocutils-a566bb8f36bd66041b4ad2013c1bf1b1ab671600.tar.gz
added basic WSGI application for the documentation (and ported too many werkzeug features over, after the app is finished we should remove the unused)
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@5079 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'sandbox/py-rest-doc/sphinx/web/application.py')
-rw-r--r--sandbox/py-rest-doc/sphinx/web/application.py92
1 files changed, 92 insertions, 0 deletions
diff --git a/sandbox/py-rest-doc/sphinx/web/application.py b/sandbox/py-rest-doc/sphinx/web/application.py
new file mode 100644
index 000000000..2745649f8
--- /dev/null
+++ b/sandbox/py-rest-doc/sphinx/web/application.py
@@ -0,0 +1,92 @@
+# -*- coding: utf-8 -*-
+"""
+ sphinx.web.application
+ ~~~~~~~~~~~~~~~~~~~~~~
+
+ A simple WSGI application that serves an interactive version
+ of the python documentation.
+
+ :copyright: 2007 by Armin Ronacher.
+ :license: Python license.
+"""
+from __future__ import with_statement
+import cPickle as pickle
+from os import path
+from ..util import relative_uri
+from .util import Request, Response, SharedDataMiddleware, NotFound, \
+ render_template
+
+
+special_urls = set(['index', 'genindex', 'modindex', 'search'])
+
+
+def get_target_uri(source_filename):
+ if source_filename == 'index.rst':
+ return ''
+ if source_filename.endswith('/index.rst'):
+ return source_filename[:-9] # up to /
+ return source_filename[:-4] + '/'
+
+
+class DocumentationApplication(object):
+
+ def __init__(self, conf):
+ self.cache = {}
+ self.data_root = conf['data_root_path']
+
+ def get_page(self, req, url):
+ try:
+ filename, mtime, text = self.cache[url]
+ except KeyError:
+ pass
+ else:
+ if path.getmtime(filename) == mtime:
+ return Response(text)
+
+ if url in special_urls:
+ filename = path.join(self.data_root, 'specials.pickle')
+ with open(filename, 'rb') as f:
+ context = pickle.load(f)
+ templatename = url + '.html'
+
+ else:
+ for filename in [path.join(self.data_root, url) + '.fpickle',
+ path.join(self.data_root, url, 'index.fpickle')]:
+ if not path.exists(filename):
+ continue
+ with open(filename, 'rb') as f:
+ context = pickle.load(f)
+ break
+ else:
+ raise NotFound()
+ templatename = 'page.html'
+
+ def relative_path_to(otheruri, resource=False):
+ if not resource:
+ otheruri = get_target_uri(otheruri)
+ return relative_uri(url + '/', otheruri)
+ context['pathto'] = relative_path_to
+
+ text = render_template(templatename, context)
+ self.cache[url] = (filename, path.getmtime(filename), text)
+ return Response(text)
+
+ def get_close_matches(self, req, url):
+ raise NotImplementedError(url)
+
+ def __call__(self, environ, start_response):
+ req = Request(environ)
+ url = req.path.strip('/') or 'index'
+ try:
+ resp = self.get_page(req, url)
+ except NotFound:
+ resp = self.get_close_matches(req, url)
+ return resp(environ, start_response)
+
+
+def make_app(conf=None):
+ app = DocumentationApplication(conf or {})
+ app = SharedDataMiddleware(app, {
+ '/style': path.join(path.dirname(__file__), '..', 'style')
+ })
+ return app