Skip to content

Commit

Permalink
[core, modules] Python 3 support!
Browse files Browse the repository at this point in the history
Issue sopel-irc#236

I'm happy to announce that with this commit, all basic Willie functionality
can be ran on Python 3!

There are few bugs remaining to iron out and few modules which fail to load
and need to be fixed, but all the basic functionality now works well with
Python3.

No extra dependencies introduced, Python 2.7 is obviously still supported.
  • Loading branch information
Elad Alfassa committed Feb 22, 2014
1 parent 6aa49ad commit b91eca7
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 18 deletions.
7 changes: 2 additions & 5 deletions help.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
http://willie.dftba.net
"""
from willie.module import commands, rule, example, priority
from willie.tools import iterkeys


@rule('$nick' '(?i)(help|doc) +([A-Za-z]+)(?:\?+)?$')
Expand All @@ -32,7 +33,7 @@ def help(bot, trigger):
@priority('low')
def commands(bot, trigger):
"""Return a list of bot's commands"""
names = ', '.join(sorted(bot.doc.iterkeys()))
names = ', '.join(sorted(iterkeys(bot.doc)))
if not trigger.is_privmsg:
bot.reply("I am sending you a private message of all my commands!")
bot.msg(trigger.nick, 'Commands I recognise: ' + names + '.', max_messages=10)
Expand All @@ -49,7 +50,3 @@ def help2(bot, trigger):
'general details. My owner is %s.'
) % bot.config.owner
bot.reply(response)


if __name__ == '__main__':
print __doc__.strip()
7 changes: 2 additions & 5 deletions reload.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os.path
import time
import imp
from willie.tools import iteritems
import willie.module
import subprocess

Expand Down Expand Up @@ -38,7 +39,7 @@ def f_reload(bot, trigger):
old_module = sys.modules[name]

old_callables = {}
for obj_name, obj in vars(old_module).iteritems():
for obj_name, obj in iteritems(vars(old_module)):
if bot.is_callable(obj) or bot.is_shutdown(obj):
old_callables[obj_name] = obj

Expand Down Expand Up @@ -151,7 +152,3 @@ def pm_f_load(bot, trigger):
"""Wrapper for allowing delivery of .load command via PM"""
if trigger.is_privmsg:
f_load(bot, trigger)


if __name__ == '__main__':
print __doc__.strip()
2 changes: 1 addition & 1 deletion remind.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def load_database(name):

def dump_database(name, data):
f = codecs.open(name, 'w', encoding='utf-8')
for unixtime, reminders in data.iteritems():
for unixtime, reminders in willie.tools.iteritems(data):
for channel, nick, message in reminders:
f.write('%s\t%s\t%s\t%s\n' % (unixtime, channel, nick, message))
f.close()
Expand Down
4 changes: 2 additions & 2 deletions tell.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import datetime
import willie.tools
import threading
from willie.tools import Nick
from willie.tools import Nick, iterkeys
from willie.module import commands, nickname_commands, rule, priority, example

maximum = 4
Expand Down Expand Up @@ -42,7 +42,7 @@ def dumpReminders(fn, data, lock):
lock.acquire()
try:
f = open(fn, 'w')
for tellee in data.iterkeys():
for tellee in iterkeys(data):
for remindon in data[tellee]:
line = '\t'.join((tellee,) + remindon)
try:
Expand Down
2 changes: 1 addition & 1 deletion translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def translate(text, input='auto', output='en'):
return ''.join(x[0] for x in data[0]), language


@rule(ur'$nickname[,:]\s+(?:([a-z]{2}) +)?(?:([a-z]{2}|en-raw) +)?["“](.+?)["”]\? *$')
@rule(u'$nickname[,:]\s+(?:([a-z]{2}) +)?(?:([a-z]{2}|en-raw) +)?["“](.+?)["”]\? *$')
@example('$nickname: "mon chien"? or $nickname: fr "mon chien"?')
@priority('low')
def tr(bot, trigger):
Expand Down
13 changes: 9 additions & 4 deletions url.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
"""

import re
from htmlentitydefs import name2codepoint
import sys
if sys.version_info.major < 3:
from htmlentitydefs import name2codepoint
import urlparse
else:
from html.entities import name2codepoint
import urllib.parse as urlparse
from willie import web, tools
from willie.module import commands, rule, example
import urlparse


url_finder = None
exclusion_char = '!'
Expand Down Expand Up @@ -117,7 +123,6 @@ def title_auto(bot, trigger):
"""
if re.match(bot.config.core.prefix + 'title', trigger):
return

urls = re.findall(url_finder, trigger)
results = process_urls(bot, trigger, urls)
bot.memory['last_seen_url'][trigger.sender] = urls[-1]
Expand Down Expand Up @@ -188,7 +193,7 @@ def check_callbacks(bot, trigger, url, run=True):
# Check if it matches the exclusion list first
matched = any(regex.search(url) for regex in bot.memory['url_exclude'])
# Then, check if there's anything in the callback list
for regex, function in bot.memory['url_callbacks'].iteritems():
for regex, function in tools.iteritems(bot.memory['url_callbacks']):
match = regex.search(url)
if match:
if run:
Expand Down

0 comments on commit b91eca7

Please sign in to comment.