Guido van Rossum | 6807721 | 2002-10-16 21:01:27 +0000 | [diff] [blame^] | 1 | #!/usr/local/bin/python |
| 2 | |
| 3 | """CGI test 3 (persistent data).""" |
| 4 | |
| 5 | import cgitb; cgitb.enable() |
| 6 | |
| 7 | import os, re, cgi, sys |
| 8 | escape = cgi.escape |
| 9 | |
| 10 | def main(): |
| 11 | form = cgi.FieldStorage() |
| 12 | print "Content-type: text/html" |
| 13 | print |
| 14 | cmd = form.getvalue("cmd") or "view" |
| 15 | page = form.getvalue("page") or "FrontPage" |
| 16 | wiki = WikiPage(page) |
| 17 | wiki.load() |
| 18 | method = getattr(wiki, 'cmd_' + cmd, None) or wiki.cmd_view |
| 19 | method(form) |
| 20 | |
| 21 | class WikiPage: |
| 22 | |
| 23 | homedir = os.path.dirname(sys.argv[0]) |
| 24 | scripturl = os.path.basename(sys.argv[0]) |
| 25 | |
| 26 | def __init__(self, name): |
| 27 | self.name = name |
| 28 | self.load() |
| 29 | |
| 30 | def cmd_view(self, form): |
| 31 | print "<h1>", escape(self.splitwikiword(self.name)), "</h1>" |
| 32 | print "<p>" |
| 33 | for line in self.data.splitlines(): |
| 34 | line = line.rstrip() |
| 35 | if not line: |
| 36 | print "<p>" |
| 37 | continue |
| 38 | words = re.split('(\W+)', line) |
| 39 | for i in range(len(words)): |
| 40 | word = words[i] |
| 41 | if self.iswikiword(word): |
| 42 | if os.path.isfile(self.mkfile(word)): |
| 43 | word = self.mklink("view", word, word) |
| 44 | else: |
| 45 | word = self.mklink("new", word, word + "*") |
| 46 | else: |
| 47 | word = escape(word) |
| 48 | words[i] = word |
| 49 | print "".join(words) |
| 50 | print "<hr>" |
| 51 | print "<p>", self.mklink("edit", self.name, "Edit this page") + "," |
| 52 | print self.mklink("view", "FrontPage", "go to front page") + "." |
| 53 | |
| 54 | def cmd_edit(self, form, label="Change"): |
| 55 | print "<h1>", label, self.name, "</h1>" |
| 56 | print '<form method="POST" action="%s">' % self.scripturl |
| 57 | s = '<textarea cols="70" rows="20" name="text">%s</textarea>' |
| 58 | print s % self.data |
| 59 | print '<input type="hidden" name="cmd" value="create">' |
| 60 | print '<input type="hidden" name="page" value="%s">' % self.name |
| 61 | print '<br>' |
| 62 | print '<input type="submit" value="%s Page">' % label |
| 63 | print "</form>" |
| 64 | |
| 65 | def cmd_create(self, form): |
| 66 | self.data = form.getvalue("text", "").strip() |
| 67 | self.store() |
| 68 | self.cmd_view(form) |
| 69 | |
| 70 | def cmd_new(self, form): |
| 71 | self.cmd_edit(form, label="Create Page") |
| 72 | |
| 73 | def iswikiword(self, word): |
| 74 | return re.match("[A-Z][a-z]+([A-Z][a-z]*)+", word) |
| 75 | |
| 76 | def splitwikiword(self, word): |
| 77 | chars = [] |
| 78 | for c in word: |
| 79 | if chars and c.isupper(): |
| 80 | chars.append(' ') |
| 81 | chars.append(c) |
| 82 | return "".join(chars) |
| 83 | |
| 84 | def mkfile(self, name=None): |
| 85 | if name is None: |
| 86 | name = self.name |
| 87 | return os.path.join(self.homedir, name) |
| 88 | |
| 89 | def mklink(self, cmd, page, text): |
| 90 | link = self.scripturl + "?cmd=" + cmd + "&page=" + page |
| 91 | return '<a href="%s">%s</a>' % (link, text) |
| 92 | |
| 93 | def load(self): |
| 94 | try: |
| 95 | f = open(self.mkfile()) |
| 96 | data = f.read().strip() |
| 97 | f.close() |
| 98 | except IOError: |
| 99 | data = "" |
| 100 | self.data = data |
| 101 | |
| 102 | def store(self): |
| 103 | data = self.data |
| 104 | try: |
| 105 | f = open(self.mkfile(), "w") |
| 106 | f.write(data) |
| 107 | f.close() |
| 108 | return "" |
| 109 | except IOError, err: |
| 110 | return "IOError: %s" % str(err) |
| 111 | |
| 112 | if __name__ == "__main__": |
| 113 | main() |