From: Bernie Innocenti Date: Mon, 15 Dec 2008 02:42:35 +0000 (+0100) Subject: Fix pychecker warnings X-Git-Tag: v4.0~17^2~2 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=c76ddda68748f9bc67641ccba61314d8d8b32b12;p=geekigeeki.git Fix pychecker warnings --- diff --git a/geekigeeki.py b/geekigeeki.py index 1221e70..23b05d5 100755 --- a/geekigeeki.py +++ b/geekigeeki.py @@ -24,7 +24,6 @@ from time import clock 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) @@ -41,25 +40,25 @@ title_done = False # 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): @@ -120,7 +119,6 @@ def send_title(name, text="Limbo", msg_text=None, msg_type='error', writable=Fal print ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' print '' - site_name = globals().get('site_name', 'Unconfigured Site') print "%s: %s" % (site_name, text) print ' ' if not name: @@ -186,7 +184,7 @@ def send_title(name, text="Limbo", msg_text=None, msg_type='error', writable=Fal 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: @@ -281,10 +279,10 @@ def make_index_key(): links = map(lambda ch: '%s' % (ch, ch), 'abcdefghijklmnopqrstuvwxyz') return '

'+ ' | '.join(links) + '

' -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) @@ -351,8 +349,8 @@ class WikiFormatter: return Page(word).link_to() def _img_repl(self, word): - path = relative_url(word) - return '' % (path, path) + pathname = relative_url(word) + return '' % (pathname, pathname) def _url_repl(self, word): if img_re.match(word): @@ -493,9 +491,9 @@ class WikiFormatter: 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` @@ -584,10 +582,10 @@ class Page: 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: @@ -617,28 +615,28 @@ class Page: 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 @@ -662,7 +660,7 @@ class Page: 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 @@ -689,7 +687,7 @@ class Page: 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: @@ -707,9 +705,9 @@ class Page: send_guru("Write access denied by ACLs", "error") return - file = '' + filename = '' if 'file' in form: - file = form['file'].value + filename = form['file'].value print ('

Editing ' + self.page_name + ' for ' + cgi.escape(remote_user()) @@ -719,7 +717,7 @@ class Page: print '' % (self.page_name) print '
' % (self.page_name) print '' % cgi.escape(preview or self.get_raw_body()) - print ' ' % file + print ' ' % filename print """
@@ -739,7 +737,7 @@ class Page: print "

" WikiFormatter(preview).print_html() print "
" - send_footer(self.page_name) + send_footer() def send_raw(self, mimetype='text/plain'): if self.can_read(): @@ -799,40 +797,39 @@ class Page: 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() @@ -840,6 +837,6 @@ except Exception: send_guru(msg_text, "error") else: send_title(None, msg_text=msg_text) - send_footer(None) + send_footer() sys.stdout.flush()