start_time = clock()
import cgi, sys, os, re, errno, stat
-from os import path, environ
# Regular expression defining a WikiWord
# (but this definition is also assumed in other places)
# CGI stuff ---------------------------------------------------------
def script_name():
- return environ.get('SCRIPT_NAME', '')
+ return os.environ.get('SCRIPT_NAME', '')
def privileged_path():
return privileged_url or script_name()
def remote_user():
- user = environ.get('REMOTE_USER', '')
+ user = os.environ.get('REMOTE_USER', '')
if user is None or user == '' or user == 'anonymous':
user = 'AnonymousCoward'
return user
def remote_host():
- return environ.get('REMOTE_ADDR', '')
+ return os.environ.get('REMOTE_ADDR', '')
def get_hostname(addr):
try:
from socket import gethostbyaddr
return gethostbyaddr(addr)[0] + ' (' + addr + ')'
- except Exception, er:
+ except Exception:
return addr
def relative_url(pathname, privileged=False):
print ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
print '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">'
- site_name = globals().get('site_name', 'Unconfigured Site')
print "<head><title>%s: %s</title>" % (site_name, text)
print ' <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />'
if not name:
def send_httperror(status="403 Not Found", query=""):
print "Status: %s" % status
send_title(None, msg_text=("%s: on query '%s'" % (status, query)))
- send_footer(None)
+ send_footer()
def link_tag(params, text=None, ss_class=None, privileged=False):
if text is None:
links = map(lambda ch: '<a href="#%s">%s</a>' % (ch, ch), 'abcdefghijklmnopqrstuvwxyz')
return '<p><center>'+ ' | '.join(links) + '</center></p>'
-def page_list(dir = None, re = word_re):
- return sorted(filter(re.match, os.listdir(dir or data_dir)))
+def page_list(dirname = None, re = word_re):
+ return sorted(filter(re.match, os.listdir(dirname or data_dir)))
-def send_footer(name, mod_string=None):
+def send_footer(mod_string=None):
if globals().get('debug_cgi', False):
cgi.print_arguments()
cgi.print_form(form)
return Page(word).link_to()
def _img_repl(self, word):
- path = relative_url(word)
- return '<a href="%s"><img border="0" src="%s" /></a>' % (path, path)
+ pathname = relative_url(word)
+ return '<a href="%s"><img border="0" src="%s" /></a>' % (pathname, pathname)
def _url_repl(self, word):
if img_re.match(word):
return res
def replace(self, match):
- for type, hit in match.groupdict().items():
+ for rule, hit in match.groupdict().items():
if hit:
- return getattr(self, '_' + type + '_repl')(hit)
+ return getattr(self, '_' + rule + '_repl')(hit)
else:
raise "Can't handle match " + `match`
return re.sub('([a-z])([A-Z])', r'\1 \2', self.page_name)
def _filename(self):
- return path.join(data_dir, self.page_name)
+ return os.path.join(data_dir, self.page_name)
def _tmp_filename(self):
- return path.join(data_dir, ('#' + self.page_name.replace('/','_') + '.' + `os.getpid()` + '#'))
+ return os.path.join(data_dir, ('#' + self.page_name.replace('/','_') + '.' + `os.getpid()` + '#'))
def exists(self):
try:
def format_dir(self):
out = '== '
- path = ''
- for dir in self.page_name.split('/'):
- path = (path + '/' + dir) if path else dir
- out += '[[' + path + '|' + dir + ']]/'
+ pathname = ''
+ for dirname in self.page_name.split('/'):
+ pathname = (pathname + '/' + dirname) if pathname else dirname
+ out += '[[' + pathname + '|' + dirname + ']]/'
out += ' ==\n'
- for file in page_list(self._filename(), file_re):
- if img_re.match(file):
+ for filename in page_list(self._filename(), file_re):
+ if img_re.match(filename):
if image_maxwidth:
maxwidth_arg = '|maxwidth=' + str(image_maxwidth)
- out += '{{' + self.page_name + '/' + file + '|' + file + maxwidth_arg + '}}\n'
+ out += '{{' + self.page_name + '/' + filename + '|' + filename + maxwidth_arg + '}}\n'
else:
- out += ' * [[' + self.page_name + '/' + file + ']]\n'
+ out += ' * [[' + self.page_name + '/' + filename + ']]\n'
return out
def get_attrs(self):
if 'attrs' in self.__dict__:
return self.attrs
self.attrs = {}
try:
- file = open(self._filename(), 'rt')
+ f = open(self._filename(), 'rt')
attr_re = re.compile(r"^#(\S*)(.*)$")
- for line in file:
+ for line in f:
m = attr_re.match(line)
if not m:
break
if user == remote_user() or user == "All":
return action in perms.split(',')
return False
- except Exception, er:
+ except Exception:
if acl:
self.msg_text = 'Illegal acl line: ' + acl
return default
send_title(self.page_name, self.split_title(),
msg_text=self.msg_text, msg_type=self.msg_type, writable=self.can_write())
self.send_naked()
- send_footer(self.page_name, self._last_modified())
+ send_footer(self._last_modified())
def _last_modified(self):
try:
send_guru("Write access denied by ACLs", "error")
return
- file = ''
+ filename = ''
if 'file' in form:
- file = form['file'].value
+ filename = form['file'].value
print ('<p><b>Editing ' + self.page_name
+ ' for ' + cgi.escape(remote_user())
print '<input type="hidden" name="edit" value="%s">' % (self.page_name)
print '<input type="input" id="editor" name="changelog" value="Edit page %s" accesskey="c" /><br />' % (self.page_name)
print '<textarea wrap="off" spellcheck="true" id="editor" name="savetext" rows="17" cols="100" accesskey="e">%s</textarea>' % cgi.escape(preview or self.get_raw_body())
- print '<label for="file" accesskey="u">Or Upload a file:</label> <input type="file" name="file" value="%s" />' % file
+ print '<label for="file" accesskey="u">Or Upload a file:</label> <input type="file" name="file" value="%s" />' % filename
print """
<br />
<input type="submit" name="save" value="Save" accesskey="s">
print "<div class='preview'>"
WikiFormatter(preview).print_html()
print "</div>"
- send_footer(self.page_name)
+ send_footer()
def send_raw(self, mimetype='text/plain'):
if self.can_read():
self.msg_text = 'Thank you for your contribution. Your attention to detail is appreciated.'
self.msg_type = 'success'
-# Main ---------------------------------------------------------------
-try:
- execfile("geekigeeki.conf.py")
- form = cgi.FieldStorage()
-
+def main():
for cmd in form:
handler = globals().get('handle_' + cmd)
if handler:
handler(form[cmd].value)
break
else:
- path_info = environ.get('PATH_INFO', '')
+ path_info = os.environ.get('PATH_INFO', '')
if len(path_info) and path_info[0] == '/':
query = path_info[1:] or 'FrontPage'
else:
- query = environ.get('QUERY_STRING', '') or 'FrontPage'
+ query = os.environ.get('QUERY_STRING', '') or 'FrontPage'
if file_re.match(query):
if word_re.match(query):
Page(query).format()
else:
from mimetypes import MimeTypes
- type, encoding = MimeTypes().guess_type(query)
- #type = type or 'text/plain'
- #Page(query).send_raw(mimetype=type)
- if type:
- if type.startswith('image/'):
- Page(query).send_image(mimetype=type,args=form)
+ mimetype, encoding = MimeTypes().guess_type(query)
+ if mimetype:
+ if mimetype.startswith('image/'):
+ Page(query).send_image(mimetype=mimetype, args=form)
else:
- Page(query).send_raw(mimetype=type)
+ Page(query).send_raw(mimetype=mimetype)
else:
Page(query).format()
else:
send_httperror("403 Forbidden", query)
+
+try:
+ execfile("geekigeeki.conf.py")
+ form = cgi.FieldStorage()
+ main()
except Exception:
import traceback
msg_text = traceback.format_exc()
send_guru(msg_text, "error")
else:
send_title(None, msg_text=msg_text)
- send_footer(None)
+ send_footer()
sys.stdout.flush()