Created
October 29, 2010 17:17
-
-
Save philchristensen/653929 to your computer and use it in GitHub Desktop.
My first python project, a wiki; this is from back when WikiWikiWeb was all there was.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import cgi, sys, os, traceback, string, re, binascii, urlparse | |
import cgitb; cgitb.enable() | |
try: | |
import cPickle as pickle | |
except: | |
import pickle | |
datafile = 'db/wabi' | |
form = cgi.FieldStorage() | |
if os.environ.has_key('HTTP_REFERER') and os.environ['HTTP_REFERER'] != '': | |
referer = os.environ['HTTP_REFERER'] | |
else: | |
referer = "/cgi-bin/wabi.cgi" | |
if(form.has_key('admin') and form['admin'].value == 'beeblebrox'): | |
is_admin = 1 | |
else: | |
is_admin = 0 | |
if os.path.exists(datafile): | |
file = open(datafile) | |
data = pickle.load(open(datafile, 'r')) | |
file.close() | |
else: | |
data = {} | |
def header(item): | |
print "Content-Type: text/html" | |
print "<HTML>" | |
print "<HEAD>" | |
print "<TITLE>wabi</TITLE>" | |
print "<STYLE>" | |
print "<!--" | |
print "A:link, A:visited, A:active {" | |
print " text-decoration: none;" | |
print "}" | |
print "-->" | |
print "</STYLE>" | |
print "</HEAD>" | |
print "<BODY BGCOLOR=\"white\" LINK=\"black\" VLINK=\"black\">" | |
print '<TABLE WIDTH="100%" BGCOLOR="lightgrey"><TR><TD><FONT SIZE=5><B>' + item + "</B></FONT></TD></TR></TABLE>" | |
def footer(): | |
print "<HR>" | |
print "<I>Wabi Engine</I>, by <A HREF=\"mailto:[email protected]\">Phil Christensen</A><BR>" | |
print "</BODY>" | |
print "</HTML>" | |
def edit(item, content): | |
header(item) | |
print "<FORM action=wabi.cgi method=post>" | |
print "<TEXTAREA name=content rows=24 cols=80>" + content + "</TEXTAREA><BR>" | |
print "<INPUT type=hidden name=commit value=\"" + item + "\">" | |
print "<INPUT type=submit value=Submit>" | |
print "</FORM>" | |
footer() | |
def view(item): | |
match_object = re.match(r'\w+?://\S+', data[item]) | |
if match_object is not None: | |
print "Location: " + match_object.group() + "\n" | |
else: | |
header(item) | |
print "<PRE>" | |
print re.sub(r'\[(?P<word>.+?)\]', url_filter, data[item], 0) | |
print "</PRE>" | |
print "<HR>" | |
print "[" | |
if(is_admin): | |
print "<A HREF=\"wabi.cgi?edit=" + escape(item) + "\">edit this page</A> | " | |
print "<A HREF=\"wabi.cgi\">start again</A> | " | |
print "<A HREF=\"" + referer + "\">go back</A> " | |
print "]<BR>" | |
if(is_admin): | |
print "<FORM ACTION=\"wabi.cgi\" METHOD=POST>" | |
print "<INPUT TYPE=TEXT NAME=edit SIZE=30><INPUT TYPE=Submit VALUE=Edit>" | |
print "</FORM>" | |
print "<FORM ACTION=\"wabi.cgi\" METHOD=POST>" | |
print "<INPUT TYPE=TEXT NAME=view SIZE=30><INPUT TYPE=Submit VALUE=View>" | |
print "</FORM>" | |
footer() | |
def commit(item, content): | |
data[item] = content | |
persist() | |
view(item) | |
def delete(item): | |
del data[item] | |
view('the Beginning') | |
def debug(): | |
header("wabi.cgi debug") | |
print "<TABLE BORDER=1>" | |
print "<TR><TD><B>Key</B></TD><TD><B>Value</B></TD></TR>" | |
for item in data.keys(): | |
print "<TR VALIGN=TOP><TD WIDTH=100><A HREF=\"wabi.cgi?view=" + escape(item) + "\">" + item + "</A></TD><TD>" + data[item] + "</TD></TR>" | |
print "</TABLE>" | |
footer() | |
def url_filter(obj): | |
dict = obj.groupdict() | |
match = dict['word'] | |
if data.has_key(match): | |
if re.match(r'\w+?://\S+', match): | |
return r'<A HREF="' + escape(match) + r'"><U>' + match + r'</U></A>' | |
else: | |
return r'<A HREF="wabi.cgi?view=' + escape(match) + r'"><B>' + match + r'</B></A>' | |
else: | |
return r'<A HREF="wabi.cgi?view=' + escape(match) + r'">' + match + r'</A>' | |
def escape(str): | |
result = "" | |
for char in str: | |
if(char not in string.ascii_letters and char not in string.digits): | |
result = result + "%" + binascii.hexlify(char) | |
else: | |
result = result + char | |
return result | |
def persist(): | |
file = open(datafile, 'w') | |
pickle.dump(data, file) | |
file.close() | |
if form.has_key('edit'): | |
if data.has_key(form['edit'].value): | |
edit(form['edit'].value, data[form['edit'].value]) | |
else: | |
edit(form['edit'].value, "") | |
elif form.has_key('view'): | |
if data.has_key(form['view'].value): | |
view(form['view'].value) | |
else: | |
edit(form['view'].value, "") | |
elif form.has_key('commit'): | |
if form.has_key('content'): | |
commit(form['commit'].value, form['content'].value) | |
else: | |
view(form['commit'].value) | |
elif form.has_key('delete'): | |
if data.has_key(form['delete'].value): | |
delete(form['delete'].value) | |
else: | |
view('the Beginning') | |
elif form.has_key('debug'): | |
debug() | |
else: | |
if data.has_key('the Beginning'): | |
view('the Beginning') | |
else: | |
edit('the Beginning', "") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment