From adb3a9db579c98afe778fe469d382c86693a3cf8 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 21 May 1997 07:24:50 +0000 Subject: Initial revision --- Tools/faqwiz/faqmain.py | 255 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+) create mode 100644 Tools/faqwiz/faqmain.py diff --git a/Tools/faqwiz/faqmain.py b/Tools/faqwiz/faqmain.py new file mode 100644 index 0000000..995cd3c --- /dev/null +++ b/Tools/faqwiz/faqmain.py @@ -0,0 +1,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 """ + Python FAQ (alpha 1) +

Python FAQ Front Page

+ + +
+ + + +
+ + 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 """ + Python FAQ +

Python FAQ

+
+ """ + 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 """ + Python FAQ Roulette +

Python FAQ Roulette

+ Please check the correctness of the entry below. + If you find any problems, please edit the entry. +

+


+ """ + 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 '

Show another one' + break + else: + names.remove(name) + else: + print "No FAQ entries?!?!" + + def do_search(self): + print """ + Search the Python FAQ +

Search the Python FAQ

+ +
+ + + +
+ """ + + def do_query(self): + import regex + print "Python FAQ Query Results" + print "

Python FAQ Query Results

" + 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 "
" + 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 """ + Python FAQ Edit Form +

Python FAQ Edit Form

+ """ + self.showheaders(headers) + title = headers['title'] + print """ +
+ + +
+ + + +
+
+ """ % 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 """ + Python FAQ Review Form +

Python FAQ Review Form

+ """ + self.show(name, {'title': title}, text) + print """ +
+ +

+


+ """ + self.showheaders(headers) + print """ + + +
+ + + +
+
+ """ % name + + def precheckin(self, name, text, title): + pass + + def showheaders(self, headers): + print "" + + 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 tags around recognizable URLs + # XXX Should also turn "see section N" into hyperlinks + title = headers['title'] + print "

%s

" % title + pre = 0 + for line in string.split(body, '\n'): + if not string.strip(line): + if pre: + print '' + pre = 0 + else: + print '

' + else: + if line == string.lstrip(line): + if pre: + print '' + pre = 0 + else: + if not pre: + print '

'
+			pre = 1
+		print line
+	if pre:
+	    print '
' + pre = 0 + print '

' + if edit: + print 'Edit this entry' %name + print '

' + print "


" + + +print "Content-type: text/html\n" +try: + x = FAQServer() + x.main() +except: + print "
Sorry, an error occurred" + cgi.print_exception() -- cgit v0.12