From cd285df131b7e5fe14ba69d7160586694c5f9d5e Mon Sep 17 00:00:00 2001 From: Bernie Innocenti Date: Sat, 25 Apr 2009 15:41:30 +0900 Subject: [PATCH] Rework action dispatcher --- geekigeeki.py | 67 ++++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/geekigeeki.py b/geekigeeki.py index 8bd3e0b..6170d73 100755 --- a/geekigeeki.py +++ b/geekigeeki.py @@ -143,7 +143,7 @@ def send_title(name, text="Limbo", msg_text=None, msg_type='error', writable=Fal if name and writable and privileged_url is not None: print(' ' \ - % (privileged_path() + '?edit=' + name)) + % (privileged_path() + '?a=edit&q=' + name)) if history_url is not None: print(' ' \ @@ -153,7 +153,7 @@ def send_title(name, text="Limbo", msg_text=None, msg_type='error', writable=Fal # Body if name and writable and privileged_url is not None: - print('') + print('') else: print('') @@ -177,7 +177,7 @@ def send_title(name, text="Limbo", msg_text=None, msg_type='error', writable=Fal print(' | ' + link_tag('?raw=' + name, 'Raw Text', 'navlink')) if privileged_url is not None: if writable: - print(' | ' + link_tag('?edit=' + name, 'Edit', 'navlink', privileged=True)) + print(' | ' + link_tag('?a=edit&q=' + name, 'Edit', 'navlink', privileged=True)) else: print(' | ' + link_tag(name, 'Login', 'navlink', privileged=True)) @@ -232,8 +232,12 @@ def link_inline(name, descr=None, kvargs={}): # Search --------------------------------------------------- -def handle_fullsearch(needle): - send_title(None, 'Full text search for "%s"' % (needle)) +def print_search_stats(hits, searched): + print("

%d hits out of %d pages searched.

" % (hits, searched)) + +def handle_fullsearch(query, form): + needle = form['q'].value + send_title(None, 'Full text search for "' + needle + '"') needle_re = re.compile(needle, re.IGNORECASE) hits = [] @@ -259,9 +263,10 @@ def handle_fullsearch(needle): print_search_stats(len(hits), len(all_pages)) -def handle_titlesearch(needle): +def handle_titlesearch(query, form): # TODO: check needle is legal -- but probably we can just accept any RE - send_title(None, "Title search for \"" + needle + '"') + needle = form['q'].value + send_title(None, 'Title search for "' + needle + '"') needle_re = re.compile(needle, re.IGNORECASE) all_pages = page_list() @@ -274,22 +279,19 @@ def handle_titlesearch(needle): print_search_stats(len(hits), len(all_pages)) -def print_search_stats(hits, searched): - print("

%d hits out of %d pages searched.

" % (hits, searched)) - -def handle_raw(pagename): +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): +def handle_edit(pagename, form): if not file_re.match(pagename): send_httperror("403 Forbidden", pagename) return - pg = Page(pagename) + pg = Page(form['q'].value) if 'save' in form: if form['file'].value: pg.save(form['file'].file.read(), form['changelog'].value) @@ -306,6 +308,18 @@ def handle_edit(pagename): text = form['savetext'].value pg.send_editor(text) +def handle_get(pagename, form): + if file_re.match(pagename): + # FIMXE: this is all bullshit, MimeTypes bases its guess on the extension! + from mimetypes import MimeTypes + mimetype, encoding = MimeTypes().guess_type(pagename) + if mimetype: + Page(pagename).send_raw(mimetype=mimetype, args=form) + else: + Page(pagename).format() + else: + send_httperror("403 Forbidden", pagename) + # Used by macros/WordIndex and macros/TitleIndex def make_index_key(): links = ['%s' % (ch, ch) for ch in 'abcdefghijklmnopqrstuvwxyz'] @@ -800,29 +814,16 @@ class Page: self.msg_text = 'Thank you for your contribution. Your attention to detail is appreciated.' self.msg_type = 'success' -def main(): - for cmd in form: - handler = globals().get('handle_' + cmd) - if handler: - handler(form[cmd].value) - break - else: - query = query_string() - if file_re.match(query): - # FIMXE: this is all bullshit, MimeTypes bases its guess on the extension! - from mimetypes import MimeTypes - mimetype, encoding = MimeTypes().guess_type(query) - if mimetype: - Page(query).send_raw(mimetype=mimetype, args=form) - else: - Page(query).format() - else: - send_httperror("403 Forbidden", query) - try: exec(open("geekigeeki.conf.py").read()) form = cgi.FieldStorage() - main() + action = form.getvalue('a', 'get') + handler = globals().get('handle_' + action) + if handler: + handler(query_string(), form) + else: + send_httperror("403 Forbidden", query_string()) + except Exception: import traceback msg_text = traceback.format_exc() -- 2.25.1