annotate Demo/cgi/wiki.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents dc420de17a6e
children 6b18a095c2e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25818
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Wiki main program. Imported and run by cgi3.py."""
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
25820
12227d79f6ba Use tempfile.gettempdir() to get the temp directory rather than
Guido van Rossum <guido@python.org>
parents: 25818
diff changeset
3 import os, re, cgi, sys, tempfile
25818
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 escape = cgi.escape
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 def main():
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 form = cgi.FieldStorage()
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 print "Content-type: text/html"
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 print
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 cmd = form.getvalue("cmd", "view")
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 page = form.getvalue("page", "FrontPage")
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 wiki = WikiPage(page)
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 method = getattr(wiki, 'cmd_' + cmd, None) or wiki.cmd_view
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 method(form)
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 class WikiPage:
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
25820
12227d79f6ba Use tempfile.gettempdir() to get the temp directory rather than
Guido van Rossum <guido@python.org>
parents: 25818
diff changeset
18 homedir = tempfile.gettempdir()
25818
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 scripturl = os.path.basename(sys.argv[0])
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 def __init__(self, name):
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 if not self.iswikiword(name):
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 raise ValueError, "page name is not a wiki word"
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 self.name = name
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 self.load()
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 def cmd_view(self, form):
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 print "<h1>", escape(self.splitwikiword(self.name)), "</h1>"
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 print "<p>"
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 for line in self.data.splitlines():
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 line = line.rstrip()
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 if not line:
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 print "<p>"
25829
dc420de17a6e Refactor cmd_view(): move line formatting to a helper method.
Guido van Rossum <guido@python.org>
parents: 25828
diff changeset
34 else:
dc420de17a6e Refactor cmd_view(): move line formatting to a helper method.
Guido van Rossum <guido@python.org>
parents: 25828
diff changeset
35 print self.formatline(line)
25818
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 print "<hr>"
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 print "<p>", self.mklink("edit", self.name, "Edit this page") + ";"
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 print self.mklink("view", "FrontPage", "go to front page") + "."
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
25829
dc420de17a6e Refactor cmd_view(): move line formatting to a helper method.
Guido van Rossum <guido@python.org>
parents: 25828
diff changeset
40 def formatline(self, line):
dc420de17a6e Refactor cmd_view(): move line formatting to a helper method.
Guido van Rossum <guido@python.org>
parents: 25828
diff changeset
41 words = []
dc420de17a6e Refactor cmd_view(): move line formatting to a helper method.
Guido van Rossum <guido@python.org>
parents: 25828
diff changeset
42 for word in re.split('(\W+)', line):
dc420de17a6e Refactor cmd_view(): move line formatting to a helper method.
Guido van Rossum <guido@python.org>
parents: 25828
diff changeset
43 if self.iswikiword(word):
dc420de17a6e Refactor cmd_view(): move line formatting to a helper method.
Guido van Rossum <guido@python.org>
parents: 25828
diff changeset
44 if os.path.isfile(self.mkfile(word)):
dc420de17a6e Refactor cmd_view(): move line formatting to a helper method.
Guido van Rossum <guido@python.org>
parents: 25828
diff changeset
45 word = self.mklink("view", word, word)
dc420de17a6e Refactor cmd_view(): move line formatting to a helper method.
Guido van Rossum <guido@python.org>
parents: 25828
diff changeset
46 else:
dc420de17a6e Refactor cmd_view(): move line formatting to a helper method.
Guido van Rossum <guido@python.org>
parents: 25828
diff changeset
47 word = self.mklink("new", word, word + "*")
dc420de17a6e Refactor cmd_view(): move line formatting to a helper method.
Guido van Rossum <guido@python.org>
parents: 25828
diff changeset
48 else:
dc420de17a6e Refactor cmd_view(): move line formatting to a helper method.
Guido van Rossum <guido@python.org>
parents: 25828
diff changeset
49 word = escape(word)
dc420de17a6e Refactor cmd_view(): move line formatting to a helper method.
Guido van Rossum <guido@python.org>
parents: 25828
diff changeset
50 words.append(word)
dc420de17a6e Refactor cmd_view(): move line formatting to a helper method.
Guido van Rossum <guido@python.org>
parents: 25828
diff changeset
51 return "".join(words)
dc420de17a6e Refactor cmd_view(): move line formatting to a helper method.
Guido van Rossum <guido@python.org>
parents: 25828
diff changeset
52
25818
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 def cmd_edit(self, form, label="Change"):
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 print "<h1>", label, self.name, "</h1>"
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 print '<form method="POST" action="%s">' % self.scripturl
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 s = '<textarea cols="70" rows="20" name="text">%s</textarea>'
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 print s % self.data
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 print '<input type="hidden" name="cmd" value="create">'
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 print '<input type="hidden" name="page" value="%s">' % self.name
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 print '<br>'
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 print '<input type="submit" value="%s Page">' % label
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 print "</form>"
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 def cmd_create(self, form):
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 self.data = form.getvalue("text", "").strip()
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 error = self.store()
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 if error:
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 print "<h1>I'm sorry. That didn't work</h1>"
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 print "<p>An error occurred while attempting to write the file:"
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 print "<p>", escape(error)
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 else:
25828
7ea674ac590e In cmd_create(), use a redirect directive instead of viewing the page;
Guido van Rossum <guido@python.org>
parents: 25823
diff changeset
72 # Use a redirect directive, to avoid "reload page" problems
7ea674ac590e In cmd_create(), use a redirect directive instead of viewing the page;
Guido van Rossum <guido@python.org>
parents: 25823
diff changeset
73 print "<head>"
7ea674ac590e In cmd_create(), use a redirect directive instead of viewing the page;
Guido van Rossum <guido@python.org>
parents: 25823
diff changeset
74 s = '<meta http-equiv="refresh" content="1; URL=%s">'
7ea674ac590e In cmd_create(), use a redirect directive instead of viewing the page;
Guido van Rossum <guido@python.org>
parents: 25823
diff changeset
75 print s % (self.scripturl + "?cmd=view&page=" + self.name)
7ea674ac590e In cmd_create(), use a redirect directive instead of viewing the page;
Guido van Rossum <guido@python.org>
parents: 25823
diff changeset
76 print "<head>"
7ea674ac590e In cmd_create(), use a redirect directive instead of viewing the page;
Guido van Rossum <guido@python.org>
parents: 25823
diff changeset
77 print "<h1>OK</h1>"
7ea674ac590e In cmd_create(), use a redirect directive instead of viewing the page;
Guido van Rossum <guido@python.org>
parents: 25823
diff changeset
78 print "<p>If nothing happens, please click here:",
7ea674ac590e In cmd_create(), use a redirect directive instead of viewing the page;
Guido van Rossum <guido@python.org>
parents: 25823
diff changeset
79 print self.mklink("view", self.name, self.name)
25818
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 def cmd_new(self, form):
25828
7ea674ac590e In cmd_create(), use a redirect directive instead of viewing the page;
Guido van Rossum <guido@python.org>
parents: 25823
diff changeset
82 self.cmd_edit(form, label="Create")
25818
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 def iswikiword(self, word):
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 return re.match("[A-Z][a-z]+([A-Z][a-z]*)+", word)
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 def splitwikiword(self, word):
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 chars = []
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 for c in word:
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 if chars and c.isupper():
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 chars.append(' ')
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 chars.append(c)
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 return "".join(chars)
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 def mkfile(self, name=None):
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 if name is None:
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 name = self.name
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 return os.path.join(self.homedir, name + ".txt")
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 def mklink(self, cmd, page, text):
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 link = self.scripturl + "?cmd=" + cmd + "&page=" + page
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 return '<a href="%s">%s</a>' % (link, text)
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 def load(self):
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 try:
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 f = open(self.mkfile())
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 data = f.read().strip()
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 f.close()
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 except IOError:
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 data = ""
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 self.data = data
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 def store(self):
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 data = self.data
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 try:
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 f = open(self.mkfile(), "w")
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 f.write(data)
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 if data and not data.endswith('\n'):
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 f.write('\n')
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 f.close()
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 return ""
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 except IOError, err:
79419897a36f Restructure: move all wiki code into a separate module.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 return "IOError: %s" % str(err)