Skip to content

Commit 922d3f5

Browse files
author
Nickolai Zeldovich
committed
python 2.6 base
0 parents  commit 922d3f5

File tree

3,915 files changed

+1463049
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

3,915 files changed

+1463049
-0
lines changed

Demo/README

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
This directory contains various demonstrations of what you can do with
2+
Python. They were all written by me except where explicitly stated
3+
otherwise -- in general, demos contributed by others ends up in the
4+
../Contrib directory, unless I think they're of utmost general
5+
importance (like Matt Conway's Tk demos).
6+
7+
A fair number of utilities that are useful when while developing
8+
Python code can be found in the ../Tools directory -- some of these
9+
can also be considered good examples of how to write Python code.
10+
11+
Finally, in order to save disk space and net bandwidth, not all
12+
subdirectories listed here are distributed. They are listed just
13+
in case I change my mind about them.
14+
15+
16+
cgi CGI examples (see also ../Tools/faqwiz/.)
17+
18+
classes Some examples of how to use classes.
19+
20+
comparisons A set of responses to a really old language-comparison
21+
challenge.
22+
23+
curses A set of curses demos.
24+
25+
embed An example of embedding Python in another application
26+
(see also pysvr).
27+
28+
imputil Demonstration subclasses of imputil.Importer.
29+
30+
md5test Test program for the optional md5 module.
31+
32+
metaclasses The code from the 1.5 metaclasses paper on the web.
33+
34+
parser Example using the parser module.
35+
36+
pdist Old, unfinished code messing with CVS, RCS and remote
37+
files.
38+
39+
pysvr An example of embedding Python in a threaded
40+
application.
41+
42+
rpc A set of classes for building clients and servers for
43+
Sun RPC.
44+
45+
scripts Some useful Python scripts that I put in my bin
46+
directory. No optional built-in modules needed.
47+
48+
sockets Examples for the new built-in module 'socket'.
49+
50+
threads Demos that use the 'thread' module. (Currently these
51+
only run on SGIs, but this may change in the future.)
52+
53+
tix Demos using the Tix widget set addition to Tkinter.
54+
55+
tkinter Demos using the Tk interface (including Matt Conway's
56+
excellent set of demos).
57+
58+
xml Some XML demos.
59+
60+
zlib Some demos for the zlib module (see also the standard
61+
library module gzip.py).

Demo/cgi/README

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CGI Examples
2+
------------
3+
4+
Here are some example CGI programs. For a larger example, see
5+
../../Tools/faqwiz/.
6+
7+
cgi0.sh -- A shell script to test your server is configured for CGI
8+
cgi1.py -- A Python script to test your server is configured for CGI
9+
cgi2.py -- A Python script showing how to parse a form
10+
cgi3.py -- A Python script for driving an arbitrary CGI application
11+
wiki.py -- Sample CGI application: a minimal Wiki implementation

Demo/cgi/cgi0.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#! /bin/sh
2+
3+
# If you can't get this to work, your web server isn't set up right
4+
5+
echo Content-type: text/plain
6+
echo
7+
echo Hello world
8+
echo This is cgi0.sh

Demo/cgi/cgi1.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/local/bin/python
2+
3+
"""CGI test 1 - check server setup."""
4+
5+
# Until you get this to work, your web server isn't set up right or
6+
# your Python isn't set up right.
7+
8+
# If cgi0.sh works but cgi1.py doesn't, check the #! line and the file
9+
# permissions. The docs for the cgi.py module have debugging tips.
10+
11+
print "Content-type: text/html"
12+
print
13+
print "<h1>Hello world</h1>"
14+
print "<p>This is cgi1.py"

Demo/cgi/cgi2.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/local/bin/python
2+
3+
"""CGI test 2 - basic use of cgi module."""
4+
5+
import cgitb; cgitb.enable()
6+
7+
import cgi
8+
9+
def main():
10+
form = cgi.FieldStorage()
11+
print "Content-type: text/html"
12+
print
13+
if not form:
14+
print "<h1>No Form Keys</h1>"
15+
else:
16+
print "<h1>Form Keys</h1>"
17+
for key in form.keys():
18+
value = form[key].value
19+
print "<p>", cgi.escape(key), ":", cgi.escape(value)
20+
21+
if __name__ == "__main__":
22+
main()

Demo/cgi/cgi3.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/local/bin/python
2+
3+
"""CGI test 3 (persistent data)."""
4+
5+
import cgitb; cgitb.enable()
6+
7+
from wiki import main
8+
9+
if __name__ == "__main__":
10+
main()

Demo/cgi/wiki.py

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
"""Wiki main program. Imported and run by cgi3.py."""
2+
3+
import os, re, cgi, sys, tempfile
4+
escape = cgi.escape
5+
6+
def main():
7+
form = cgi.FieldStorage()
8+
print "Content-type: text/html"
9+
print
10+
cmd = form.getvalue("cmd", "view")
11+
page = form.getvalue("page", "FrontPage")
12+
wiki = WikiPage(page)
13+
method = getattr(wiki, 'cmd_' + cmd, None) or wiki.cmd_view
14+
method(form)
15+
16+
class WikiPage:
17+
18+
homedir = tempfile.gettempdir()
19+
scripturl = os.path.basename(sys.argv[0])
20+
21+
def __init__(self, name):
22+
if not self.iswikiword(name):
23+
raise ValueError, "page name is not a wiki word"
24+
self.name = name
25+
self.load()
26+
27+
def cmd_view(self, form):
28+
print "<h1>", escape(self.splitwikiword(self.name)), "</h1>"
29+
print "<p>"
30+
for line in self.data.splitlines():
31+
line = line.rstrip()
32+
if not line:
33+
print "<p>"
34+
else:
35+
print self.formatline(line)
36+
print "<hr>"
37+
print "<p>", self.mklink("edit", self.name, "Edit this page") + ";"
38+
print self.mklink("view", "FrontPage", "go to front page") + "."
39+
40+
def formatline(self, line):
41+
words = []
42+
for word in re.split('(\W+)', line):
43+
if self.iswikiword(word):
44+
if os.path.isfile(self.mkfile(word)):
45+
word = self.mklink("view", word, word)
46+
else:
47+
word = self.mklink("new", word, word + "*")
48+
else:
49+
word = escape(word)
50+
words.append(word)
51+
return "".join(words)
52+
53+
def cmd_edit(self, form, label="Change"):
54+
print "<h1>", label, self.name, "</h1>"
55+
print '<form method="POST" action="%s">' % self.scripturl
56+
s = '<textarea cols="70" rows="20" name="text">%s</textarea>'
57+
print s % self.data
58+
print '<input type="hidden" name="cmd" value="create">'
59+
print '<input type="hidden" name="page" value="%s">' % self.name
60+
print '<br>'
61+
print '<input type="submit" value="%s Page">' % label
62+
print "</form>"
63+
64+
def cmd_create(self, form):
65+
self.data = form.getvalue("text", "").strip()
66+
error = self.store()
67+
if error:
68+
print "<h1>I'm sorry. That didn't work</h1>"
69+
print "<p>An error occurred while attempting to write the file:"
70+
print "<p>", escape(error)
71+
else:
72+
# Use a redirect directive, to avoid "reload page" problems
73+
print "<head>"
74+
s = '<meta http-equiv="refresh" content="1; URL=%s">'
75+
print s % (self.scripturl + "?cmd=view&page=" + self.name)
76+
print "<head>"
77+
print "<h1>OK</h1>"
78+
print "<p>If nothing happens, please click here:",
79+
print self.mklink("view", self.name, self.name)
80+
81+
def cmd_new(self, form):
82+
self.cmd_edit(form, label="Create")
83+
84+
def iswikiword(self, word):
85+
return re.match("[A-Z][a-z]+([A-Z][a-z]*)+", word)
86+
87+
def splitwikiword(self, word):
88+
chars = []
89+
for c in word:
90+
if chars and c.isupper():
91+
chars.append(' ')
92+
chars.append(c)
93+
return "".join(chars)
94+
95+
def mkfile(self, name=None):
96+
if name is None:
97+
name = self.name
98+
return os.path.join(self.homedir, name + ".txt")
99+
100+
def mklink(self, cmd, page, text):
101+
link = self.scripturl + "?cmd=" + cmd + "&page=" + page
102+
return '<a href="%s">%s</a>' % (link, text)
103+
104+
def load(self):
105+
try:
106+
f = open(self.mkfile())
107+
data = f.read().strip()
108+
f.close()
109+
except IOError:
110+
data = ""
111+
self.data = data
112+
113+
def store(self):
114+
data = self.data
115+
try:
116+
f = open(self.mkfile(), "w")
117+
f.write(data)
118+
if data and not data.endswith('\n'):
119+
f.write('\n')
120+
f.close()
121+
return ""
122+
except IOError, err:
123+
return "IOError: %s" % str(err)

0 commit comments

Comments
 (0)