Encyclopedia: Add Python 3 support.

This commit is contained in:
Krytarik Raido 2018-02-22 11:45:04 +01:00
parent b18cc37a15
commit 669293de1e
5 changed files with 68 additions and 56 deletions

View File

@ -14,10 +14,10 @@
### ###
import sys import sys
import time import time, datetime, pytz
import urllib
import ConfigParser import ConfigParser
import sqlite import sqlite
import cPickle as pickle
CONFIG_FILENAME = "bantracker.conf" CONFIG_FILENAME = "bantracker.conf"
config = ConfigParser.RawConfigParser() config = ConfigParser.RawConfigParser()
@ -110,7 +110,7 @@ def urlencode(**kwargs):
"""Return the url options as a string, inserting additional ones if given.""" """Return the url options as a string, inserting additional ones if given."""
d = dict([ (i.name, i.value) for i in form.list ]) d = dict([ (i.name, i.value) for i in form.list ])
d.update(kwargs) d.update(kwargs)
return urllib.urlencode(d.items()) return utils.web.urlencode(d.items())
def isTrue(value): def isTrue(value):
"""Returns True if the form value is one of "1", "true", "yes", or "on", case insensitive""" """Returns True if the form value is one of "1", "true", "yes", or "on", case insensitive"""

View File

@ -22,8 +22,9 @@ funtionality has been moved to PackageInfo
import supybot import supybot
import supybot.world as world import supybot.world as world
from imp import reload
__version__ = "2.4" __version__ = "2.5"
__author__ = supybot.Author("Krytarik Raido", "krytarik", "krytarik@tuxgarage.com") __author__ = supybot.Author("Krytarik Raido", "krytarik", "krytarik@tuxgarage.com")
__contributors__ = { __contributors__ = {
supybot.Author("Dennis Kaarsemaker", "Seveas", "dennis@kaarsemaker.net"): ['Original Author'], supybot.Author("Dennis Kaarsemaker", "Seveas", "dennis@kaarsemaker.net"): ['Original Author'],
@ -31,13 +32,13 @@ __contributors__ = {
} }
__url__ = 'https://launchpad.net/ubuntu-bots' __url__ = 'https://launchpad.net/ubuntu-bots'
import config from . import config
reload(config) reload(config)
import plugin from . import plugin
reload(plugin) reload(plugin)
if world.testing: if world.testing:
import test from . import test
Class = plugin.Class Class = plugin.Class
configure = config.configure configure = config.configure

View File

@ -98,7 +98,7 @@ if 'search' in form:
# Select factoids # Select factoids
if search: 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 = [] values = []
if not keys: if not keys:
keys = [''] keys = ['']
@ -127,19 +127,19 @@ else:
# Pagination links # Pagination links
npages = int(math.ceil(total / float(NUM_PER_PAGE))) npages = int(math.ceil(total / float(NUM_PER_PAGE)))
print '·' print('·')
for i in range(npages): for i in range(npages):
print '<a href="factoids.cgi?db=%s&search=%s&order=%s&page=%s">%d</a> &middot;' % (database, search, order_by, i, i+1) print('<a href="factoids.cgi?db=%s&search=%s&order=%s&page=%s">%d</a> &middot;' % (database, search, order_by, i, i+1))
print '<br />Order by<br />&middot;'; print('<br />Order by<br />&middot;');
print ' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> &middot;' % (database, search, 'name ASC', 'Name +') print(' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> &middot;' % (database, search, 'name ASC', 'Name +'))
print ' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> &middot;' % (database, search, 'name DESC', 'Name -') print(' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> &middot;' % (database, search, 'name DESC', 'Name -'))
print ' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> &middot;' % (database, search, 'popularity ASC', 'Popularity +') print(' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> &middot;' % (database, search, 'popularity ASC', 'Popularity +'))
print ' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> &middot;' % (database, search, 'popularity DESC', 'Popularity -') print(' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> &middot;' % (database, search, 'popularity DESC', 'Popularity -'))
print ' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> &middot;' % (database, search, 'added ASC', 'Date added +') print(' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> &middot;' % (database, search, 'added ASC', 'Date added +'))
print ' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> &middot;' % (database, search, 'added DESC', 'Date added -') print(' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> &middot;' % (database, search, 'added DESC', 'Date added -'))
print ''' print('''
<table cellspacing="0"> <table cellspacing="0">
<thead> <thead>
<tr> <tr>
@ -148,7 +148,7 @@ print '''
<th style="width: 20%;">Author</th> <th style="width: 20%;">Author</th>
</tr> </tr>
</thead> </thead>
<tbody>''' <tbody>''')
url_re = re.compile('(?P<url>(https?://\S+|www\S+))') url_re = re.compile('(?P<url>(https?://\S+|www\S+))')
def q(x): def q(x):
@ -183,16 +183,16 @@ for fact in factoids:
sys.stdout.write(' <tr') sys.stdout.write(' <tr')
if i % 2: sys.stdout.write(' class="bg2"') if i % 2: sys.stdout.write(' class="bg2"')
i += 1 i += 1
print '''> print('''>
<td>%s</td> <td>%s</td>
<td>%s</td> <td>%s</td>
<td>%s<br /> <td>%s<br />
Added on: %s<br /> Added on: %s<br />
Requested %s times</td> Requested %s times</td>
</tr>''' % tuple(data) </tr>''' % tuple(data))
print ''' print('''
</tbody> </tbody>
</table>''' </table>''')
send_page('factoids.tmpl') send_page('factoids.tmpl')

View File

@ -91,7 +91,7 @@ def queue(irc, to, msg):
return return
now = time.time() now = time.time()
for m in msgcache.keys(): for m in list(msgcache.keys()):
if msgcache[m] < now - 30: if msgcache[m] < now - 30:
msgcache.pop(m) msgcache.pop(m)
for m in msgcache: for m in msgcache:
@ -215,7 +215,7 @@ class Encyclopedia(callbacks.Plugin):
Lists all the users who are in the list of editors. 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) editors = wrap(editors)
def moderators(self, irc, msg, args): 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. 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) moderators = wrap(moderators)
def get_target(self, nick, text, orig_target): def get_target(self, nick, text, orig_target):
@ -399,7 +399,7 @@ class Encyclopedia(callbacks.Plugin):
def check_aliases(self, channel, factoid): def check_aliases(self, channel, factoid):
now = time.time() now = time.time()
for e in self.edits.keys(): for e in list(self.edits.keys()):
if self.edits[e] + 10 < now: if self.edits[e] + 10 < now:
self.edits.pop(e) self.edits.pop(e)
if not factoid.value.startswith('<alias>'): if not factoid.value.startswith('<alias>'):
@ -459,7 +459,11 @@ class Encyclopedia(callbacks.Plugin):
if checkIgnored(msg.prefix,msg.args[0]): if checkIgnored(msg.prefix,msg.args[0]):
return return
# Are we being queried? # 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): if not self.registryValue('enabled', ircutils.isChannel(recipient) and recipient or None):
# Encyclopedia is disabled here, do nothing # Encyclopedia is disabled here, do nothing
return return
@ -554,7 +558,10 @@ class Encyclopedia(callbacks.Plugin):
retmsg = '' retmsg = ''
ret = self.registryValue('notfoundmsg') ret = self.registryValue('notfoundmsg')
if ret.count('%') == ret.count('%s') == 1: 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): if channel.lower() == irc.nick.lower() or self.registryValue('privateNotFound', channel):
myqueue(irc, msg.nick, ret) myqueue(irc, msg.nick, ret)
else: else:
@ -572,7 +579,7 @@ class Encyclopedia(callbacks.Plugin):
if doChanMsg and channel.lower() != irc.nick.lower() and target[0] != '#': # not /msg if doChanMsg and channel.lower() != irc.nick.lower() and target[0] != '#': # not /msg
if target in irc.state.channels[channel].users: if target in irc.state.channels[channel].users:
myqueue(irc, channel, "%s, please see my private message" % target) myqueue(irc, channel, "%s, please see my private message" % target)
if type(ret) != list: if not isinstance(ret, list):
myqueue(irc, target, retmsg + ret) myqueue(irc, target, retmsg + ret)
else: else:
myqueue(irc, target, retmsg + ret[0]) myqueue(irc, target, retmsg + ret[0])
@ -855,7 +862,7 @@ class Encyclopedia(callbacks.Plugin):
ret[r] = 1 ret[r] = 1
if not ret: if not ret:
return "None found" 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): def sync(self, irc, msg, args, channel):
"""[<channel>] """[<channel>]
@ -876,16 +883,14 @@ class Encyclopedia(callbacks.Plugin):
return return
def download_database(location, dpath): def download_database(location, dpath):
"""Download the database located at location to path dpath""" """Download the database located at location to path dpath"""
import urllib2
tmp_db = "%s%stmp" % (dpath, os.extsep) tmp_db = "%s%stmp" % (dpath, os.extsep)
fd = urllib2.urlopen(location) data = utils.web.getUrl(location)
fd2 = open(tmp_db, 'w') fd = open(tmp_db, 'w')
fd2.write(fd.read()) # Download to a temparary file fd.write(data) # Download to a temporary file
fd.close() fd.close()
fd2.close()
# Do some checking to make sure we have an SQLite database # Do some checking to make sure we have an SQLite database
fd2 = open(tmp_db, 'rb') fd = open(tmp_db, 'rb')
check = fd2.read(15) check = fd.read(15)
if check == 'SQLite format 3': # OK, rename to dpath if check == 'SQLite format 3': # OK, rename to dpath
os.rename(tmp_db, dpath) os.rename(tmp_db, dpath)
try: try:
@ -896,9 +901,9 @@ class Encyclopedia(callbacks.Plugin):
self.databases.pop(channel) self.databases.pop(channel)
except: except:
pass pass
else: # Remove the tmpparary file and raise an error else: # Remove the temporary file and raise an error
os.remove(tmp_db) 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) db = self.registryValue('database', channel)
if not db: if not db:
@ -920,7 +925,7 @@ class Encyclopedia(callbacks.Plugin):
except OSError: except OSError:
# file doesn't exist yet, so nothing to backup # file doesn't exist yet, so nothing to backup
pass 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: Could not rename %s to %s.backup" % (dbpath, dbpath))
self.log.error('Encyclopedia: ' + utils.exnToString(e)) self.log.error('Encyclopedia: ' + utils.exnToString(e))
irc.error("Internal error, see log") irc.error("Internal error, see log")
@ -931,7 +936,7 @@ class Encyclopedia(callbacks.Plugin):
irc.reply("Attemting to download database", prefixNick=False) irc.reply("Attemting to download database", prefixNick=False)
download_database(remotedb, dbpath) download_database(remotedb, dbpath)
irc.replySuccess() 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: Could not download %s to %s" % (remotedb, dbpath))
self.log.error('Encyclopedia: ' + utils.exnToString(e)) self.log.error('Encyclopedia: ' + utils.exnToString(e))
irc.error("Internal error, see log") irc.error("Internal error, see log")
@ -999,11 +1004,11 @@ class Encyclopedia(callbacks.Plugin):
if not ret: if not ret:
rmsg = "Authored: None found" rmsg = "Authored: None found"
else: 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: if not log_ret:
log_rmsg = "Edited: None found" log_rmsg = "Edited: None found"
else: 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(rmsg)
irc.reply(log_rmsg) irc.reply(log_rmsg)
lookup = wrap(lookup, [optional('otherUser')]) 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) irc.reply("I'm not currently ignoring any hostmasks in '%s'" % channel)
else: else:
L = sorted(c.ignores) L = sorted(c.ignores)
irc.reply(utils.str.commaAndify(map(repr, L))) irc.reply(utils.str.commaAndify(list(map(repr, L))))
else: else:
if ircdb.ignores.hostmasks: if ircdb.ignores.hostmasks:
irc.reply(format('%L', (map(repr,ircdb.ignores.hostmasks)))) irc.reply(format('%L', (list(map(repr,ircdb.ignores.hostmasks)))))
else: else:
irc.reply("I'm not currently globally ignoring anyone.") irc.reply("I'm not currently globally ignoring anyone.")

View File

@ -2,6 +2,7 @@
### ###
# Copyright (c) 2006-2007 Dennis Kaarsemaker # Copyright (c) 2006-2007 Dennis Kaarsemaker
# Copyright (c) 2008-2010 Terence Simpson # Copyright (c) 2008-2010 Terence Simpson
# Copyright (c) 2018- Krytarik Raido
# #
# This program is free software; you can redistribute it and/or modify # 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 # 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 cgi, cgitb, sys, os, re, math
import cPickle as pickle import supybot.utils as utils
cgitb.enable()
if sys.version_info < (3,0):
import Cookie as cookies
else:
import http.cookies as cookies
cgitb.enable()
form = cgi.FieldStorage() form = cgi.FieldStorage()
cookie = Cookie.SimpleCookie() cookie = cookies.SimpleCookie()
if os.environ.has_key('HTTP_COOKIE'): if 'HTTP_COOKIE' in os.environ:
cookie.load(os.environ['HTTP_COOKIE']) cookie.load(os.environ['HTTP_COOKIE'])
if cookie.has_key('sess'): if 'sess' in cookie:
cookie['sess']['max-age'] = 2592000 * 3 cookie['sess']['max-age'] = 2592000 * 3
cookie['sess']['version'] = 1 cookie['sess']['version'] = 1
if cookie.has_key('tz'): if 'tz' in cookie:
cookie['tz']['max-age'] = 2592000 * 3 cookie['tz']['max-age'] = 2592000 * 3
cookie['tz']['version'] = 1 cookie['tz']['version'] = 1
@ -48,9 +54,9 @@ def send_page(template):
errdata = sys.stderr.getvalue() errdata = sys.stderr.getvalue()
sys.stdout = sys.__stdout__ sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__ sys.stderr = sys.__stderr__
print "Content-Type: text/html" print("Content-Type: text/html")
print cookie print(cookie)
print "" print("")
fd = open(template) fd = open(template)
tmpl = fd.read() tmpl = fd.read()