Initial (nonworking) support for ATOM
authorBernie Innocenti <bernie@codewiz.org>
Sat, 3 Apr 2010 23:25:31 +0000 (20:25 -0300)
committerBernie Innocenti <bernie@codewiz.org>
Sat, 3 Apr 2010 23:40:32 +0000 (20:40 -0300)
geekigeeki.py

index e45a3ba3fef75914c3872cd03ef059aecd383a6f..d63850fb57b8e55494930871cc8f4f23271b3b5c 100755 (executable)
@@ -3,7 +3,7 @@
 #
 # Copyright (C) 1999, 2000 Martin Pool <mbp@humbug.org.au>
 # Copyright (C) 2002 Gerardo Poggiali
-# Copyright (C) 2007, 2008, 2009 Bernie Innocenti <bernie@codewiz.org>
+# Copyright (C) 2007, 2008, 2009, 2010 Bernie Innocenti <bernie@codewiz.org>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as
@@ -123,10 +123,14 @@ def send_guru(msg_text, msg_type):
     print('</pre><script type="text/javascript" src="%s" defer="defer"></script>' \
         % relative_url('sys/GuruMeditation.js'))
 
-def send_httperror(status="403 Not Found", query=""):
+def send_httperror(status="403 Not Found", query="", trace=False):
     print("Status: %s" % status)
+    msg_text = "%s: on query '%s'" % (status, query)
+    if trace:
+        import traceback
+        msg_text += '\n\n' + traceback.format_exc()
     page = Page()
-    page.send_title(msg_text=("%s: on query '%s'" % (status, query)))
+    page.send_title(msg_text=msg_text)
     page.send_footer()
 
 def link_tag(dest, text=None, privileged=False, **kvargs):
@@ -218,28 +222,23 @@ def handle_titlesearch(query, form):
     WikiFormatter(out).print_html()
 
 def handle_raw(pagename, form):
-    if not file_re.match(pagename):
-        send_httperror("403 Forbidden", pagename)
-        return
-
     Page(pagename).send_raw()
 
-def handle_edit(pagename, form):
-    if not file_re.match(pagename):
-        send_httperror("403 Forbidden", pagename)
-        return
+def handle_atom(pagename, form):
+    Page(pagename).send_atom()
 
+def handle_edit(pagename, form):
     pg  = Page(form['q'].value)
     if 'save' in form:
         if form['file'].value:
             pg.save(form['file'].file.read(), form['changelog'].value)
         else:
             pg.save(form['savetext'].value.replace('\r\n', '\n'), form['changelog'].value)
-        pg.format()
+        pg.send()
     elif 'cancel' in form:
         pg.msg_text = 'Editing canceled'
         pg.msg_type = 'notice'
-        pg.format()
+        pg.send()
     else: # preview or edit
         text = None
         if 'preview' in form:
@@ -247,16 +246,13 @@ def handle_edit(pagename, form):
         pg.send_editor(text)
 
 def handle_get(pagename, form):
-        if file_re.match(pagename):
-            if not ext_re.search(pagename): # FIXME: no extension guesses a wiki page
-                Page(pagename).format()
-            else:
-                # FIMXE: this is all bullshit, MimeTypes bases its guess on the extension!
-                from mimetypes import MimeTypes
-                mimetype, encoding = MimeTypes().guess_type(pagename)
-                Page(pagename).send_raw(mimetype=mimetype, args=form)
-        else:
-            send_httperror("403 Forbidden", pagename)
+    if not ext_re.search(pagename): # FIXME: no extension guesses a wiki page
+        Page(pagename).send()
+    else:
+        # FIMXE: this is all bullshit, MimeTypes bases its guess on the extension!
+        from mimetypes import MimeTypes
+        mimetype, encoding = MimeTypes().guess_type(pagename)
+        Page(pagename).send_raw(mimetype=mimetype, args=form)
 
 # Used by sys/macros/WordIndex and sys/macros/TitleIndex
 def make_index_key():
@@ -530,11 +526,18 @@ class WikiFormatter:
         print(self._indent_to(0))
         print('</p></div>')
 
+class HttpException(Exception):
+    def __init__(self, error, query):
+        self.error = error
+        self.query = query
+
 class Page:
     def __init__(self, page_name="Limbo"):
         self.page_name = page_name.rstrip('/');
         self.msg_text = ''
         self.msg_type = 'error'
+        if not file_re.match(self.page_name):
+            raise HttpException("403 Forbidden", self.page_name)
 
     def split_title(self):
         # look for the end of words and the start of a new word and insert a space there
@@ -709,7 +712,7 @@ class Page:
         footer = self.pragma("footer", "sys/footer")
         if footer != "off":
             link_inline(footer, kvargs = {
-                'LAST_MODIFIED': strftime(config_get('datetime_fmt', '%a %d %b %Y %I:%M %p'), localtime(self._mtime()))
+                'LAST_MODIFIED': strftime(config_get('datetime_fmt', '%Y-%m-%dT%I:%M:%S%p'), localtime(self._mtime()))
             })
         print("</body></html>")
 
@@ -719,7 +722,7 @@ class Page:
         else:
             send_guru("Read access denied by ACLs", "notice")
 
-    def format(self):
+    def send(self):
         #css foo.css
         value = self.pragma("css", None)
         if value:
@@ -730,6 +733,18 @@ class Page:
         self.send_naked()
         self.send_footer()
 
+    def send_atom(self):
+        emit_header(self._mtime(), 'application/atom+xml')
+        self.in_html = True
+        link_inline("sys/atom_header", kvargs = {
+            'LAST_MODIFIED': strftime(config_get('datetime_fmt', '%a, %d %b %Y %I:%M:%S %p'), localtime(self._mtime()))
+        })
+        self.in_html = False
+        self.send_naked()
+        self.in_html = True
+        link_inline("sys/atom_footer")
+        self.in_html = False
+
     def send_editor(self, preview=None):
         self.send_title(text='Edit ' + self.split_title(), msg_text=self.msg_text, msg_type=self.msg_type)
         if not self.can_write():
@@ -818,14 +833,9 @@ try:
     else:
         send_httperror("403 Forbidden", query_string())
 
+except HttpException, e:
+    send_httperror(e.error, query=e.query)
 except Exception:
-    import traceback
-    msg_text = traceback.format_exc()
-    page = Page()
-    if title_done:
-        send_guru(msg_text, "error")
-    else:
-        page.send_title(msg_text=msg_text)
-    page.send_footer()
+    send_httperror("500 Internal Server Error", query=query_string(), trace=True)
 
 sys.stdout.flush()