From 669293de1e428ec397e5b48c017ced9bfd646729 Mon Sep 17 00:00:00 2001 From: Krytarik Raido Date: Thu, 22 Feb 2018 11:45:04 +0100 Subject: [PATCH] Encyclopedia: Add Python 3 support. --- Bantracker/cgi/bans.cgi | 6 ++--- Encyclopedia/__init__.py | 9 ++++--- Encyclopedia/factoids.cgi | 32 ++++++++++++------------ Encyclopedia/plugin.py | 51 +++++++++++++++++++++------------------ commoncgi.py | 26 ++++++++++++-------- 5 files changed, 68 insertions(+), 56 deletions(-) diff --git a/Bantracker/cgi/bans.cgi b/Bantracker/cgi/bans.cgi index 02e6efc..8b1ac7c 100755 --- a/Bantracker/cgi/bans.cgi +++ b/Bantracker/cgi/bans.cgi @@ -14,10 +14,10 @@ ### import sys -import time -import urllib +import time, datetime, pytz import ConfigParser import sqlite +import cPickle as pickle CONFIG_FILENAME = "bantracker.conf" config = ConfigParser.RawConfigParser() @@ -110,7 +110,7 @@ def urlencode(**kwargs): """Return the url options as a string, inserting additional ones if given.""" d = dict([ (i.name, i.value) for i in form.list ]) d.update(kwargs) - return urllib.urlencode(d.items()) + return utils.web.urlencode(d.items()) def isTrue(value): """Returns True if the form value is one of "1", "true", "yes", or "on", case insensitive""" diff --git a/Encyclopedia/__init__.py b/Encyclopedia/__init__.py index e806e7c..2b6fa46 100644 --- a/Encyclopedia/__init__.py +++ b/Encyclopedia/__init__.py @@ -22,8 +22,9 @@ funtionality has been moved to PackageInfo import supybot import supybot.world as world +from imp import reload -__version__ = "2.4" +__version__ = "2.5" __author__ = supybot.Author("Krytarik Raido", "krytarik", "krytarik@tuxgarage.com") __contributors__ = { supybot.Author("Dennis Kaarsemaker", "Seveas", "dennis@kaarsemaker.net"): ['Original Author'], @@ -31,13 +32,13 @@ __contributors__ = { } __url__ = 'https://launchpad.net/ubuntu-bots' -import config +from . import config reload(config) -import plugin +from . import plugin reload(plugin) if world.testing: - import test + from . import test Class = plugin.Class configure = config.configure diff --git a/Encyclopedia/factoids.cgi b/Encyclopedia/factoids.cgi index 9a3fe8c..d13c315 100755 --- a/Encyclopedia/factoids.cgi +++ b/Encyclopedia/factoids.cgi @@ -98,7 +98,7 @@ if 'search' in form: # Select factoids if search: - keys = [urllib2.unquote(x.strip()) for x in search.split() if len(x.strip()) >=2][:5] + keys = [utils.web.urlunquote(x.strip()) for x in search.split() if len(x.strip()) >=2][:5] values = [] if not keys: keys = [''] @@ -127,19 +127,19 @@ else: # Pagination links npages = int(math.ceil(total / float(NUM_PER_PAGE))) -print '·' +print('·') for i in range(npages): - print '%d ·' % (database, search, order_by, i, i+1) + print('%d ·' % (database, search, order_by, i, i+1)) -print '
Order by
·'; -print ' %s ·' % (database, search, 'name ASC', 'Name +') -print ' %s ·' % (database, search, 'name DESC', 'Name -') -print ' %s ·' % (database, search, 'popularity ASC', 'Popularity +') -print ' %s ·' % (database, search, 'popularity DESC', 'Popularity -') -print ' %s ·' % (database, search, 'added ASC', 'Date added +') -print ' %s ·' % (database, search, 'added DESC', 'Date added -') +print('
Order by
·'); +print(' %s ·' % (database, search, 'name ASC', 'Name +')) +print(' %s ·' % (database, search, 'name DESC', 'Name -')) +print(' %s ·' % (database, search, 'popularity ASC', 'Popularity +')) +print(' %s ·' % (database, search, 'popularity DESC', 'Popularity -')) +print(' %s ·' % (database, search, 'added ASC', 'Date added +')) +print(' %s ·' % (database, search, 'added DESC', 'Date added -')) -print ''' +print(''' @@ -148,7 +148,7 @@ print ''' - ''' + ''') url_re = re.compile('(?P(https?://\S+|www\S+))') def q(x): @@ -183,16 +183,16 @@ for fact in factoids: sys.stdout.write(' + print('''> - ''' % tuple(data) + ''' % tuple(data)) -print ''' +print(''' -
Author
%s %s %s
Added on: %s
Requested %s times
''' + ''') send_page('factoids.tmpl') diff --git a/Encyclopedia/plugin.py b/Encyclopedia/plugin.py index 90aafa7..cf168db 100644 --- a/Encyclopedia/plugin.py +++ b/Encyclopedia/plugin.py @@ -91,7 +91,7 @@ def queue(irc, to, msg): return now = time.time() - for m in msgcache.keys(): + for m in list(msgcache.keys()): if msgcache[m] < now - 30: msgcache.pop(m) for m in msgcache: @@ -215,7 +215,7 @@ class Encyclopedia(callbacks.Plugin): Lists all the users who are in the list of editors. """ - irc.reply(', '.join([u.name for u in ircdb.users.users.values() if capab(u.name, 'editfactoids')]), private=True) + irc.reply(', '.join([u.name for u in list(ircdb.users.users.values()) if capab(u.name, 'editfactoids')]), private=True) editors = wrap(editors) def moderators(self, irc, msg, args): @@ -223,7 +223,7 @@ class Encyclopedia(callbacks.Plugin): Lists all the users who can add users to the list of editors. """ - irc.reply(', '.join([u.name for u in ircdb.users.users.values() if capab(u.name, 'addeditors')]), private=True) + irc.reply(', '.join([u.name for u in list(ircdb.users.users.values()) if capab(u.name, 'addeditors')]), private=True) moderators = wrap(moderators) def get_target(self, nick, text, orig_target): @@ -399,7 +399,7 @@ class Encyclopedia(callbacks.Plugin): def check_aliases(self, channel, factoid): now = time.time() - for e in self.edits.keys(): + for e in list(self.edits.keys()): if self.edits[e] + 10 < now: self.edits.pop(e) if not factoid.value.startswith(''): @@ -459,7 +459,11 @@ class Encyclopedia(callbacks.Plugin): if checkIgnored(msg.prefix,msg.args[0]): return # Are we being queried? - recipient, text = msg.args[0], msg.args[1].decode('utf-8') + recipient = msg.args[0] + if sys.version_info < (3,0): + text = msg.args[1].decode('utf-8') + else: + text = msg.args[1] if not self.registryValue('enabled', ircutils.isChannel(recipient) and recipient or None): # Encyclopedia is disabled here, do nothing return @@ -554,7 +558,10 @@ class Encyclopedia(callbacks.Plugin): retmsg = '' ret = self.registryValue('notfoundmsg') if ret.count('%') == ret.count('%s') == 1: - ret = ret % repr(text).lstrip('u') + if sys.version_info < (3,0): + ret = ret % repr(text).lstrip('u') + else: + ret = ret % repr(text) if channel.lower() == irc.nick.lower() or self.registryValue('privateNotFound', channel): myqueue(irc, msg.nick, ret) else: @@ -572,7 +579,7 @@ class Encyclopedia(callbacks.Plugin): if doChanMsg and channel.lower() != irc.nick.lower() and target[0] != '#': # not /msg if target in irc.state.channels[channel].users: myqueue(irc, channel, "%s, please see my private message" % target) - if type(ret) != list: + if not isinstance(ret, list): myqueue(irc, target, retmsg + ret) else: myqueue(irc, target, retmsg + ret[0]) @@ -855,7 +862,7 @@ class Encyclopedia(callbacks.Plugin): ret[r] = 1 if not ret: return "None found" - return 'Found: %s' % ', '.join(sorted(ret.keys(), lambda x, y: cmp(ret[x], ret[y]))[:10]) + return 'Found: %s' % ', '.join(sorted(list(ret.keys()), key=lambda x: ret[x])[:10]) def sync(self, irc, msg, args, channel): """[] @@ -876,16 +883,14 @@ class Encyclopedia(callbacks.Plugin): return def download_database(location, dpath): """Download the database located at location to path dpath""" - import urllib2 tmp_db = "%s%stmp" % (dpath, os.extsep) - fd = urllib2.urlopen(location) - fd2 = open(tmp_db, 'w') - fd2.write(fd.read()) # Download to a temparary file + data = utils.web.getUrl(location) + fd = open(tmp_db, 'w') + fd.write(data) # Download to a temporary file fd.close() - fd2.close() # Do some checking to make sure we have an SQLite database - fd2 = open(tmp_db, 'rb') - check = fd2.read(15) + fd = open(tmp_db, 'rb') + check = fd.read(15) if check == 'SQLite format 3': # OK, rename to dpath os.rename(tmp_db, dpath) try: @@ -896,9 +901,9 @@ class Encyclopedia(callbacks.Plugin): self.databases.pop(channel) except: pass - else: # Remove the tmpparary file and raise an error + else: # Remove the temporary file and raise an error os.remove(tmp_db) - raise RuntimeError, "Downloaded file was not an SQLite 3 database" + raise RuntimeError("Downloaded file was not an SQLite 3 database") db = self.registryValue('database', channel) if not db: @@ -920,7 +925,7 @@ class Encyclopedia(callbacks.Plugin): except OSError: # file doesn't exist yet, so nothing to backup pass - except Exception, e: + except Exception as e: self.log.error("Encyclopedia: Could not rename %s to %s.backup" % (dbpath, dbpath)) self.log.error('Encyclopedia: ' + utils.exnToString(e)) irc.error("Internal error, see log") @@ -931,7 +936,7 @@ class Encyclopedia(callbacks.Plugin): irc.reply("Attemting to download database", prefixNick=False) download_database(remotedb, dbpath) irc.replySuccess() - except Exception, e: + except Exception as e: self.log.error("Encyclopedia: Could not download %s to %s" % (remotedb, dbpath)) self.log.error('Encyclopedia: ' + utils.exnToString(e)) irc.error("Internal error, see log") @@ -999,11 +1004,11 @@ class Encyclopedia(callbacks.Plugin): if not ret: rmsg = "Authored: None found" else: - rmsg = 'Authored Found: %s' % ', '.join(sorted(ret.keys(), lambda x, y: cmp(ret[x], ret[y]))[:10]) + rmsg = 'Authored Found: %s' % ', '.join(sorted(list(ret.keys()), key=lambda x: ret[x])[:10]) if not log_ret: log_rmsg = "Edited: None found" else: - log_rmsg = 'Edited Found: %s' % ', '.join(sorted(log_ret.keys(), lambda x, y: cmp(log_ret[x], log_ret[y]))[:10]) + log_rmsg = 'Edited Found: %s' % ', '.join(sorted(list(log_ret.keys()), key=lambda x: log_ret[x])[:10]) irc.reply(rmsg) irc.reply(log_rmsg) lookup = wrap(lookup, [optional('otherUser')]) @@ -1106,10 +1111,10 @@ class Encyclopedia(callbacks.Plugin): irc.reply("I'm not currently ignoring any hostmasks in '%s'" % channel) else: L = sorted(c.ignores) - irc.reply(utils.str.commaAndify(map(repr, L))) + irc.reply(utils.str.commaAndify(list(map(repr, L)))) else: if ircdb.ignores.hostmasks: - irc.reply(format('%L', (map(repr,ircdb.ignores.hostmasks)))) + irc.reply(format('%L', (list(map(repr,ircdb.ignores.hostmasks))))) else: irc.reply("I'm not currently globally ignoring anyone.") diff --git a/commoncgi.py b/commoncgi.py index 9d9f261..ebb6ab8 100644 --- a/commoncgi.py +++ b/commoncgi.py @@ -2,6 +2,7 @@ ### # Copyright (c) 2006-2007 Dennis Kaarsemaker # Copyright (c) 2008-2010 Terence Simpson +# Copyright (c) 2018- Krytarik Raido # # This program is free software; you can redistribute it and/or modify # it under the terms of version 2 of the GNU General Public License as @@ -14,19 +15,24 @@ # ### -import cgi, cgitb, re, sys, math, os, hashlib, random, time, datetime, pytz, Cookie, StringIO, urllib2 -import cPickle as pickle -cgitb.enable() +import cgi, cgitb, sys, os, re, math +import supybot.utils as utils +if sys.version_info < (3,0): + import Cookie as cookies +else: + import http.cookies as cookies + +cgitb.enable() form = cgi.FieldStorage() -cookie = Cookie.SimpleCookie() -if os.environ.has_key('HTTP_COOKIE'): +cookie = cookies.SimpleCookie() +if 'HTTP_COOKIE' in os.environ: cookie.load(os.environ['HTTP_COOKIE']) -if cookie.has_key('sess'): +if 'sess' in cookie: cookie['sess']['max-age'] = 2592000 * 3 cookie['sess']['version'] = 1 -if cookie.has_key('tz'): +if 'tz' in cookie: cookie['tz']['max-age'] = 2592000 * 3 cookie['tz']['version'] = 1 @@ -48,9 +54,9 @@ def send_page(template): errdata = sys.stderr.getvalue() sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ - print "Content-Type: text/html" - print cookie - print "" + print("Content-Type: text/html") + print(cookie) + print("") fd = open(template) tmpl = fd.read()