From f66cccf506b935b2a38355f86ff676a8b6326faa Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Thu, 17 Oct 2002 15:53:02 +0000 Subject: Restructure: move all wiki code into a separate module. --- Demo/cgi/cgi3.py | 114 +----------------------------------------------------- Demo/cgi/wiki.py | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 113 deletions(-) create mode 100644 Demo/cgi/wiki.py diff --git a/Demo/cgi/cgi3.py b/Demo/cgi/cgi3.py index 205f63d..a3421b5 100755 --- a/Demo/cgi/cgi3.py +++ b/Demo/cgi/cgi3.py @@ -4,119 +4,7 @@ import cgitb; cgitb.enable() -import os, re, cgi, sys -escape = cgi.escape - -def main(): - form = cgi.FieldStorage() - print "Content-type: text/html" - print - cmd = form.getvalue("cmd", "view") - page = form.getvalue("page", "FrontPage") - wiki = WikiPage(page) - wiki.load() - method = getattr(wiki, 'cmd_' + cmd, None) or wiki.cmd_view - method(form) - -class WikiPage: - - homedir = "/tmp" - scripturl = os.path.basename(sys.argv[0]) - - def __init__(self, name): - if not self.iswikiword(name): - raise ValueError, "page name is not a wiki word" - self.name = name - self.load() - - def cmd_view(self, form): - print "

", escape(self.splitwikiword(self.name)), "

" - print "

" - for line in self.data.splitlines(): - line = line.rstrip() - if not line: - print "

" - continue - words = re.split('(\W+)', line) - for i in range(len(words)): - word = words[i] - if self.iswikiword(word): - if os.path.isfile(self.mkfile(word)): - word = self.mklink("view", word, word) - else: - word = self.mklink("new", word, word + "*") - else: - word = escape(word) - words[i] = word - print "".join(words) - print "


" - print "

", self.mklink("edit", self.name, "Edit this page") + ";" - print self.mklink("view", "FrontPage", "go to front page") + "." - - def cmd_edit(self, form, label="Change"): - print "

", label, self.name, "

" - print '
' % self.scripturl - s = '' - print s % self.data - print '' - print '' % self.name - print '
' - print '' % label - print "
" - - def cmd_create(self, form): - self.data = form.getvalue("text", "").strip() - error = self.store() - if error: - print "

I'm sorry. That didn't work

" - print "

An error occurred while attempting to write the file:" - print "

", escape(error) - else: - self.cmd_view(form) - - def cmd_new(self, form): - self.cmd_edit(form, label="Create Page") - - def iswikiword(self, word): - return re.match("[A-Z][a-z]+([A-Z][a-z]*)+", word) - - def splitwikiword(self, word): - chars = [] - for c in word: - if chars and c.isupper(): - chars.append(' ') - chars.append(c) - return "".join(chars) - - def mkfile(self, name=None): - if name is None: - name = self.name - return os.path.join(self.homedir, name) - - def mklink(self, cmd, page, text): - link = self.scripturl + "?cmd=" + cmd + "&page=" + page - return '%s' % (link, text) - - def load(self): - try: - f = open(self.mkfile()) - data = f.read().strip() - f.close() - except IOError: - data = "" - self.data = data - - def store(self): - data = self.data - try: - f = open(self.mkfile(), "w") - f.write(data) - if data and not data.endswith('\n'): - f.write('\n') - f.close() - return "" - except IOError, err: - return "IOError: %s" % str(err) +from wiki import main if __name__ == "__main__": main() diff --git a/Demo/cgi/wiki.py b/Demo/cgi/wiki.py new file mode 100644 index 0000000..f0208a4 --- /dev/null +++ b/Demo/cgi/wiki.py @@ -0,0 +1,115 @@ +"""Wiki main program. Imported and run by cgi3.py.""" + +import os, re, cgi, sys +escape = cgi.escape + +def main(): + form = cgi.FieldStorage() + print "Content-type: text/html" + print + cmd = form.getvalue("cmd", "view") + page = form.getvalue("page", "FrontPage") + wiki = WikiPage(page) + wiki.load() + method = getattr(wiki, 'cmd_' + cmd, None) or wiki.cmd_view + method(form) + +class WikiPage: + + homedir = "/tmp" + scripturl = os.path.basename(sys.argv[0]) + + def __init__(self, name): + if not self.iswikiword(name): + raise ValueError, "page name is not a wiki word" + self.name = name + self.load() + + def cmd_view(self, form): + print "

", escape(self.splitwikiword(self.name)), "

" + print "

" + for line in self.data.splitlines(): + line = line.rstrip() + if not line: + print "

" + continue + words = re.split('(\W+)', line) + for i in range(len(words)): + word = words[i] + if self.iswikiword(word): + if os.path.isfile(self.mkfile(word)): + word = self.mklink("view", word, word) + else: + word = self.mklink("new", word, word + "*") + else: + word = escape(word) + words[i] = word + print "".join(words) + print "


" + print "

", self.mklink("edit", self.name, "Edit this page") + ";" + print self.mklink("view", "FrontPage", "go to front page") + "." + + def cmd_edit(self, form, label="Change"): + print "

", label, self.name, "

" + print '
' % self.scripturl + s = '' + print s % self.data + print '' + print '' % self.name + print '
' + print '' % label + print "
" + + def cmd_create(self, form): + self.data = form.getvalue("text", "").strip() + error = self.store() + if error: + print "

I'm sorry. That didn't work

" + print "

An error occurred while attempting to write the file:" + print "

", escape(error) + else: + self.cmd_view(form) + + def cmd_new(self, form): + self.cmd_edit(form, label="Create Page") + + def iswikiword(self, word): + return re.match("[A-Z][a-z]+([A-Z][a-z]*)+", word) + + def splitwikiword(self, word): + chars = [] + for c in word: + if chars and c.isupper(): + chars.append(' ') + chars.append(c) + return "".join(chars) + + def mkfile(self, name=None): + if name is None: + name = self.name + return os.path.join(self.homedir, name + ".txt") + + def mklink(self, cmd, page, text): + link = self.scripturl + "?cmd=" + cmd + "&page=" + page + return '%s' % (link, text) + + def load(self): + try: + f = open(self.mkfile()) + data = f.read().strip() + f.close() + except IOError: + data = "" + self.data = data + + def store(self): + data = self.data + try: + f = open(self.mkfile(), "w") + f.write(data) + if data and not data.endswith('\n'): + f.write('\n') + f.close() + return "" + except IOError, err: + return "IOError: %s" % str(err) -- cgit v0.12