1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
#! /depot/sundry/plat/bin/python1.4
"""Interactive FAQ project."""
import cgi, string, os
NAMEPAT = "faq??.???.htp"
class FAQServer:
def __init__(self):
pass
def main(self):
self.form = cgi.FieldStorage()
req = self.req or 'frontpage'
try:
method = getattr(self, 'do_%s' % req)
except AttributeError:
print "Unrecognized request type", req
else:
method()
KEYS = ['req', 'query', 'name', 'text', 'commit', 'title']
def __getattr__(self, key):
if key not in self.KEYS:
raise AttributeError
if self.form.has_key(key):
item = self.form[key]
return item.value
return ''
def do_frontpage(self):
print """
<TITLE>Python FAQ (alpha 1)</TITLE>
<H1>Python FAQ Front Page</H1>
<UL>
<LI><A HREF="faq.py?req=search">Search the FAQ</A>
<LI><A HREF="faq.py?req=browse">Browse the FAQ</A>
<LI><A HREF="faq.py?req=submit">Submit a new FAQ entry</A>
<LI><A HREF="faq.py?req=roulette">Random FAQ entry</A>
</UL>
<FORM ACTION="faq.py?req=query">
<INPUT TYPE=text NAME=query>
<INPUT TYPE=submit VALUE="Search">
<INPUT TYPE=hidden NAME=req VALUE=query>
</FORM>
Disclaimer: these pages are intended to be edited by anyone.
Please exercise discretion when editing, don't be rude, etc.
"""
def do_browse(self):
print """
<TITLE>Python FAQ</TITLE>
<H1>Python FAQ</H1>
<HR>
"""
names = os.listdir(os.curdir)
names.sort()
n = 0
for name in names:
headers, body = self.read(name)
if headers:
self.show(name, headers, body, 1)
n = n+1
if not n:
print "No FAQ entries?!?!"
def do_roulette(self):
import whrandom
print """
<TITLE>Python FAQ Roulette</TITLE>
<H1>Python FAQ Roulette</H1>
Please check the correctness of the entry below.
If you find any problems, please edit the entry.
<P>
<HR>
"""
names = os.listdir(os.curdir)
while names:
name = whrandom.choice(names)
headers, body = self.read(name)
if headers:
self.show(name, headers, body, 1)
print '<P><A HREF="faq.py?req=roulette">Show another one</A>'
break
else:
names.remove(name)
else:
print "No FAQ entries?!?!"
def do_search(self):
print """
<TITLE>Search the Python FAQ</TITLE>
<H1>Search the Python FAQ</H1>
<FORM ACTION="faq.py?req=query">
<INPUT TYPE=text NAME=query>
<INPUT TYPE=submit VALUE="Search">
<INPUT TYPE=hidden NAME=req VALUE=query>
</FORM>
"""
def do_query(self):
import regex
print "<TITLE>Python FAQ Query Results</TITLE>"
print "<H1>Python FAQ Query Results</H1>"
query = self.query
if not query:
print "No query string"
return
p = regex.compile(query, regex.casefold)
names = os.listdir(os.curdir)
names.sort()
print "<HR>"
n = 0
for name in names:
headers, body = self.read(name)
if headers:
title = headers['title']
if p.search(title) >= 0 or p.search(body) >= 0:
self.show(name, headers, body, 1)
n = n+1
if not n:
print "No hits."
def do_edit(self):
name = self.name
headers, body = self.read(name)
if not headers:
print "Invalid file name", name
return
print """
<TITLE>Python FAQ Edit Form</TITLE>
<H1>Python FAQ Edit Form</H1>
"""
self.showheaders(headers)
title = headers['title']
print """
<FORM METHOD=POST ACTION=faq.py>
<INPUT TYPE=text SIZE=80 NAME=title VALUE="%s"<BR>
<TEXTAREA COLS=80 ROWS=20 NAME=text>""" % title
print cgi.escape(string.strip(body))
print """</TEXTAREA>
<BR>
<INPUT TYPE=submit VALUE="Review Edit">
<INPUT TYPE=hidden NAME=req VALUE=review>
<INPUT TYPE=hidden NAME=name VALUE=%s>
</FORM>
<HR>
""" % name
self.show(name, headers, body)
def do_review(self):
name = self.name
text = self.text
commit = self.commit
title = self.title
if commit:
self.precheckin(name, text, title)
return
headers, body = self.read(name)
if not headers:
print "Invalid file name", name
return
print """
<TITLE>Python FAQ Review Form</TITLE>
<H1>Python FAQ Review Form</H1>
"""
self.show(name, {'title': title}, text)
print """
<FORM METHOD=POST ACTION=faq.py>
<INPUT TYPE=submit NAME=commit VALUE="Commit">
<P>
<HR>
"""
self.showheaders(headers)
print """
<INPUT TYPE=text SIZE=80 NAME=title VALUE="%s"<BR>
<TEXTAREA COLS=80 ROWS=20 NAME=text>""" % title
print cgi.escape(string.strip(text))
print """</TEXTAREA>
<BR>
<INPUT TYPE=submit VALUE="Review Edit">
<INPUT TYPE=hidden NAME=req VALUE=review>
<INPUT TYPE=hidden NAME=name VALUE=%s>
</FORM>
<HR>
""" % name
def precheckin(self, name, text, title):
pass
def showheaders(self, headers):
print "<UL>"
keys = map(string.lower, headers.keys())
keys.sort()
for key in keys:
print "<LI><B>%s:</B> %s" % (string.capwords(key, '-'),
headers[key] or '')
print "</UL>"
def read(self, name):
import fnmatch, rfc822
if not fnmatch.fnmatch(name, NAMEPAT):
return None, None
f = open(name)
headers = rfc822.Message(f)
body = f.read()
f.close()
return headers, body
def show(self, name, headers, body, edit=0):
# XXX Should put <A> tags around recognizable URLs
# XXX Should also turn "see section N" into hyperlinks
title = headers['title']
print "<H2>%s</H2>" % title
pre = 0
for line in string.split(body, '\n'):
if not string.strip(line):
if pre:
print '</PRE>'
pre = 0
else:
print '<P>'
else:
if line == string.lstrip(line):
if pre:
print '</PRE>'
pre = 0
else:
if not pre:
print '<PRE>'
pre = 1
print line
if pre:
print '</PRE>'
pre = 0
print '<P>'
if edit:
print '<A HREF="faq.py?req=edit&name=%s">Edit this entry</A>' %name
print '<P>'
print "<HR>"
print "Content-type: text/html\n"
try:
x = FAQServer()
x.main()
except:
print "<HR>Sorry, an error occurred"
cgi.print_exception()
|