Merge send_verbatim() with Page.send_raw()
authorBernardo Innocenti <bernie@codewiz.org>
Tue, 8 Jan 2008 22:00:51 +0000 (23:00 +0100)
committerBernardo Innocenti <bernie@codewiz.org>
Tue, 8 Jan 2008 22:00:51 +0000 (23:00 +0100)
geekigeeki.py

index 5340646c63101b24629d96a26ff2adea8e66a3b7..a1c99a02742b3a862e2590af9b64addcd40fe6a3 100755 (executable)
@@ -84,7 +84,7 @@ def send_guru(msg, msg_type):
     if msg_type == 'error':
         print '      Guru Meditation #DEADBEEF.ABADC0DE'
     print '</pre>'
-    # FIXME: This simple JS snippet is harder to pass than ACID 3.0 
+    # FIXME: This little JS snippet is harder to pass than ACID 3.0 
     print """
     <script language="JavaScript" type="text/javascript">
         var guru = document.getElementById('guru');
@@ -222,14 +222,10 @@ def do_titlesearch(needle):
 def print_search_stats(hits, searched):
     print "<p>%d hits out of %d pages searched.</p>" % (hits, searched)
 
-#TODO: merge into do_savepage()
-def do_edit(pagename):
-    Page(pagename).send_editor()
-
 def do_raw(pagename):
     Page(pagename).send_raw()
 
-def do_savepage(pagename):
+def do_edit(pagename):
     global form
     pg = Page(pagename)
     if 'preview' in form:
@@ -242,7 +238,7 @@ def do_savepage(pagename):
         pg.msg_type = 'notice'
         pg.send_page()
     else:
-        raise 'What did you press?'
+        pg.send_editor()
 
 def make_index_key():
     s = '<p><center>'
@@ -494,7 +490,7 @@ class PageFormatter:
 
         # For each line, we scan through looking for magic
         # strings, outputting verbatim any intervening text
-        # TODO: highlight search words (look at referer)
+        # TODO: highlight search words (look at referrer)
         scan_re = re.compile(
             r"(?:"
             # Formatting
@@ -566,8 +562,7 @@ class Page:
         self.msg_type = 'error'
 
     def split_title(self):
-        # look for the end of words and the start of a new word,
-        # and insert a space there
+        # look for the end of words and the start of a new word and insert a space there
         return re.sub('([a-z])([A-Z])', r'\1 \2', self.page_name)
 
     def _text_filename(self):
@@ -594,7 +589,7 @@ class Page:
 
     def get_raw_body(self):
         try:
-            return open(self._text_filename(), 'rt').read()
+            return open(self._text_filename(), 'rb').read()
         except IOError, er:
             if er.errno == errno.ENOENT:
                 return '' # just doesn't exist, use default
@@ -645,7 +640,6 @@ class Page:
         if self.can_write():
             page_name = self.page_name
 
-        #FIXME: are there security implications?
         #css foo.css bar.css
         global css_url
         css_url = css_url + self.get_attr("css", "").split()
@@ -678,7 +672,7 @@ class Page:
             + ' from ' + cgi.escape(get_hostname(remote_host()))
             + '</b></p>')
         print '<div class="editor"><form method="post" action="%s">' % relative_url(self.page_name)
-        print '<input type="hidden" name="savepage" value="%s">' % (self.page_name)
+        print '<input type="hidden" name="edit" value="%s">' % (self.page_name)
         print """<textarea wrap="off" spellcheck="true" id="editor" name="savetext" rows="17" cols="100">%s</textarea>""" % (preview or self.get_raw_body())
         print """
             <br />
@@ -695,12 +689,13 @@ class Page:
             print "</div>"
         send_footer(self.page_name)
 
-    def send_raw(self):
+    def send_raw(self, mimetype='text/plain'):
         if not self.can_read():
             send_title(None, msg='Read access denied by ACLs', msg_type='notice')
             return
-        emit_header("text/plain")
-        print self.get_raw_body()
+        body = self.get_raw_body()
+        emit_header(mimetype)
+        print body
 
     def _write_file(self, text):
         tmp_filename = self._tmp_filename()
@@ -741,24 +736,16 @@ class Page:
             self.msg = 'Thank you for your contribution.  Your attention to detail is appreciated.'
             self.msg_type = 'success'
 
-#TODO: merge into send_raw()
-def send_verbatim(filename, mime_type='application/octet-stream'):
-    pathname = path.join(data_dir, filename)
-    data = open(pathname, 'rb').read()
-    emit_header(mime_type)
-    sys.stdout.write(data)
-
 # Main ---------------------------------------------------------------
 try:
     execfile("geekigeeki.conf.py")
-
     form = cgi.FieldStorage()
-
-    handlers = { 'fullsearch':  do_fullsearch,
-                 'titlesearch': do_titlesearch,
-                 'edit':        do_edit,
-                 'raw':         do_raw,
-                 'savepage':    do_savepage }
+    handlers = {
+        'fullsearch':  do_fullsearch,
+        'titlesearch': do_titlesearch,
+        'edit':        do_edit,
+        'raw':         do_raw,
+    }
 
     for cmd in handlers.keys():
         if form.has_key(cmd):
@@ -776,9 +763,9 @@ try:
                 Page(query).send_page()
             elif img_re.match(query):
                 #FIXME: use correct mime type
-                send_verbatim(query, 'image/jpeg')
+                Page(query).send_raw(mimetype='image/jpeg')
             else:
-                send_verbatim(query)
+                Page(query).send_raw(mimetype='application/octet-stream')
         else:
             print "Status: 404 Not Found"
             send_title(None, msg='Can\'t work out query: ' + query)