1997-05-21 18:31:39 -03:00
|
|
|
"""Interactive FAQ project.
|
|
|
|
|
1997-05-22 12:21:57 -03:00
|
|
|
Note that this is not an executable script; it's an importable module.
|
|
|
|
The actual CGI script can be kept minimal; it's appended at the end of
|
|
|
|
this file as a string constant.
|
|
|
|
|
1997-05-21 18:31:39 -03:00
|
|
|
XXX TO DO
|
|
|
|
|
1997-05-22 13:57:50 -03:00
|
|
|
- next/prev/index links in do_show?
|
1997-05-21 19:25:56 -03:00
|
|
|
- customize rcs command pathnames
|
1997-05-21 18:31:39 -03:00
|
|
|
- explanation of editing somewhere
|
|
|
|
- various embellishments, GIFs, crosslinks, hints, etc.
|
|
|
|
- create new sections
|
|
|
|
- rearrange entries
|
|
|
|
- delete entries
|
|
|
|
- send email on changes
|
|
|
|
- optional staging of entries until reviewed?
|
|
|
|
- freeze entries
|
|
|
|
- username/password for editors
|
|
|
|
- Change references to other Q's and whole sections
|
|
|
|
- support adding annotations, too
|
1997-05-22 12:21:57 -03:00
|
|
|
- make it more generic (so you can create your own FAQ)
|
1997-05-22 21:50:01 -03:00
|
|
|
- more OO structure, e.g. add a class representing one FAQ entry
|
1997-05-21 18:31:39 -03:00
|
|
|
|
|
|
|
"""
|
1997-05-21 04:24:50 -03:00
|
|
|
|
|
|
|
NAMEPAT = "faq??.???.htp"
|
1997-05-21 18:31:39 -03:00
|
|
|
NAMEREG = "^faq\([0-9][0-9]\)\.\([0-9][0-9][0-9]\)\.htp$"
|
1997-05-21 04:24:50 -03:00
|
|
|
|
1997-05-23 14:45:04 -03:00
|
|
|
# Like so many other things, this should come from a file.
|
1997-05-23 12:55:19 -03:00
|
|
|
SECTIONS = {
|
|
|
|
"1": "General information and availability",
|
|
|
|
"2": "Python in the real world",
|
|
|
|
"3": "Building Python and Other Known Bugs",
|
|
|
|
"4": "Programming in Python",
|
|
|
|
"5": "Extending Python",
|
|
|
|
"6": "Python's design",
|
|
|
|
"7": "Using Python on non-UNIX platforms",
|
|
|
|
}
|
|
|
|
|
1997-05-21 04:24:50 -03:00
|
|
|
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()
|
1997-05-21 20:43:39 -03:00
|
|
|
self.epilogue()
|
1997-05-21 04:24:50 -03:00
|
|
|
|
1997-05-21 13:52:18 -03:00
|
|
|
KEYS = ['req', 'query', 'name', 'text', 'commit', 'title',
|
1997-05-21 20:43:39 -03:00
|
|
|
'author', 'email', 'log', 'section', 'number', 'add',
|
1997-05-23 12:55:19 -03:00
|
|
|
'version', 'edit']
|
1997-05-21 04:24:50 -03:00
|
|
|
|
|
|
|
def __getattr__(self, key):
|
|
|
|
if key not in self.KEYS:
|
|
|
|
raise AttributeError
|
1997-05-21 13:52:18 -03:00
|
|
|
try:
|
1997-05-22 12:21:57 -03:00
|
|
|
form = self.form
|
|
|
|
try:
|
|
|
|
item = form[key]
|
|
|
|
except TypeError, msg:
|
|
|
|
raise KeyError, msg, sys.exc_traceback
|
1997-05-21 13:52:18 -03:00
|
|
|
except KeyError:
|
|
|
|
return ''
|
1997-05-22 13:57:50 -03:00
|
|
|
value = self.form[key].value
|
|
|
|
value = string.strip(value)
|
|
|
|
setattr(self, key, value)
|
|
|
|
return value
|
1997-05-21 04:24:50 -03:00
|
|
|
|
|
|
|
def do_frontpage(self):
|
1997-05-21 20:43:39 -03:00
|
|
|
self.prologue("Python FAQ (alpha) Front Page")
|
1997-05-21 04:24:50 -03:00
|
|
|
print """
|
|
|
|
<UL>
|
1997-05-21 18:31:39 -03:00
|
|
|
<LI><A HREF="faq.py?req=index">FAQ index</A>
|
|
|
|
<LI><A HREF="faq.py?req=all">The whole FAQ</A>
|
1997-05-21 13:52:18 -03:00
|
|
|
<LI><A HREF="faq.py?req=roulette">FAQ roulette</A>
|
1997-05-21 18:31:39 -03:00
|
|
|
<LI><A HREF="faq.py?req=recent">Recently changed FAQ entries</A>
|
|
|
|
<LI><A HREF="faq.py?req=add">Add a new FAQ entry</A>
|
1997-05-22 13:57:50 -03:00
|
|
|
<LI><A HREF="faq.py?req=delete">Delete a FAQ entry</A>
|
1997-05-21 04:24:50 -03:00
|
|
|
</UL>
|
|
|
|
|
1997-05-23 14:45:04 -03:00
|
|
|
<HR>
|
|
|
|
|
1997-05-21 18:31:39 -03:00
|
|
|
<H2>Search the FAQ</H2>
|
|
|
|
|
1997-05-23 15:13:58 -03:00
|
|
|
<FORM ACTION="faq.py">
|
1997-05-21 04:24:50 -03:00
|
|
|
<INPUT TYPE=text NAME=query>
|
1997-05-23 14:45:04 -03:00
|
|
|
<INPUT TYPE=submit VALUE="Search"><BR>
|
|
|
|
(Case insensitive regular expressions.)
|
1997-05-21 04:24:50 -03:00
|
|
|
<INPUT TYPE=hidden NAME=req VALUE=query>
|
|
|
|
</FORM>
|
1997-05-23 14:45:04 -03:00
|
|
|
<HR>
|
|
|
|
<P>
|
1997-05-21 04:24:50 -03:00
|
|
|
Disclaimer: these pages are intended to be edited by anyone.
|
|
|
|
Please exercise discretion when editing, don't be rude, etc.
|
|
|
|
"""
|
|
|
|
|
1997-05-21 18:31:39 -03:00
|
|
|
def do_index(self):
|
1997-05-21 20:43:39 -03:00
|
|
|
self.prologue("Python FAQ Index")
|
1997-05-21 18:31:39 -03:00
|
|
|
names = os.listdir(os.curdir)
|
|
|
|
names.sort()
|
|
|
|
section = None
|
|
|
|
for name in names:
|
|
|
|
headers, text = self.read(name)
|
|
|
|
if headers:
|
|
|
|
title = headers['title']
|
|
|
|
i = string.find(title, '.')
|
|
|
|
nsec = title[:i]
|
|
|
|
if nsec != section:
|
|
|
|
if section:
|
|
|
|
print """
|
|
|
|
<P>
|
|
|
|
<LI><A HREF="faq.py?req=add&section=%s"
|
|
|
|
>Add new entry</A> (at this point)
|
|
|
|
</UL>
|
|
|
|
""" % section
|
|
|
|
section = nsec
|
1997-05-23 12:55:19 -03:00
|
|
|
if SECTIONS.has_key(section):
|
|
|
|
stitle = SECTIONS[section]
|
|
|
|
else:
|
|
|
|
stitle = ""
|
|
|
|
print "<H2>Section %s. %s</H2>" % (section, stitle)
|
1997-05-21 18:31:39 -03:00
|
|
|
print "<UL>"
|
|
|
|
print '<LI><A HREF="faq.py?req=show&name=%s">%s</A>' % (
|
|
|
|
name, cgi.escape(title))
|
|
|
|
if section:
|
|
|
|
print """
|
|
|
|
<P>
|
|
|
|
<LI><A HREF="faq.py?req=add&section=%s">Add new entry</A>
|
|
|
|
(at this point)
|
|
|
|
</UL>
|
|
|
|
""" % section
|
|
|
|
else:
|
|
|
|
print "No FAQ entries?!?!"
|
|
|
|
|
|
|
|
def do_show(self):
|
1997-05-22 13:57:50 -03:00
|
|
|
self.prologue("Python FAQ Entry")
|
|
|
|
print "<HR>"
|
1997-05-21 18:31:39 -03:00
|
|
|
name = self.name
|
|
|
|
headers, text = self.read(name)
|
|
|
|
if not headers:
|
1997-05-22 13:57:50 -03:00
|
|
|
self.error("Invalid file name", name)
|
1997-05-21 18:31:39 -03:00
|
|
|
return
|
1997-05-22 12:21:57 -03:00
|
|
|
self.show(name, headers['title'], text)
|
1997-05-21 18:31:39 -03:00
|
|
|
|
|
|
|
def do_all(self):
|
1997-05-21 20:43:39 -03:00
|
|
|
self.prologue("The Whole Python FAQ")
|
|
|
|
print "<HR>"
|
1997-05-21 04:24:50 -03:00
|
|
|
names = os.listdir(os.curdir)
|
|
|
|
names.sort()
|
1997-05-21 18:31:39 -03:00
|
|
|
section = None
|
1997-05-21 04:24:50 -03:00
|
|
|
for name in names:
|
1997-05-21 13:52:18 -03:00
|
|
|
headers, text = self.read(name)
|
1997-05-21 04:24:50 -03:00
|
|
|
if headers:
|
1997-05-21 18:31:39 -03:00
|
|
|
title = headers['title']
|
|
|
|
i = string.find(title, '.')
|
|
|
|
nsec = title[:i]
|
|
|
|
if nsec != section:
|
|
|
|
section = nsec
|
1997-05-23 12:55:19 -03:00
|
|
|
if SECTIONS.has_key(section):
|
|
|
|
stitle = SECTIONS[section]
|
|
|
|
else:
|
|
|
|
stitle = ""
|
|
|
|
print "<H1>Section %s. %s</H1>" % (section, stitle)
|
1997-05-21 18:31:39 -03:00
|
|
|
print "<HR>"
|
1997-05-23 12:55:19 -03:00
|
|
|
self.show(name, title, text, edit=(self.edit != 'no'))
|
1997-05-21 18:31:39 -03:00
|
|
|
if not section:
|
1997-05-21 04:24:50 -03:00
|
|
|
print "No FAQ entries?!?!"
|
|
|
|
|
|
|
|
def do_roulette(self):
|
|
|
|
import whrandom
|
1997-05-21 20:43:39 -03:00
|
|
|
self.prologue("Python FAQ Roulette")
|
1997-05-21 04:24:50 -03:00
|
|
|
print """
|
|
|
|
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)
|
1997-05-21 13:52:18 -03:00
|
|
|
headers, text = self.read(name)
|
1997-05-21 04:24:50 -03:00
|
|
|
if headers:
|
1997-05-22 12:21:57 -03:00
|
|
|
self.show(name, headers['title'], text)
|
|
|
|
print "<P>Use `Reload' to show another one."
|
1997-05-21 04:24:50 -03:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
names.remove(name)
|
|
|
|
else:
|
|
|
|
print "No FAQ entries?!?!"
|
|
|
|
|
1997-05-21 18:31:39 -03:00
|
|
|
def do_recent(self):
|
|
|
|
import fnmatch, stat
|
|
|
|
names = os.listdir(os.curdir)
|
|
|
|
now = time.time()
|
|
|
|
list = []
|
|
|
|
for name in names:
|
|
|
|
if not fnmatch.fnmatch(name, NAMEPAT):
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
st = os.stat(name)
|
|
|
|
except os.error:
|
|
|
|
continue
|
|
|
|
tuple = (st[stat.ST_MTIME], name)
|
|
|
|
list.append(tuple)
|
|
|
|
list.sort()
|
|
|
|
list.reverse()
|
1997-05-21 20:43:39 -03:00
|
|
|
self.prologue("Python FAQ, Most Recently Modified First")
|
|
|
|
print "<HR>"
|
1997-05-21 18:31:39 -03:00
|
|
|
n = 0
|
|
|
|
for (mtime, name) in list:
|
|
|
|
headers, text = self.read(name)
|
1997-05-22 21:50:01 -03:00
|
|
|
if headers and headers.has_key('last-changed-date'):
|
1997-05-22 12:21:57 -03:00
|
|
|
self.show(name, headers['title'], text)
|
1997-05-21 18:31:39 -03:00
|
|
|
n = n+1
|
|
|
|
if not n:
|
|
|
|
print "No FAQ entries?!?!"
|
1997-05-21 04:24:50 -03:00
|
|
|
|
|
|
|
def do_query(self):
|
|
|
|
query = self.query
|
|
|
|
if not query:
|
1997-05-22 13:57:50 -03:00
|
|
|
self.error("No query string")
|
1997-05-21 04:24:50 -03:00
|
|
|
return
|
1997-05-22 13:57:50 -03:00
|
|
|
import regex
|
|
|
|
self.prologue("Python FAQ Query Results")
|
1997-05-21 04:24:50 -03:00
|
|
|
p = regex.compile(query, regex.casefold)
|
|
|
|
names = os.listdir(os.curdir)
|
|
|
|
names.sort()
|
|
|
|
print "<HR>"
|
|
|
|
n = 0
|
|
|
|
for name in names:
|
1997-05-21 13:52:18 -03:00
|
|
|
headers, text = self.read(name)
|
1997-05-21 04:24:50 -03:00
|
|
|
if headers:
|
|
|
|
title = headers['title']
|
1997-05-21 13:52:18 -03:00
|
|
|
if p.search(title) >= 0 or p.search(text) >= 0:
|
1997-05-22 12:21:57 -03:00
|
|
|
self.show(name, title, text)
|
1997-05-21 04:24:50 -03:00
|
|
|
n = n+1
|
|
|
|
if not n:
|
|
|
|
print "No hits."
|
|
|
|
|
1997-05-21 18:31:39 -03:00
|
|
|
def do_add(self):
|
|
|
|
section = self.section
|
|
|
|
if not section:
|
1997-05-21 20:43:39 -03:00
|
|
|
self.prologue("How to add a new FAQ entry")
|
1997-05-21 18:31:39 -03:00
|
|
|
print """
|
|
|
|
Go to the <A HREF="faq.py?req=index">FAQ index</A>
|
|
|
|
and click on the "Add new entry" link at the end
|
|
|
|
of the section to which you want to add the entry.
|
|
|
|
"""
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
nsec = string.atoi(section)
|
|
|
|
except ValueError:
|
|
|
|
print "Bad section number", nsec
|
|
|
|
names = os.listdir(os.curdir)
|
|
|
|
max = 0
|
|
|
|
import regex
|
|
|
|
prog = regex.compile(NAMEREG)
|
|
|
|
for name in names:
|
|
|
|
if prog.match(name) >= 0:
|
|
|
|
s1, s2 = prog.group(1, 2)
|
|
|
|
n1, n2 = string.atoi(s1), string.atoi(s2)
|
|
|
|
if n1 == nsec:
|
|
|
|
if n2 > max:
|
|
|
|
max = n2
|
|
|
|
if not max:
|
1997-05-22 13:57:50 -03:00
|
|
|
self.error("Can't add new sections yet.")
|
1997-05-21 18:31:39 -03:00
|
|
|
return
|
|
|
|
num = max+1
|
|
|
|
name = "faq%02d.%03d.htp" % (nsec, num)
|
|
|
|
self.name = name
|
|
|
|
self.add = "yes"
|
|
|
|
self.number = str(num)
|
|
|
|
self.do_edit()
|
|
|
|
|
1997-05-22 13:57:50 -03:00
|
|
|
def do_delete(self):
|
|
|
|
self.prologue("How to delete a FAQ entry")
|
|
|
|
print """
|
|
|
|
At the moment, there's no direct way to delete entries.
|
|
|
|
This is because the entry numbers are also their
|
|
|
|
unique identifiers -- it's a bad idea to renumber entries.
|
|
|
|
<P>
|
|
|
|
If you really think an entry needs to be deleted,
|
|
|
|
change the title to "(deleted)" and make the body
|
|
|
|
empty (keep the entry number in the title though).
|
|
|
|
"""
|
|
|
|
|
1997-05-21 04:24:50 -03:00
|
|
|
def do_edit(self):
|
|
|
|
name = self.name
|
1997-05-21 13:52:18 -03:00
|
|
|
headers, text = self.read(name)
|
1997-05-21 04:24:50 -03:00
|
|
|
if not headers:
|
1997-05-22 13:57:50 -03:00
|
|
|
self.error("Invalid file name", name)
|
1997-05-21 04:24:50 -03:00
|
|
|
return
|
1997-05-21 20:43:39 -03:00
|
|
|
self.prologue("Python FAQ Edit Form")
|
1997-05-21 04:24:50 -03:00
|
|
|
title = headers['title']
|
1997-05-21 20:43:39 -03:00
|
|
|
version = self.getversion(name)
|
1997-05-21 13:52:18 -03:00
|
|
|
print "<FORM METHOD=POST ACTION=faq.py>"
|
1997-05-21 18:31:39 -03:00
|
|
|
self.showedit(name, title, text)
|
|
|
|
if self.add:
|
|
|
|
print """
|
|
|
|
<INPUT TYPE=hidden NAME=add VALUE=%s>
|
|
|
|
<INPUT TYPE=hidden NAME=section VALUE=%s>
|
|
|
|
<INPUT TYPE=hidden NAME=number VALUE=%s>
|
|
|
|
""" % (self.add, self.section, self.number)
|
1997-05-21 04:24:50 -03:00
|
|
|
print """
|
|
|
|
<INPUT TYPE=submit VALUE="Review Edit">
|
|
|
|
<INPUT TYPE=hidden NAME=req VALUE=review>
|
|
|
|
<INPUT TYPE=hidden NAME=name VALUE=%s>
|
1997-05-21 20:43:39 -03:00
|
|
|
<INPUT TYPE=hidden NAME=version VALUE=%s>
|
1997-05-21 04:24:50 -03:00
|
|
|
</FORM>
|
|
|
|
<HR>
|
1997-05-21 20:43:39 -03:00
|
|
|
""" % (name, version)
|
1997-05-22 12:21:57 -03:00
|
|
|
self.show(name, title, text, edit=0)
|
1997-05-21 04:24:50 -03:00
|
|
|
|
|
|
|
def do_review(self):
|
1997-05-21 13:52:18 -03:00
|
|
|
if self.commit:
|
|
|
|
self.checkin()
|
|
|
|
return
|
1997-05-21 04:24:50 -03:00
|
|
|
name = self.name
|
|
|
|
text = self.text
|
|
|
|
title = self.title
|
1997-05-21 13:52:18 -03:00
|
|
|
headers, oldtext = self.read(name)
|
1997-05-21 04:24:50 -03:00
|
|
|
if not headers:
|
1997-05-22 13:57:50 -03:00
|
|
|
self.error("Invalid file name", name)
|
1997-05-21 04:24:50 -03:00
|
|
|
return
|
1997-05-22 13:57:50 -03:00
|
|
|
if self.author and '@' in self.email:
|
|
|
|
self.set_cookie(self.author, self.email)
|
1997-05-21 20:43:39 -03:00
|
|
|
self.prologue("Python FAQ Review Form")
|
|
|
|
print "<HR>"
|
1997-05-22 12:21:57 -03:00
|
|
|
self.show(name, title, text, edit=0)
|
1997-05-21 13:52:18 -03:00
|
|
|
print "<FORM METHOD=POST ACTION=faq.py>"
|
|
|
|
if self.log and self.author and '@' in self.email:
|
|
|
|
print """
|
|
|
|
<INPUT TYPE=submit NAME=commit VALUE="Commit">
|
|
|
|
Click this button to commit the change.
|
|
|
|
<P>
|
|
|
|
<HR>
|
|
|
|
<P>
|
|
|
|
"""
|
|
|
|
else:
|
|
|
|
print """
|
|
|
|
To commit this change, please enter your name,
|
|
|
|
email and a log message in the form below.
|
|
|
|
<P>
|
|
|
|
<HR>
|
|
|
|
<P>
|
|
|
|
"""
|
1997-05-21 18:31:39 -03:00
|
|
|
self.showedit(name, title, text)
|
|
|
|
if self.add:
|
|
|
|
print """
|
|
|
|
<INPUT TYPE=hidden NAME=add VALUE=%s>
|
|
|
|
<INPUT TYPE=hidden NAME=section VALUE=%s>
|
|
|
|
<INPUT TYPE=hidden NAME=number VALUE=%s>
|
|
|
|
""" % (self.add, self.section, self.number)
|
1997-05-21 04:24:50 -03:00
|
|
|
print """
|
|
|
|
<BR>
|
|
|
|
<INPUT TYPE=submit VALUE="Review Edit">
|
|
|
|
<INPUT TYPE=hidden NAME=req VALUE=review>
|
|
|
|
<INPUT TYPE=hidden NAME=name VALUE=%s>
|
1997-05-21 20:43:39 -03:00
|
|
|
<INPUT TYPE=hidden NAME=version VALUE=%s>
|
1997-05-21 04:24:50 -03:00
|
|
|
</FORM>
|
|
|
|
<HR>
|
1997-05-21 20:43:39 -03:00
|
|
|
""" % (name, self.version)
|
1997-05-21 04:24:50 -03:00
|
|
|
|
1997-05-21 19:25:56 -03:00
|
|
|
def do_info(self):
|
|
|
|
name = self.name
|
|
|
|
headers, text = self.read(name)
|
|
|
|
if not headers:
|
1997-05-22 13:57:50 -03:00
|
|
|
self.error("Invalid file name", name)
|
1997-05-21 19:25:56 -03:00
|
|
|
return
|
1997-05-22 21:50:01 -03:00
|
|
|
self.prologue("Info for %s" % name)
|
1997-05-21 19:25:56 -03:00
|
|
|
print '<PRE>'
|
|
|
|
sys.stdout.flush()
|
|
|
|
os.system("/depot/gnu/plat/bin/rlog -r %s </dev/null 2>&1" % self.name)
|
|
|
|
print '</PRE>'
|
|
|
|
print '<A HREF="faq.py?req=rlog&name=%s">View full rcs log</A>' % name
|
|
|
|
|
|
|
|
def do_rlog(self):
|
|
|
|
name = self.name
|
|
|
|
headers, text = self.read(name)
|
|
|
|
if not headers:
|
1997-05-22 13:57:50 -03:00
|
|
|
self.error("Invalid file name", name)
|
1997-05-21 19:25:56 -03:00
|
|
|
return
|
1997-05-22 21:50:01 -03:00
|
|
|
self.prologue("RCS log for %s" % name)
|
1997-05-21 19:25:56 -03:00
|
|
|
print '<PRE>'
|
|
|
|
sys.stdout.flush()
|
|
|
|
os.system("/depot/gnu/plat/bin/rlog %s </dev/null 2>&1" % self.name)
|
|
|
|
print '</PRE>'
|
|
|
|
|
1997-05-21 13:52:18 -03:00
|
|
|
def checkin(self):
|
|
|
|
import regsub, time, tempfile
|
|
|
|
name = self.name
|
|
|
|
|
|
|
|
headers, oldtext = self.read(name)
|
|
|
|
if not headers:
|
1997-05-22 13:57:50 -03:00
|
|
|
self.error("Invalid file name", name)
|
1997-05-21 13:52:18 -03:00
|
|
|
return
|
1997-05-21 20:43:39 -03:00
|
|
|
version = self.version
|
|
|
|
curversion = self.getversion(name)
|
|
|
|
if version != curversion:
|
1997-05-22 13:57:50 -03:00
|
|
|
self.error("Version conflict.",
|
|
|
|
"You edited version %s but current version is %s." % (
|
|
|
|
version, curversion),
|
|
|
|
'<A HREF="faq.py?req=show&name=%s">Reload.</A>' % name)
|
1997-05-21 20:43:39 -03:00
|
|
|
return
|
1997-05-21 13:52:18 -03:00
|
|
|
text = self.text
|
|
|
|
title = self.title
|
|
|
|
author = self.author
|
|
|
|
email = self.email
|
|
|
|
log = self.log
|
|
|
|
text = regsub.gsub("\r\n", "\n", text)
|
|
|
|
log = regsub.gsub("\r\n", "\n", log)
|
|
|
|
author = string.join(string.split(author))
|
|
|
|
email = string.join(string.split(email))
|
|
|
|
title = string.join(string.split(title))
|
|
|
|
oldtitle = headers['title']
|
|
|
|
oldtitle = string.join(string.split(oldtitle))
|
|
|
|
text = string.strip(text)
|
|
|
|
oldtext = string.strip(oldtext)
|
|
|
|
if text == oldtext and title == oldtitle:
|
1997-05-22 13:57:50 -03:00
|
|
|
self.error("No changes.")
|
1997-05-21 13:52:18 -03:00
|
|
|
return
|
1997-05-21 18:31:39 -03:00
|
|
|
# Check that the FAQ entry number didn't change
|
1997-05-21 13:52:18 -03:00
|
|
|
if string.split(title)[:1] != string.split(oldtitle)[:1]:
|
1997-05-22 13:57:50 -03:00
|
|
|
self.error("Don't change the FAQ entry number please.")
|
1997-05-21 13:52:18 -03:00
|
|
|
return
|
|
|
|
remhost = os.environ["REMOTE_HOST"]
|
|
|
|
remaddr = os.environ["REMOTE_ADDR"]
|
|
|
|
try:
|
|
|
|
os.unlink(name + "~")
|
|
|
|
except os.error:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
os.rename(name, name + "~")
|
|
|
|
except os.error:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
os.unlink(name)
|
|
|
|
except os.error:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
f = open(name, "w")
|
|
|
|
except IOError, msg:
|
1997-05-22 13:57:50 -03:00
|
|
|
self.error("Can't open", name, "for writing:", cgi.escape(str(msg)))
|
1997-05-21 13:52:18 -03:00
|
|
|
return
|
|
|
|
now = time.ctime(time.time())
|
|
|
|
f.write("Title: %s\n" % title)
|
|
|
|
f.write("Last-Changed-Date: %s\n" % now)
|
|
|
|
f.write("Last-Changed-Author: %s\n" % author)
|
|
|
|
f.write("Last-Changed-Email: %s\n" % email)
|
|
|
|
f.write("Last-Changed-Remote-Host: %s\n" % remhost)
|
|
|
|
f.write("Last-Changed-Remote-Address: %s\n" % remaddr)
|
|
|
|
keys = headers.keys()
|
|
|
|
keys.sort()
|
|
|
|
keys.remove('title')
|
|
|
|
for key in keys:
|
|
|
|
if key[:13] != 'last-changed-':
|
|
|
|
f.write("%s: %s\n" % (string.capwords(key, '-'),
|
|
|
|
headers[key]))
|
|
|
|
f.write("\n")
|
|
|
|
f.write(text)
|
|
|
|
f.write("\n")
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
tfn = tempfile.mktemp()
|
|
|
|
f = open(tfn, "w")
|
|
|
|
f.write("Last-Changed-Date: %s\n" % now)
|
|
|
|
f.write("Last-Changed-Author: %s\n" % author)
|
|
|
|
f.write("Last-Changed-Email: %s\n" % email)
|
|
|
|
f.write("Last-Changed-Remote-Host: %s\n" % remhost)
|
|
|
|
f.write("Last-Changed-Remote-Address: %s\n" % remaddr)
|
|
|
|
f.write("\n")
|
|
|
|
f.write(log)
|
|
|
|
f.write("\n")
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
p = os.popen("""
|
|
|
|
/depot/gnu/plat/bin/rcs -l %s </dev/null 2>&1
|
|
|
|
/depot/gnu/plat/bin/ci -u %s <%s 2>&1
|
|
|
|
rm -f %s
|
|
|
|
""" % (name, name, tfn, tfn))
|
|
|
|
output = p.read()
|
|
|
|
sts = p.close()
|
|
|
|
if not sts:
|
1997-05-22 13:57:50 -03:00
|
|
|
self.set_cookie(author, email)
|
1997-05-21 20:43:39 -03:00
|
|
|
self.prologue("Python FAQ Entry Edited")
|
|
|
|
print "<HR>"
|
1997-05-22 12:21:57 -03:00
|
|
|
self.show(name, title, text)
|
1997-05-21 13:52:18 -03:00
|
|
|
if output:
|
|
|
|
print "<PRE>%s</PRE>" % cgi.escape(output)
|
|
|
|
else:
|
1997-05-22 13:57:50 -03:00
|
|
|
self.error("Python FAQ Entry Commit Failed",
|
|
|
|
"Exit status 0x%04x" % sts)
|
1997-05-21 13:52:18 -03:00
|
|
|
if output:
|
|
|
|
print "<PRE>%s</PRE>" % cgi.escape(output)
|
1997-05-22 12:49:23 -03:00
|
|
|
print '<HR>'
|
|
|
|
print '<A HREF="faq.py?req=show&name=%s">Reload this entry.</A>' % name
|
1997-05-21 13:52:18 -03:00
|
|
|
|
1997-05-22 13:57:50 -03:00
|
|
|
def set_cookie(self, author, email):
|
|
|
|
name = "Python-FAQ-ID"
|
|
|
|
value = "%s;%s" % (author, email)
|
|
|
|
import urllib
|
|
|
|
value = urllib.quote(value)
|
|
|
|
print "Set-Cookie: %s=%s; path=/cgi-bin/;" % (name, value),
|
|
|
|
print "domain=%s;" % os.environ['HTTP_HOST'],
|
|
|
|
print "expires=Sat, 01-Jan-2000 00:00:00 GMT"
|
|
|
|
|
|
|
|
def get_cookie(self):
|
|
|
|
if not os.environ.has_key('HTTP_COOKIE'):
|
|
|
|
return "", ""
|
|
|
|
raw = os.environ['HTTP_COOKIE']
|
|
|
|
words = string.split(raw, ';')
|
|
|
|
cookies = {}
|
|
|
|
for word in words:
|
|
|
|
i = string.find(word, '=')
|
|
|
|
if i >= 0:
|
|
|
|
key, value = word[:i], word[i+1:]
|
|
|
|
cookies[key] = value
|
|
|
|
if not cookies.has_key('Python-FAQ-ID'):
|
|
|
|
return "", ""
|
|
|
|
value = cookies['Python-FAQ-ID']
|
|
|
|
import urllib
|
|
|
|
value = urllib.unquote(value)
|
|
|
|
i = string.rfind(value, ';')
|
|
|
|
author, email = value[:i], value[i+1:]
|
|
|
|
return author, email
|
|
|
|
|
1997-05-21 18:31:39 -03:00
|
|
|
def showedit(self, name, title, text):
|
1997-05-22 13:57:50 -03:00
|
|
|
author = self.author
|
|
|
|
email = self.email
|
|
|
|
if not author or not email:
|
|
|
|
a, e = self.get_cookie()
|
|
|
|
author = author or a
|
|
|
|
email = email or e
|
1997-05-21 13:52:18 -03:00
|
|
|
print """
|
1997-05-22 12:21:57 -03:00
|
|
|
Title: <INPUT TYPE=text SIZE=70 NAME=title VALUE="%s"><BR>
|
1997-05-21 13:52:18 -03:00
|
|
|
<TEXTAREA COLS=80 ROWS=20 NAME=text>""" % title
|
|
|
|
print cgi.escape(string.strip(text))
|
|
|
|
print """</TEXTAREA>
|
|
|
|
<BR>
|
|
|
|
Please provide the following information for logging purposes:
|
|
|
|
<BR>
|
1997-05-22 12:21:57 -03:00
|
|
|
<CODE>Name : </CODE><INPUT TYPE=text SIZE=40 NAME=author VALUE="%s">
|
|
|
|
<BR>
|
|
|
|
<CODE>Email: </CODE><INPUT TYPE=text SIZE=40 NAME=email VALUE="%s">
|
|
|
|
<BR>
|
1997-05-21 13:52:18 -03:00
|
|
|
Log message (reason for the change):<BR>
|
1997-05-22 13:57:50 -03:00
|
|
|
<TEXTAREA COLS=80 ROWS=5 NAME=log>%s\n</TEXTAREA>
|
|
|
|
""" % (author, email, self.log)
|
1997-05-21 04:24:50 -03:00
|
|
|
|
|
|
|
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>"
|
|
|
|
|
1997-05-22 12:21:57 -03:00
|
|
|
headers = None
|
|
|
|
|
1997-05-21 04:24:50 -03:00
|
|
|
def read(self, name):
|
1997-05-22 12:21:57 -03:00
|
|
|
self.headers = None
|
1997-05-21 04:24:50 -03:00
|
|
|
import fnmatch, rfc822
|
|
|
|
if not fnmatch.fnmatch(name, NAMEPAT):
|
|
|
|
return None, None
|
1997-05-21 18:31:39 -03:00
|
|
|
if self.add:
|
|
|
|
try:
|
|
|
|
fname = "faq%02d.%03d.htp" % (string.atoi(self.section),
|
|
|
|
string.atoi(self.number))
|
|
|
|
except ValueError:
|
|
|
|
return None, None
|
|
|
|
if fname != name:
|
|
|
|
return None, None
|
|
|
|
headers = {'title': "%s.%s. " % (self.section, self.number)}
|
|
|
|
text = ""
|
1997-05-22 12:21:57 -03:00
|
|
|
else:
|
|
|
|
f = open(name)
|
|
|
|
headers = rfc822.Message(f)
|
|
|
|
text = f.read()
|
|
|
|
f.close()
|
|
|
|
self.headers = headers
|
1997-05-21 13:52:18 -03:00
|
|
|
return headers, text
|
1997-05-21 04:24:50 -03:00
|
|
|
|
1997-05-22 12:21:57 -03:00
|
|
|
def show(self, name, title, text, edit=1):
|
1997-05-21 04:24:50 -03:00
|
|
|
# XXX Should put <A> tags around recognizable URLs
|
|
|
|
# XXX Should also turn "see section N" into hyperlinks
|
1997-05-21 18:31:39 -03:00
|
|
|
print "<H2>%s</H2>" % cgi.escape(title)
|
1997-05-21 04:24:50 -03:00
|
|
|
pre = 0
|
1997-05-21 13:52:18 -03:00
|
|
|
for line in string.split(text, '\n'):
|
1997-05-21 04:24:50 -03:00
|
|
|
if not string.strip(line):
|
|
|
|
if pre:
|
|
|
|
print '</PRE>'
|
|
|
|
pre = 0
|
|
|
|
else:
|
|
|
|
print '<P>'
|
|
|
|
else:
|
1997-05-23 01:44:30 -03:00
|
|
|
if line[0] not in string.whitespace:
|
1997-05-21 04:24:50 -03:00
|
|
|
if pre:
|
|
|
|
print '</PRE>'
|
|
|
|
pre = 0
|
|
|
|
else:
|
|
|
|
if not pre:
|
|
|
|
print '<PRE>'
|
|
|
|
pre = 1
|
1997-05-23 14:45:04 -03:00
|
|
|
if '/' in line or '@' in line:
|
|
|
|
line = self.translate(line)
|
1997-05-23 15:13:58 -03:00
|
|
|
elif '<' in line or '&' in line:
|
|
|
|
line = cgi.escape(line)
|
1997-05-23 14:45:04 -03:00
|
|
|
if not pre and '*' in line:
|
|
|
|
line = self.emphasize(line)
|
|
|
|
print line
|
1997-05-21 04:24:50 -03:00
|
|
|
if pre:
|
|
|
|
print '</PRE>'
|
|
|
|
pre = 0
|
|
|
|
print '<P>'
|
|
|
|
if edit:
|
1997-05-21 19:25:56 -03:00
|
|
|
print """
|
|
|
|
<A HREF="faq.py?req=edit&name=%s">Edit this entry</A> /
|
|
|
|
<A HREF="faq.py?req=info&name=%s" TARGET=_blank>Log info</A>
|
|
|
|
""" % (name, name)
|
1997-05-22 12:21:57 -03:00
|
|
|
if self.headers:
|
|
|
|
try:
|
|
|
|
date = self.headers['last-changed-date']
|
|
|
|
author = self.headers['last-changed-author']
|
|
|
|
email = self.headers['last-changed-email']
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
else:
|
1997-05-22 12:49:23 -03:00
|
|
|
s = '/ Last changed on %s by <A HREF="%s">%s</A>'
|
1997-05-22 12:21:57 -03:00
|
|
|
print s % (date, email, author)
|
1997-05-21 04:24:50 -03:00
|
|
|
print '<P>'
|
|
|
|
print "<HR>"
|
|
|
|
|
1997-05-21 20:43:39 -03:00
|
|
|
def getversion(self, name):
|
|
|
|
p = os.popen("/depot/gnu/plat/bin/rlog -h %s </dev/null 2>&1" % name)
|
1997-05-22 12:49:23 -03:00
|
|
|
head = "*new*"
|
1997-05-21 20:43:39 -03:00
|
|
|
while 1:
|
|
|
|
line = p.readline()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
if line[:5] == 'head:':
|
|
|
|
head = string.strip(line[5:])
|
|
|
|
p.close()
|
|
|
|
return head
|
|
|
|
|
|
|
|
def prologue(self, title):
|
|
|
|
title = cgi.escape(title)
|
1997-05-22 13:57:50 -03:00
|
|
|
print '''
|
1997-05-21 20:43:39 -03:00
|
|
|
<HTML>
|
|
|
|
<HEAD>
|
|
|
|
<TITLE>%s</TITLE>
|
|
|
|
</HEAD>
|
|
|
|
<BODY BACKGROUND="http://www.python.org/pics/RedShort.gif"
|
|
|
|
BGCOLOR="#FFFFFF"
|
|
|
|
TEXT="#000000"
|
|
|
|
LINK="#AA0000"
|
|
|
|
VLINK="#906A6A">
|
|
|
|
<H1>%s</H1>
|
|
|
|
''' % (title, title)
|
|
|
|
|
1997-05-22 13:57:50 -03:00
|
|
|
def error(self, *messages):
|
|
|
|
self.prologue("Python FAQ error")
|
|
|
|
print "Sorry, an error occurred:<BR>"
|
|
|
|
for message in messages:
|
|
|
|
print message,
|
|
|
|
print
|
|
|
|
|
1997-05-21 20:43:39 -03:00
|
|
|
def epilogue(self):
|
1997-05-23 14:45:04 -03:00
|
|
|
if self.edit == 'no':
|
|
|
|
global wanttime
|
|
|
|
wanttime = 0
|
|
|
|
else:
|
|
|
|
print '''
|
|
|
|
<P>
|
|
|
|
<HR>
|
|
|
|
<A HREF="http://www.python.org">Python home</A> /
|
1997-05-23 15:13:58 -03:00
|
|
|
<A HREF="faq.py?req=frontpage">FAQ home</A> /
|
1997-05-23 14:45:04 -03:00
|
|
|
Feedback to <A HREF="mailto:guido@python.org">GvR</A>
|
|
|
|
'''
|
1997-05-21 20:43:39 -03:00
|
|
|
print '''
|
|
|
|
</BODY>
|
|
|
|
</HTML>
|
|
|
|
'''
|
1997-05-21 04:24:50 -03:00
|
|
|
|
1997-05-23 01:44:30 -03:00
|
|
|
translate_prog = None
|
|
|
|
|
|
|
|
def translate(self, text):
|
|
|
|
if not self.translate_prog:
|
|
|
|
import regex
|
|
|
|
url = '\(http\|ftp\)://[^ \t\r\n]*'
|
|
|
|
email = '\<[-a-zA-Z0-9._]+@[-a-zA-Z0-9._]+'
|
|
|
|
self.translate_prog = prog = regex.compile(url + "\|" + email)
|
|
|
|
else:
|
|
|
|
prog = self.translate_prog
|
|
|
|
i = 0
|
|
|
|
list = []
|
|
|
|
while 1:
|
|
|
|
j = prog.search(text, i)
|
|
|
|
if j < 0:
|
|
|
|
break
|
|
|
|
list.append(cgi.escape(text[i:j]))
|
|
|
|
i = j
|
|
|
|
url = prog.group(0)
|
|
|
|
while url[-1] in ");:,.?":
|
|
|
|
url = url[:-1]
|
|
|
|
url = cgi.escape(url)
|
|
|
|
if ':' in url:
|
|
|
|
repl = '<A HREF="%s">%s</A>' % (url, url)
|
|
|
|
else:
|
|
|
|
repl = '<A HREF="mailto:%s"><%s></A>' % (url, url)
|
|
|
|
list.append(repl)
|
|
|
|
i = i + len(url)
|
|
|
|
j = len(text)
|
|
|
|
list.append(cgi.escape(text[i:j]))
|
|
|
|
return string.join(list, '')
|
|
|
|
|
1997-05-23 14:45:04 -03:00
|
|
|
emphasize_prog = None
|
|
|
|
|
|
|
|
def emphasize(self, line):
|
|
|
|
import regsub
|
|
|
|
if not self.emphasize_prog:
|
|
|
|
import regex
|
|
|
|
pat = "\*\([a-zA-Z]+\)\*"
|
|
|
|
self.emphasize_prog = prog = regex.compile(pat)
|
|
|
|
else:
|
|
|
|
prog = self.emphasize_prog
|
|
|
|
return regsub.gsub(prog, "<I>\\1</I>", line)
|
|
|
|
|
1997-05-22 13:57:50 -03:00
|
|
|
print "Content-type: text/html"
|
1997-05-21 13:52:18 -03:00
|
|
|
dt = 0
|
1997-05-23 14:45:04 -03:00
|
|
|
wanttime = 1
|
1997-05-21 04:24:50 -03:00
|
|
|
try:
|
1997-05-21 13:52:18 -03:00
|
|
|
import time
|
|
|
|
t1 = time.time()
|
1997-05-21 20:43:39 -03:00
|
|
|
import cgi, string, os, sys
|
1997-05-21 04:24:50 -03:00
|
|
|
x = FAQServer()
|
|
|
|
x.main()
|
1997-05-21 13:52:18 -03:00
|
|
|
t2 = time.time()
|
|
|
|
dt = t2-t1
|
1997-05-21 04:24:50 -03:00
|
|
|
except:
|
1997-05-22 13:57:50 -03:00
|
|
|
print "\n<HR>Sorry, an error occurred"
|
1997-05-21 04:24:50 -03:00
|
|
|
cgi.print_exception()
|
1997-05-23 14:45:04 -03:00
|
|
|
if wanttime:
|
|
|
|
print "<BR>(running time = %s seconds)" % str(round(dt, 3))
|
1997-05-22 12:21:57 -03:00
|
|
|
|
|
|
|
# The following bootstrap script must be placed in cgi-bin/faq.py:
|
|
|
|
BOOTSTRAP = """
|
|
|
|
#! /usr/local/bin/python
|
|
|
|
FAQDIR = "/usr/people/guido/python/FAQ"
|
|
|
|
import os, sys
|
|
|
|
os.chdir(FAQDIR)
|
|
|
|
sys.path.insert(0, os.curdir)
|
|
|
|
import faqmain
|
|
|
|
"""
|