LDP/LDP/scrollserver/scrollserver.py

211 lines
6.9 KiB
Python
Raw Normal View History

#!/usr/bin/python2
2001-09-22 02:54:49 +00:00
import os # Import required modules
import string
import commands
import StringIO
2001-09-22 17:52:38 +00:00
import locale
import SimpleHTTPServer
from urlparse import URI # Import ScrollServer modules
import scrollkeeper
# Defaults
dbxslfile = "stylesheets/db144/html/docbook.xsl"
#dbxslfile = "stylesheets/docbook/docbook.xsl"
2001-09-22 17:52:38 +00:00
htmlbase = "/var/cache/scrollserver/" # This is the cache directory
caching = 1 # set to 1 to enable caching
2001-09-20 03:19:31 +00:00
xsltparam = "--timing" # parameters to pass in all xsltproc calls
2001-09-22 02:54:49 +00:00
xsltparam = ""
lang = locale.setlocale(locale.LC_ALL) # hard code an ISO language code here to test it
#lang = "de" # but it must match in scrollkeeper.py
2001-09-22 02:54:49 +00:00
BaseClass = SimpleHTTPServer.SimpleHTTPRequestHandler
ScrollKeeper = scrollkeeper.ScrollKeeper()
def FileContents(filename): # Return the contents of any file
f = open(filename, "r")
text = f.read()
f.close
return text
2001-09-22 02:54:49 +00:00
# Kind of like make. Aging not implemented yet.
2001-09-22 02:54:49 +00:00
def FileCache(sourcefile, htmlfile, cmd):
if not os.path.exists(htmlfile) or caching == 0:
os.system(cmd)
class RequestHandler(BaseClass): # Intercepts the HTTP requests and serves them
def do_GET(self):
BaseClass.do_GET(self)
def send_head(self):
# Send the requested page
if self.path == "" or self.path == "/" or self.path == "/index.html":
2001-09-20 03:19:31 +00:00
return self.send_Home()
elif self.path == "/controls.html":
return self.send_Controls()
elif self.path == "/reset.html":
return self.send_Reset()
2001-09-20 03:19:31 +00:00
elif self.path == "/contents.html":
return self.send_ContentsList()
2001-09-20 03:19:31 +00:00
elif self.path =="/documents.html":
return self.send_DocList()
elif self.path == "/help.html":
return self.send_Help()
else: # If not an internal page, it is a document or an
# image file being requested.
2001-09-22 02:54:49 +00:00
uri = URI(self.path)
# Serve a document
2001-09-22 02:54:49 +00:00
if uri.Filename == "docid":
return self.send_DocumentByID(uri.Parameter)
else:
# Try to serve a file
2001-09-22 02:54:49 +00:00
return self.send_URI(uri)
2001-09-20 03:19:31 +00:00
def send_Home(self): # Send home page
FileCache ("", htmlbase+ "index.html", "xsltproc " + xsltparam + " stylesheets/index.xsl stylesheets/index.xsl > " + htmlbase + "index.html")
return self.send_File(htmlbase + "index.html")
2001-09-20 03:19:31 +00:00
def send_Help(self): # Send help page
FileCache ("", htmlbase+ "help.html", "xsltproc " + xsltparam + " stylesheets/help.xsl stylesheets/help.xsl > " + htmlbase + "help.html")
return self.send_File(htmlbase + "help.html")
def send_Controls(self): # Send controls page
FileCache ("", htmlbase + "controls.html", "xsltproc " + xsltparam + " stylesheets/controls.xsl stylesheets/controls.xsl > " + htmlbase + "controls.html")
return self.send_File(htmlbase + "controls.html")
def send_Reset(self): # Reset cache and send reset page
os.system("rm -rf " + htmlbase + "*")
FileCache ("", htmlbase + "reset.html", "xsltproc " + xsltparam + " stylesheets/reset.xsl stylesheets/reset.xsl > " + htmlbase + "reset.html")
return self.send_File(htmlbase + "reset.html")
def send_ContentsList(self): # Send table of contents
2001-09-22 17:52:38 +00:00
contents_list = commands.getoutput("scrollkeeper-get-content-list " + lang)
FileCache (contents_list, htmlbase + "contents.html", "xsltproc " + xsltparam + " stylesheets/contents.xsl " + contents_list + " > " + htmlbase + "contents.html")
return self.send_File(htmlbase + "contents.html")
2001-09-20 03:19:31 +00:00
def send_DocList(self): # Send alphabetical document list
2001-09-22 17:52:38 +00:00
contents_list = commands.getoutput("scrollkeeper-get-content-list " + lang)
FileCache (contents_list, htmlbase + "documents.html", "xsltproc " + xsltparam + " stylesheets/documents.xsl " + contents_list + " > " + htmlbase + "documents.html")
return self.send_File(htmlbase + "documents.html")
# Send a document
def send_DocumentByID(self, docid):
document = ScrollKeeper.DocumentByID(docid)
if not document:
text = "Error: ScrollServer couldn't find document number " + docid
return self.send_Text(text)
2001-09-20 03:19:31 +00:00
# Determine files and paths to read and write
2001-09-20 03:19:31 +00:00
xmlfile = document.SourceFile
xmlpath = os.path.dirname(xmlfile)
htmlpath = htmlbase + docid
htmlfile = htmlpath + "/index.html"
# Uncomment to debug file and path problems
# print "xmlpath:" + xmlpath
# print "htmlpath:" + htmlpath
# print "htmlfile:" + htmlfile
# print "xmlfile:" + xmlfile
# print "format:" + document.Format
2001-09-22 02:54:49 +00:00
# The directory must exist
2001-09-22 02:54:49 +00:00
FileCache ("", htmlpath, "mkdir " + htmlpath)
# Process DocBook SGML files (other formats fail)
if document.Format == "text/sgml":
FileCache (xmlfile, htmlfile, "xsltproc --docbook " + xsltparam + " " + dbxslfile + " " + xmlfile + " > " + htmlfile)
# Symbolic links to the files in source directory
# Required for efficient subsequent image requests
2001-09-22 02:54:49 +00:00
os.system("ln -s --target-directory=" + htmlpath + " " + xmlpath + "/*")
return self.send_File(htmlfile)
def send_URI(self, uri): # Send some external file or image request
filename = uri.Path + "/" + uri.Filename
if os.path.isfile(filename):
return self.send_File(filename)
# Adjust relative links using referer
2001-09-22 02:54:49 +00:00
referer = self.headers.getheader("Referer")
refuri = URI(referer)
if refuri.Filename == "docid":
document = ScrollKeeper.DocumentByID(refuri.Parameter)
filename = htmlbase + document.ID + "/" + uri.Path + "/" + uri.Filename
return self.send_File(filename)
else:
text = "Unrecognized request: " + uri.Filename
return self.send_Text(text)
def send_File(self, filename): # Send the contents of a file
# Extract extension, guess if missing
# Due to missing file extensions in some current
# ScrollKeeper data.
2001-09-22 02:54:49 +00:00
temp = string.split(filename, ".")
if len(temp) > 1:
fileext = temp[1]
else:
if os.path.isfile(filename + ".png"):
fileext = "png"
elif os.path.isfile(filename + ".jpeg"):
fileext = "jpeg"
if os.path.isfile(filename + ".jpg"):
fileext = "jpg"
if os.path.isfile(filename + ".gif"):
fileext = "gif"
else:
fileext = ""
2001-09-22 02:54:49 +00:00
filename += "." + fileext
# Determine mimetype from extension
2001-09-20 03:19:31 +00:00
if fileext == "html" or fileext == "htm":
mimetype = "text/html"
elif fileext == "png":
mimetype = "image/png"
elif fileext == "gif":
mimetype = "image/gif"
elif fileext == "jpg" or fileext == "jpeg":
mimetype = "image/jpeg"
elif fileext == "css":
mimetype = "text/css"
2001-09-22 02:54:49 +00:00
else:
mimetype = "text/plain"
2001-09-22 02:54:49 +00:00
# Send file if found, or error message
2001-09-22 02:54:49 +00:00
if os.path.isfile(filename):
self.send_response(200)
self.send_header("Content-type", mimetype)
self.end_headers()
text = FileContents(filename)
else:
text = "Unrecognized file: " + filename
return StringIO.StringIO(text)
# Send a text message
def send_Text(self, text):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
return StringIO.StringIO(text)
2001-09-22 02:54:49 +00:00
def ScrollServer(): # Initialize the server
os.system("rm -rf " + htmlbase + "*")
print "ScrollServer v0.6 -- development version!"
2001-09-22 02:54:49 +00:00
SimpleHTTPServer.test(RequestHandler)
ScrollServer()