From 699af67492bf63859b6dedf1afea72a2471c11f8 Mon Sep 17 00:00:00 2001 From: Bernardo Innocenti Date: Sun, 23 Sep 2007 11:14:27 +0200 Subject: [PATCH] Move config to a separate file; Code cleanup. --- geekigeeki.py | 68 ++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 39 deletions(-) diff --git a/geekigeeki.py b/geekigeeki.py index ad4fc5d..5ae1bd3 100755 --- a/geekigeeki.py +++ b/geekigeeki.py @@ -103,15 +103,16 @@ def send_title(name, text="Limbo", msg=None, msg_type='error'): # Head emit_header() - print """ - -""" + print '' + print '' + + site_name = globals().get('site_name', 'Unconfigured Site') print "%s: %s" % (site_name, text) print ' ' if not name: print ' ' - if css_url: + if globals().has_key('css_url'): print ' ' % css_url print '' @@ -133,10 +134,12 @@ def send_title(name, text="Limbo", msg=None, msg_type='error'): print text + ' ' print ' | ' + link_tag('FrontPage', 'Front Page', 'navlink') print ' | ' + link_tag('FindPage', 'Find Page', 'navlink') - print ' | Recent Changes' + if globals().has_key('history_url'): + print ' | Recent Changes' + if name: + print ' | Page History' if name: - print ' | Page History' print ' | ' + link_tag('?raw=' + name, 'Raw Text', 'navlink') if privileged_url is not None: print ' | ' + link_tag('?edit=' + name, 'Edit Page', 'navlink', authentication=True) @@ -187,7 +190,7 @@ def do_fullsearch(needle): for (count, page_name) in hits: print '
  • ' + Page(page_name).link_to() print ' . . . . ' + `count` - print ['match', 'matches'][count <> 1] + print ['match', 'matches'][count != 1] print '

  • ' print "" @@ -211,6 +214,7 @@ def do_titlesearch(needle): def print_search_stats(hits, searched): print "

    %d hits out of %d pages searched.

    " % (hits, searched) +#TODO: merge into do_savepage() def do_edit(pagename): Page(pagename).send_editor() @@ -244,7 +248,7 @@ def page_list(): return filter(word_re.match, os.listdir(text_dir)) def send_footer(name, mod_string=None): - if debug_cgi: + if globals().get('debug_cgi', False): cgi.print_arguments() cgi.print_form(cgi.FieldStorage()) cgi.print_environ() @@ -290,7 +294,7 @@ def _macro_WordIndex(*vargs): # set title for word in all_words: letter = string.lower(word[0]) - if letter <> last_letter: + if letter != last_letter: s = s + ';

    %s

    ' % (letter, letter) last_letter = letter @@ -312,7 +316,7 @@ def _macro_TitleIndex(*vargs): current_letter = None for name in pages: letter = string.lower(name[0]) - if letter <> current_letter: + if letter != current_letter: s = s + '

    %s

    ' % (letter, letter) current_letter = letter else: @@ -571,10 +575,10 @@ class Page: def exists(self): try: os.stat(self._text_filename()) - return 1 + return True except OSError, er: if er.errno == errno.ENOENT: - return 0 + return False else: raise er @@ -590,10 +594,8 @@ class Page: return open(self._text_filename(), 'rt').read() except IOError, er: if er.errno == errno.ENOENT: - # just doesn't exist, use default - return 'Describe %s here.' % self.page_name - else: - raise er + return '' # just doesn't exist, use default + raise er def get_attrs(self): if self.attrs: @@ -647,10 +649,13 @@ class Page: send_footer(page_name, self._last_modified()) def _last_modified(self): - if not self.exists(): + try: + from time import localtime, strftime + modtime = localtime(os.stat(self._text_filename())[stat.ST_MTIME]) + except OSError, er: + if er.errno != errno.ENOENT: + raise er return None - from time import localtime, strftime - modtime = localtime(os.stat(self._text_filename())[stat.ST_MTIME]) return strftime(datetime_fmt, modtime) def send_editor(self, preview=None): @@ -697,7 +702,7 @@ class Page: try: os.remove(text) except OSError, er: - if er.errno <> errno.ENOENT: raise er + if er.errno != errno.ENOENT: raise er os.rename(tmp_filename, text) def save_text(self, newtext): @@ -727,6 +732,7 @@ 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(text_dir, filename) data = open(pathname, 'rb').read() @@ -735,23 +741,7 @@ def send_verbatim(filename, mime_type='application/octet-stream'): # Main --------------------------------------------------------------- try: - # Configuration values - site_name = 'Codewiz' - - # set to None for read-only sites, leave empty ('') to allow anonymous edits - # otherwise, set to a URL that requires authentication - privileged_url = 'https://www.codewiz.org/~bernie/wiki' - - data_dir = '/home/bernie/public_html/wiki' - text_dir = path.join(data_dir, 'text') - css_url = '../wikidata/geekigeeki.css' # optional stylesheet link - history_url = '../wikigit/wiki.git' - post_edit_hook = './post_edit_hook.sh' - datetime_fmt = '%a %d %b %Y %I:%M %p' - allow_edit = True # Is it possible to edit pages? - show_hosts = True # show hostnames? - nonexist_pfx = '' # prefix before nonexistent link (usually '?') - debug_cgi = False # Set to True for CGI var dump + execfile("geekigeeki.conf.py") form = cgi.FieldStorage() @@ -781,7 +771,7 @@ try: else: send_verbatim(query) else: - # TODO: return 404? + print "Status: 404 Not Found" send_title(None, msg='Can\'t work out query: ' + query) except: import traceback -- 2.25.1