SVN: r20985 - in trunk/quixote: . doc
Andrew Kuchling <akuchlin-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]> Thu, 06 Mar 2003 10:30:28 -0500
| Newsgroups | gmane.comp.web.quixote.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: akuchlin
Date: 2003-03-06 10:30:27 -0500 (Thu, 06 Mar 2003)
New Revision: 20985
Modified:
trunk/quixote/CHANGES
trunk/quixote/doc/static-files.txt
trunk/quixote/util.py
Log:
Remove CGIScript class
Modified: trunk/quixote/util.py
==============================================================================
--- trunk/quixote/util.py (original)
+++ trunk/quixote/util.py 2003-03-06 10:30:27.000000000 -0500
@@ -10,16 +10,15 @@
Quixote resource.
StaticFilesFolder : Wraps a filesystem folder containing static
files as a Quixote namespace.
- CGIScript : Wraps a Python CGI script as a Quixote resource.
-StaticFile, StaticFilesFolder, and CGIScript were contributed by
-Hamish Lawson. See doc/static-files.txt for examples of their use.
+StaticFile and StaticFilesFolder were contributed by Hamish Lawson.
+See doc/static-files.txt for examples of their use.
"""
__revision__ = "$Id$"
import sys, xmlrpclib
-import os, mimetypes, cgi, urllib
+import os, mimetypes, urllib
from quixote import errors, html
from cStringIO import StringIO
@@ -198,110 +197,3 @@
return item(request)
-class SimulatedCGIStandardInput:
-
- """
- Provides a simulated stdin to CGI scripts. The data is obtained from
- the request.form object already created by Quixote.
- """
-
- def __init__(self, request):
- self.request = request
-
- def read(self, length):
- if self.request.environ['REQUEST_METHOD'] == 'POST':
- return urllib.urlencode(self.request.form, doseq=1)
- else:
- return None
-
-
-class CGIScript:
-
- """
- Wraps a Python CGI script as a Quixote resource.
-
- An instance is initialized with the absolute path to the script and
- optionally flags indicating whether the compiled code should be cached and
- whether a symbolic link should be followed.
- """
-
- def __init__(self, path, use_cache=0, follow_symlinks=0):
- """CGIScript(path:string, use_cache:bool, follow_symlinks:bool)
-
- Initialize instance with the absolute path to a CGI script.
- If 'use_cache' is true, the script's content will be cached in memory.
- If 'follow_symlinks' is true, symbolic links will be followed.
- """
- if not os.path.isabs(path):
- raise ValueError, "Path %r is not absolute" % path
- self.path = path
- self.folder, self.filename = os.path.split(path)
- self.use_cache = use_cache
- self.follow_symlinks = follow_symlinks
- self.cache = None
-
- def __call__(self, request):
- import email
-
- # If the compiled script is cached, get it from there. Otherwise
- # read the script file and compile it; if caching is being used,
- # cache the compiled code.
- if self.cache:
- code = self.cache
- else:
- try:
- assert os.path.isfile(self.path)
- assert not os.path.islink(self.path) or self.follow_symlinks
- scriptfile = open(self.path)
- except (AssertionError, IOError), exc:
- raise errors.TraversalError
- code = compile(scriptfile.read(), self.path, 'exec')
- scriptfile.close()
- if self.use_cache:
- self.cache = code
-
- # Set up the context a conventional CGI script may expect.
- #
- # 1. If the request is a POST, Quixote will already have consumed
- # stdin, so we provide the CGI script with a simulated stdin that uses
- # the form object created by Quixote.
- #
- # 2. We capture the script's stdout in order to return it to Quixote.
- #
- # 3. We update os.environ to cater for the fact that a CGI script will
- # look for HTTP/CGI environment variables there, but Quixote stores
- # them in request.environ.
- #
- # 4. We provide for two assumptions that a Python CGI script might
- # make about directories. First, in a conventional CGI context the web
- # server would set the current directory to the CGI script's location.
- # Second, this directory would be at the start of Python's module
- # search path, due to the fact that a new Python interpreter would
- # be started up to run the script.
- original_stdin = sys.stdin
- original_stdout = sys.stdout
- sys.stdin = SimulatedCGIStandardInput(request)
- sys.stdout = StringIO()
- os.environ.update(request.environ)
- original_cwd = os.getcwd()
- os.chdir(self.folder)
- original_sys_path = sys.path
- sys.path.insert(0, self.folder)
-
- try:
- # Execute the compiled CGI script and collect its output as a MIME
- # message (but parsing only the headers).
- exec code
- parser = email.Parser.HeaderParser()
- mime_message = parser.parsestr(sys.stdout.getvalue())
- finally:
- # Restore the context that was in effect before running the script.
- sys.stdout = original_stdout
- sys.stdin = original_stdin
- sys.path = original_sys_path
- os.chdir(original_cwd)
-
- # Copy the generated headers to Quixote's response and return the body.
- for header, value in mime_message.items():
- request.response.set_header(header, value)
- return str(mime_message.get_payload())
Modified: trunk/quixote/doc/static-files.txt
==============================================================================
--- trunk/quixote/doc/static-files.txt (original)
+++ trunk/quixote/doc/static-files.txt 2003-03-06 10:30:27.000000000 -0500
@@ -1,9 +1,8 @@
Examples of serving static files
================================
-The ``quixote.util`` module includes classes for making files,
-directories, and even CGI scripts available as Quixote resources.
-Here are some examples.
+The ``quixote.util`` module includes classes for making files and
+directories available as Quixote resources. Here are some examples.
A single file
@@ -35,15 +34,3 @@
::
notes = StaticFilesFolder("/htdocs/legacy_app/notes")
-A CGI script
-------------
-
-The ``use_cache=1`` requests that the compiled script be cached.
-
-::
- this_module = sys.modules[__name__]
- setattr(
- this_module,
- "results.cgi",
- CGIScript("/htdocs/legacy_app/results.cgi", use_cache=1)
- )
Modified: trunk/quixote/CHANGES
==============================================================================
--- trunk/quixote/CHANGES (original)
+++ trunk/quixote/CHANGES 2003-03-06 10:30:28.000000000 -0500
@@ -8,9 +8,9 @@
Note that this means HTML templates will not work with Python 2.0
unless you compile the C extension.
- * Added StaticFile, StaticFilesFolder, and CGIScript classes
- to quixote.util. Consult doc/static_files.txt for examples.
- (Contributed and documented by Hamish Lawson.)
+ * Added StaticFile and StaticFilesFolder classes to quixote.util.
+ Consult doc/static-files.txt for examples. (Contributed and
+ documented by Hamish Lawson.)
0.6b2 (27 Jan 2003):