Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f66cccf

Browse files
committed
Restructure: move all wiki code into a separate module.
1 parent 26a1ac4 commit f66cccf

2 files changed

Lines changed: 116 additions & 113 deletions

File tree

Demo/cgi/cgi3.py

Lines changed: 1 addition & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -4,119 +4,7 @@
44

55
import cgitb; cgitb.enable()
66

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", "view")
15-
page = form.getvalue("page", "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 = "/tmp"
24-
scripturl = os.path.basename(sys.argv[0])
25-
26-
def __init__(self, name):
27-
if not self.iswikiword(name):
28-
raise ValueError, "page name is not a wiki word"
29-
self.name = name
30-
self.load()
31-
32-
def cmd_view(self, form):
33-
print "<h1>", escape(self.splitwikiword(self.name)), "</h1>"
34-
print "<p>"
35-
for line in self.data.splitlines():
36-
line = line.rstrip()
37-
if not line:
38-
print "<p>"
39-
continue
40-
words = re.split('(\W+)', line)
41-
for i in range(len(words)):
42-
word = words[i]
43-
if self.iswikiword(word):
44-
if os.path.isfile(self.mkfile(word)):
45-
word = self.mklink("view", word, word)
46-
else:
47-
word = self.mklink("new", word, word + "*")
48-
else:
49-
word = escape(word)
50-
words[i] = word
51-
print "".join(words)
52-
print "<hr>"
53-
print "<p>", self.mklink("edit", self.name, "Edit this page") + ";"
54-
print self.mklink("view", "FrontPage", "go to front page") + "."
55-
56-
def cmd_edit(self, form, label="Change"):
57-
print "<h1>", label, self.name, "</h1>"
58-
print '<form method="POST" action="%s">' % self.scripturl
59-
s = '<textarea cols="70" rows="20" name="text">%s</textarea>'
60-
print s % self.data
61-
print '<input type="hidden" name="cmd" value="create">'
62-
print '<input type="hidden" name="page" value="%s">' % self.name
63-
print '<br>'
64-
print '<input type="submit" value="%s Page">' % label
65-
print "</form>"
66-
67-
def cmd_create(self, form):
68-
self.data = form.getvalue("text", "").strip()
69-
error = self.store()
70-
if error:
71-
print "<h1>I'm sorry. That didn't work</h1>"
72-
print "<p>An error occurred while attempting to write the file:"
73-
print "<p>", escape(error)
74-
else:
75-
self.cmd_view(form)
76-
77-
def cmd_new(self, form):
78-
self.cmd_edit(form, label="Create Page")
79-
80-
def iswikiword(self, word):
81-
return re.match("[A-Z][a-z]+([A-Z][a-z]*)+", word)
82-
83-
def splitwikiword(self, word):
84-
chars = []
85-
for c in word:
86-
if chars and c.isupper():
87-
chars.append(' ')
88-
chars.append(c)
89-
return "".join(chars)
90-
91-
def mkfile(self, name=None):
92-
if name is None:
93-
name = self.name
94-
return os.path.join(self.homedir, name)
95-
96-
def mklink(self, cmd, page, text):
97-
link = self.scripturl + "?cmd=" + cmd + "&page=" + page
98-
return '<a href="%s">%s</a>' % (link, text)
99-
100-
def load(self):
101-
try:
102-
f = open(self.mkfile())
103-
data = f.read().strip()
104-
f.close()
105-
except IOError:
106-
data = ""
107-
self.data = data
108-
109-
def store(self):
110-
data = self.data
111-
try:
112-
f = open(self.mkfile(), "w")
113-
f.write(data)
114-
if data and not data.endswith('\n'):
115-
f.write('\n')
116-
f.close()
117-
return ""
118-
except IOError, err:
119-
return "IOError: %s" % str(err)
7+
from wiki import main
1208

1219
if __name__ == "__main__":
12210
main()

Demo/cgi/wiki.py

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

0 commit comments

Comments
 (0)