Commits

Matthew Scott  committed b265db1 Merge

Merge from remote work

  • Participants
  • Parent commits b4437c3, 1c14b22

Comments (0)

Files changed (3)

 *.egg-info/*
 build/*
 dist/*
+.*.swp
     entry_points="""
     # -*- Entry points: -*-
     [console_scripts]
-    slipper_cgi = slipper.script:cgi
+    slipper_cgi = slipper.script:cgi_script
     """,
     )

File slipper/script.py

 from ConfigParser import ConfigParser
+from os.path import exists, join
+from sha import sha
 import sys
+from textwrap import dedent
 
+from docutils.core import publish_string
+from docutils.writers.html4css1 import Writer
 
-USAGE = """\
-Please visit http://getschevo.org/slipper/usage.html
-for usage information.
-"""
+import mercurial.hgweb.wsgicgi as wsgicgi
+from mercurial.localrepo import localrepository
+from mercurial.ui import ui
 
 
-def cgi():
+def app(environ, start_response):
     # Get the name of the configuration file.
     config_name = sys.argv[1]
     # Read the configuration.
     cp = ConfigParser()
     cp.read(config_name)
     # Determine the section to use and the file to render.
+    section, revision, file_path = environ['QUERY_STRING'].split('/', 2)
     # Get the configuration for that section.
+    hg_repos = cp.get(section, 'hg_repos')
+    hgweb_url = cp.get(section, 'hgweb_url')
+    cache = cp.get(section, 'cache')
     # Get the text of requested file/revision from repository.
+    repo = localrepository(None, hg_repos)
+    filelog = repo.file(file_path)
+    if filelog.count() == 0:
+        raise ValueError('%s Not found in repository.' % file_path)
+    source_text = filelog.read(filelog.tip())
     # Add a link to the source text.
-    # Convert to HTML.
+    source_text += dedent("""\
+        
+        .. admonition:: Link to source text
+
+            Automatically generated from `<%s/file/%s/%s>`__.
+        """ % (hgweb_url, revision, file_path))
+    hash = sha(source_text).hexdigest()
+    # Look in cache first.
+    cache_filename = join(cache, '%s.html' % hash)
+    if exists(cache_filename):
+        f = open(cache_filename, 'rb')
+        response_html = f.read()
+        f.close()
+    else:
+        # Not in cache, convert to HTML.
+        w = Writer()
+        response_html = publish_string(source_text, writer=w)
+        f = open(cache_filename, 'wb')
+        f.write(response_html)
+        f.close()
     # Serve as text/html.
+    status = '200 OK'
+    response_headers = [
+        ('Content-Type', 'text/html'),
+        ]
+    start_response(status, response_headers)
+    return [response_html]
+
+
+def cgi_script():
+    wsgicgi.launch(app)