From d99c07fdccde781e4bb65864dc6cf3a83bb64d9a Mon Sep 17 00:00:00 2001 From: Edward Powell Date: Sat, 22 Jun 2013 16:28:34 -0400 Subject: [PATCH] [ping, isup, rand, tld, wiktionary] Update to 4.0 API Also clean up a bit, and add an example or two Issues #125 and #276 --- willie/modules/isup.py | 6 ++++-- willie/modules/ping.py | 34 ++++++++++++++++++---------------- willie/modules/rand.py | 16 +++++++--------- willie/modules/tld.py | 14 +++++++------- willie/modules/wiktionary.py | 16 +++++++++------- 5 files changed, 45 insertions(+), 41 deletions(-) diff --git a/willie/modules/isup.py b/willie/modules/isup.py index b08dacf5d8..0d51378bee 100644 --- a/willie/modules/isup.py +++ b/willie/modules/isup.py @@ -6,9 +6,12 @@ This allows users to check if a website is up through isup.me. """ -import willie.web as web +from willie import web +from willie.module import commands import re + +@commands('isup') def isup(willie, trigger): """isup.me website status checker""" site = trigger.group(2) @@ -31,4 +34,3 @@ def isup(willie, trigger): willie.say(site + ' looks fine to me.') else: willie.say(site + ' is down from here.') -isup.commands = ['isup'] diff --git a/willie/modules/ping.py b/willie/modules/ping.py index 68e53efeb8..20c1a71702 100644 --- a/willie/modules/ping.py +++ b/willie/modules/ping.py @@ -5,24 +5,26 @@ """ import random +from willie.module import rule, priority, thread -def hello(willie, trigger): - if trigger.owner: - greeting = random.choice(('Fuck off,', 'Screw you,', 'Go away')) - else: greeting = random.choice(('Hi', 'Hey', 'Hello')) - punctuation = random.choice(('', '!')) - willie.say(greeting + ' ' + trigger.nick + punctuation) -hello.rule = r'(?i)(hi|hello|hey) $nickname[ \t]*$' +@rule(r'(?i)(hi|hello|hey) $nickname[ \t]*$') +def hello(willie, trigger): + if trigger.owner: + greeting = random.choice(('Fuck off,', 'Screw you,', 'Go away')) + else: + greeting = random.choice(('Hi', 'Hey', 'Hello')) + punctuation = random.choice(('', '!')) + willie.say(greeting + ' ' + trigger.nick + punctuation) + + +@rule(r'(?i)(Fuck|Screw) you, $nickname[ \t]*$') def rude(willie, trigger): - willie.say('Watch your mouth, ' + trigger.nick + ', or I\'ll tell your mother!') -rude.rule = r'(?i)(Fuck|Screw) you, $nickname[ \t]*$' + willie.say('Watch your mouth, ' + trigger.nick + ', or I\'ll tell your mother!') -def interjection(willie, trigger): - willie.say(trigger.nick + '!') -interjection.rule = r'$nickname!' -interjection.priority = 'high' -interjection.thread = False -if __name__ == '__main__': - print __doc__.strip() +@rule('$nickname!') +@priority('high') +@thread(False) +def interjection(willie, trigger): + willie.say(trigger.nick + '!') diff --git a/willie/modules/rand.py b/willie/modules/rand.py index c09340b719..10a0a4c0d1 100644 --- a/willie/modules/rand.py +++ b/willie/modules/rand.py @@ -6,12 +6,16 @@ http://willie.dftba.net """ +from willie.module import commands, example import random import re + +@commands('rand') +@example('.rand 1 100') def rand(willie, trigger): - """.rand - Generates a random integer between and .""" - if trigger.group(2) == " " or trigger.group(2) == "" or str(trigger.group(2)) == None or str(trigger.group(2)) == "" or trigger.group(2) == None: + """Generates a random integer between and .""" + if not trigger.group(2): willie.say("I'm sorry, " + str(trigger.nick) + ", but you must enter at least one number.") else: random.seed() @@ -29,7 +33,7 @@ def rand(willie, trigger): randinte = random.randint(0, a) willie.say(str(trigger.nick) + ": your random integer is: " + str(randinte)) else: - a,b = li_integers.split() + a, b = li_integers.split() a = re.sub(r'\D', '', str(a)) b = re.sub(r'\D', '', str(b)) a = int(a) @@ -39,9 +43,3 @@ def rand(willie, trigger): else: randinte = random.randint(b, a) willie.say(str(trigger.nick) + ": your random integer is: " + str(randinte)) - -rand.commands = ['rand'] -rand.priority = 'medium' - -if __name__ == '__main__': - print __doc__.strip() diff --git a/willie/modules/tld.py b/willie/modules/tld.py index e7337c08a1..e13134f1de 100644 --- a/willie/modules/tld.py +++ b/willie/modules/tld.py @@ -6,12 +6,17 @@ http://willie.dftba.net """ -import re, urllib2 -import willie.web as web +from willie import web +from willie.module import commands, example +import re +import urllib2 uri = 'https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains' r_tag = re.compile(r'<(?!!)[^>]+>') + +@commands('tld') +@example('.tld ru') def gettld(willie, trigger): """Show information about the given Top Level Domain.""" page = web.get(uri) @@ -55,8 +60,3 @@ def gettld(willie, trigger): else: reply = "No matches found for TLD: {0}".format(unicode(trigger.group(2))) willie.reply(reply) -gettld.commands = ['tld'] -gettld.thread = False - -if __name__ == '__main__': - print __doc__.strip() diff --git a/willie/modules/wiktionary.py b/willie/modules/wiktionary.py index 8a1edc1a77..24fbd485a1 100644 --- a/willie/modules/wiktionary.py +++ b/willie/modules/wiktionary.py @@ -7,12 +7,14 @@ """ import re -import willie.web as web +from willie import web +from willie.module import commands, example uri = 'http://en.wiktionary.org/w/index.php?title=%s&printable=yes' r_tag = re.compile(r'<[^>]+>') r_ul = re.compile(r'(?ims)
    .*?
') + def text(html): text = r_tag.sub('', html).strip() text = text.replace('\n', ' ') @@ -21,6 +23,7 @@ def text(html): text = text.replace('(transitive', '(trans.') return text + def wikt(word): bytes = web.get(uri % web.quote(word.encode('utf-8'))) bytes = r_ul.sub('', bytes) @@ -60,16 +63,20 @@ def wikt(word): parts = ('preposition', 'particle', 'noun', 'verb', 'adjective', 'adverb', 'interjection') + def format(word, definitions, number=2): result = '%s' % word.encode('utf-8') for part in parts: - if definitions.has_key(part): + if part in definitions: defs = definitions[part][:number] result += u' \u2014 '.encode('utf-8') + ('%s: ' % part) n = ['%s. %s' % (i + 1, e.strip(' .')) for i, e in enumerate(defs)] result += ', '.join(n) return result.strip(' .,') + +@commands('wt', 'define', 'dict') +@example('.wt bailiwick') def wiktionary(willie, trigger): """Look up a word on Wiktionary.""" word = trigger.group(2) @@ -91,8 +98,3 @@ def wiktionary(willie, trigger): if len(result) > 300: result = result[:295] + '[...]' willie.say(result.decode('utf8')) -wiktionary.commands = ['wt', 'define', 'dict'] -wiktionary.example = '.wt bailiwick' - -if __name__ == '__main__': - print __doc__.strip()