Permalink
Browse files

add Python3.1.2 files

  • Loading branch information...
1 parent b154ab7 commit b7671f7077d9bb51fd046da75139872b1f4bcec7 @cocoatomo committed Jan 29, 2011
Showing 12,030 changed files with 1,912,107 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
View
@@ -0,0 +1,61 @@
+This directory contains various demonstrations of what you can do with
+Python. They were all written by me except where explicitly stated
+otherwise -- in general, demos contributed by others ends up in the
+../Contrib directory, unless I think they're of utmost general
+importance (like Matt Conway's Tk demos).
+
+A fair number of utilities that are useful when while developing
+Python code can be found in the ../Tools directory -- some of these
+can also be considered good examples of how to write Python code.
+
+Finally, in order to save disk space and net bandwidth, not all
+subdirectories listed here are distributed. They are listed just
+in case I change my mind about them.
+
+
+cgi CGI examples (see also ../Tools/faqwiz/.)
+
+classes Some examples of how to use classes.
+
+comparisons A set of responses to a really old language-comparison
+ challenge.
+
+curses A set of curses demos.
+
+embed An example of embedding Python in another application
+ (see also pysvr).
+
+imputil Demonstration subclasses of imputil.Importer.
+
+md5test Test program for the optional md5 module.
+
+metaclasses The code from the 1.5 metaclasses paper on the web.
+
+parser Example using the parser module.
+
+pdist Old, unfinished code messing with CVS, RCS and remote
+ files.
+
+pysvr An example of embedding Python in a threaded
+ application.
+
+rpc A set of classes for building clients and servers for
+ Sun RPC.
+
+scripts Some useful Python scripts that I put in my bin
+ directory. No optional built-in modules needed.
+
+sockets Examples for the new built-in module 'socket'.
+
+threads Demos that use the 'thread' module. (Currently these
+ only run on SGIs, but this may change in the future.)
+
+tix Demos using the Tix widget set addition to Tkinter.
+
+tkinter Demos using the Tk interface (including Matt Conway's
+ excellent set of demos).
+
+xml Some XML demos.
+
+zlib Some demos for the zlib module (see also the standard
+ library module gzip.py).
View
@@ -0,0 +1,11 @@
+CGI Examples
+------------
+
+Here are some example CGI programs. For a larger example, see
+../../Tools/faqwiz/.
+
+cgi0.sh -- A shell script to test your server is configured for CGI
+cgi1.py -- A Python script to test your server is configured for CGI
+cgi2.py -- A Python script showing how to parse a form
+cgi3.py -- A Python script for driving an arbitrary CGI application
+wiki.py -- Sample CGI application: a minimal Wiki implementation
View
@@ -0,0 +1,8 @@
+#! /bin/sh
+
+# If you can't get this to work, your web server isn't set up right
+
+echo Content-type: text/plain
+echo
+echo Hello world
+echo This is cgi0.sh
View
@@ -0,0 +1,14 @@
+#!/usr/local/bin/python
+
+"""CGI test 1 - check server setup."""
+
+# Until you get this to work, your web server isn't set up right or
+# your Python isn't set up right.
+
+# If cgi0.sh works but cgi1.py doesn't, check the #! line and the file
+# permissions. The docs for the cgi.py module have debugging tips.
+
+print("Content-type: text/html")
+print()
+print("<h1>Hello world</h1>")
+print("<p>This is cgi1.py")
View
@@ -0,0 +1,22 @@
+#!/usr/local/bin/python
+
+"""CGI test 2 - basic use of cgi module."""
+
+import cgitb; cgitb.enable()
+
+import cgi
+
+def main():
+ form = cgi.FieldStorage()
+ print("Content-type: text/html")
+ print()
+ if not form:
+ print("<h1>No Form Keys</h1>")
+ else:
+ print("<h1>Form Keys</h1>")
+ for key in form.keys():
+ value = form[key].value
+ print("<p>", cgi.escape(key), ":", cgi.escape(value))
+
+if __name__ == "__main__":
+ main()
View
@@ -0,0 +1,10 @@
+#!/usr/local/bin/python
+
+"""CGI test 3 (persistent data)."""
+
+import cgitb; cgitb.enable()
+
+from wiki import main
+
+if __name__ == "__main__":
+ main()
View
@@ -0,0 +1,123 @@
+"""Wiki main program. Imported and run by cgi3.py."""
+
+import os, re, cgi, sys, tempfile
+escape = cgi.escape
+
+def main():
+ form = cgi.FieldStorage()
+ print("Content-type: text/html")
+ print()
+ cmd = form.getvalue("cmd", "view")
+ page = form.getvalue("page", "FrontPage")
+ wiki = WikiPage(page)
+ method = getattr(wiki, 'cmd_' + cmd, None) or wiki.cmd_view
+ method(form)
+
+class WikiPage:
+
+ homedir = tempfile.gettempdir()
+ scripturl = os.path.basename(sys.argv[0])
+
+ def __init__(self, name):
+ if not self.iswikiword(name):
+ raise ValueError("page name is not a wiki word")
+ self.name = name
+ self.load()
+
+ def cmd_view(self, form):
+ print("<h1>", escape(self.splitwikiword(self.name)), "</h1>")
+ print("<p>")
+ for line in self.data.splitlines():
+ line = line.rstrip()
+ if not line:
+ print("<p>")
+ else:
+ print(self.formatline(line))
+ print("<hr>")
+ print("<p>", self.mklink("edit", self.name, "Edit this page") + ";")
+ print(self.mklink("view", "FrontPage", "go to front page") + ".")
+
+ def formatline(self, line):
+ words = []
+ for word in re.split('(\W+)', line):
+ if self.iswikiword(word):
+ if os.path.isfile(self.mkfile(word)):
+ word = self.mklink("view", word, word)
+ else:
+ word = self.mklink("new", word, word + "*")
+ else:
+ word = escape(word)
+ words.append(word)
+ return "".join(words)
+
+ def cmd_edit(self, form, label="Change"):
+ print("<h1>", label, self.name, "</h1>")
+ print('<form method="POST" action="%s">' % self.scripturl)
+ s = '<textarea cols="70" rows="20" name="text">%s</textarea>'
+ print(s % self.data)
+ print('<input type="hidden" name="cmd" value="create">')
+ print('<input type="hidden" name="page" value="%s">' % self.name)
+ print('<br>')
+ print('<input type="submit" value="%s Page">' % label)
+ print("</form>")
+
+ def cmd_create(self, form):
+ self.data = form.getvalue("text", "").strip()
+ error = self.store()
+ if error:
+ print("<h1>I'm sorry. That didn't work</h1>")
+ print("<p>An error occurred while attempting to write the file:")
+ print("<p>", escape(error))
+ else:
+ # Use a redirect directive, to avoid "reload page" problems
+ print("<head>")
+ s = '<meta http-equiv="refresh" content="1; URL=%s">'
+ print(s % (self.scripturl + "?cmd=view&page=" + self.name))
+ print("<head>")
+ print("<h1>OK</h1>")
+ print("<p>If nothing happens, please click here:", end=' ')
+ print(self.mklink("view", self.name, self.name))
+
+ def cmd_new(self, form):
+ self.cmd_edit(form, label="Create")
+
+ def iswikiword(self, word):
+ return re.match("[A-Z][a-z]+([A-Z][a-z]*)+", word)
+
+ def splitwikiword(self, word):
+ chars = []
+ for c in word:
+ if chars and c.isupper():
+ chars.append(' ')
+ chars.append(c)
+ return "".join(chars)
+
+ def mkfile(self, name=None):
+ if name is None:
+ name = self.name
+ return os.path.join(self.homedir, name + ".txt")
+
+ def mklink(self, cmd, page, text):
+ link = self.scripturl + "?cmd=" + cmd + "&page=" + page
+ return '<a href="%s">%s</a>' % (link, text)
+
+ def load(self):
+ try:
+ f = open(self.mkfile())
+ data = f.read().strip()
+ f.close()
+ except IOError:
+ data = ""
+ self.data = data
+
+ def store(self):
+ data = self.data
+ try:
+ f = open(self.mkfile(), "w")
+ f.write(data)
+ if data and not data.endswith('\n'):
+ f.write('\n')
+ f.close()
+ return ""
+ except IOError as err:
+ return "IOError: %s" % str(err)
Oops, something went wrong.

0 comments on commit b7671f7

Please sign in to comment.