Encyclopedia: Add Python 3 support.
This commit is contained in:
@ -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"""
|
||||
|
@ -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
|
||||
|
@ -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 '<a href="factoids.cgi?db=%s&search=%s&order=%s&page=%s">%d</a> ·' % (database, search, order_by, i, i+1)
|
||||
print('<a href="factoids.cgi?db=%s&search=%s&order=%s&page=%s">%d</a> ·' % (database, search, order_by, i, i+1))
|
||||
|
||||
print '<br />Order by<br />·';
|
||||
print ' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> ·' % (database, search, 'name ASC', 'Name +')
|
||||
print ' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> ·' % (database, search, 'name DESC', 'Name -')
|
||||
print ' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> ·' % (database, search, 'popularity ASC', 'Popularity +')
|
||||
print ' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> ·' % (database, search, 'popularity DESC', 'Popularity -')
|
||||
print ' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> ·' % (database, search, 'added ASC', 'Date added +')
|
||||
print ' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> ·' % (database, search, 'added DESC', 'Date added -')
|
||||
print('<br />Order by<br />·');
|
||||
print(' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> ·' % (database, search, 'name ASC', 'Name +'))
|
||||
print(' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> ·' % (database, search, 'name DESC', 'Name -'))
|
||||
print(' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> ·' % (database, search, 'popularity ASC', 'Popularity +'))
|
||||
print(' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> ·' % (database, search, 'popularity DESC', 'Popularity -'))
|
||||
print(' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> ·' % (database, search, 'added ASC', 'Date added +'))
|
||||
print(' <a href="factoids.cgi?db=%s&search=%s&order=%s&page=0">%s</a> ·' % (database, search, 'added DESC', 'Date added -'))
|
||||
|
||||
print '''
|
||||
print('''
|
||||
<table cellspacing="0">
|
||||
<thead>
|
||||
<tr>
|
||||
@ -148,7 +148,7 @@ print '''
|
||||
<th style="width: 20%;">Author</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>'''
|
||||
<tbody>''')
|
||||
|
||||
url_re = re.compile('(?P<url>(https?://\S+|www\S+))')
|
||||
def q(x):
|
||||
@ -183,16 +183,16 @@ for fact in factoids:
|
||||
sys.stdout.write(' <tr')
|
||||
if i % 2: sys.stdout.write(' class="bg2"')
|
||||
i += 1
|
||||
print '''>
|
||||
print('''>
|
||||
<td>%s</td>
|
||||
<td>%s</td>
|
||||
<td>%s<br />
|
||||
Added on: %s<br />
|
||||
Requested %s times</td>
|
||||
</tr>''' % tuple(data)
|
||||
</tr>''' % tuple(data))
|
||||
|
||||
print '''
|
||||
print('''
|
||||
</tbody>
|
||||
</table>'''
|
||||
</table>''')
|
||||
|
||||
send_page('factoids.tmpl')
|
||||
|
@ -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('<alias>'):
|
||||
@ -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):
|
||||
"""[<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.")
|
||||
|
||||
|
26
commoncgi.py
26
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()
|
||||
|
Reference in New Issue
Block a user