From b91eca7599f448bcbd3ee0a25284dd5d7e8bbbe7 Mon Sep 17 00:00:00 2001 From: Elad Alfassa Date: Sat, 22 Feb 2014 18:36:10 +0200 Subject: [PATCH] [core, modules] Python 3 support! Issue #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. --- help.py | 7 ++----- reload.py | 7 ++----- remind.py | 2 +- tell.py | 4 ++-- translate.py | 2 +- url.py | 13 +++++++++---- 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/help.py b/help.py index b8e1baca27..6b6c741002 100644 --- a/help.py +++ b/help.py @@ -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]+)(?:\?+)?$') @@ -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) @@ -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() diff --git a/reload.py b/reload.py index 93ead4f4ad..9da03c9a27 100644 --- a/reload.py +++ b/reload.py @@ -10,6 +10,7 @@ import os.path import time import imp +from willie.tools import iteritems import willie.module import subprocess @@ -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 @@ -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() diff --git a/remind.py b/remind.py index ea6738867e..78b0a325d9 100644 --- a/remind.py +++ b/remind.py @@ -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() diff --git a/tell.py b/tell.py index 9aa8f6c12e..630fd67bce 100644 --- a/tell.py +++ b/tell.py @@ -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 @@ -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: diff --git a/translate.py b/translate.py index 1caabac1fd..c3c0d1ca3e 100644 --- a/translate.py +++ b/translate.py @@ -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): diff --git a/url.py b/url.py index cfe4140de8..4391db3876 100644 --- a/url.py +++ b/url.py @@ -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 = '!' @@ -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] @@ -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: