PEP8 compliance (only whitespace changes)

This commit is contained in:
Ryan Hitchman 2009-04-17 18:57:18 -06:00
parent 94d9fd22f0
commit 5ea26b8ff7
20 changed files with 122 additions and 63 deletions

7
bot.py
View File

@ -20,7 +20,9 @@ import yaml
os.chdir(sys.path[0]) # do stuff relative to the installation directory os.chdir(sys.path[0]) # do stuff relative to the installation directory
class Bot(object): class Bot(object):
def __init__(self, nick, channel, network): def __init__(self, nick, channel, network):
self.nick = nick self.nick = nick
self.channel = channel self.channel = channel
@ -32,6 +34,7 @@ print 'Loading plugins'
plugin_mtimes = {} plugin_mtimes = {}
def reload_plugins(): def reload_plugins():
if not hasattr(bot, 'plugs'): if not hasattr(bot, 'plugs'):
@ -79,7 +82,9 @@ bot.persist_dir = os.path.abspath('persist')
print 'Running main loop' print 'Running main loop'
class Input(object): class Input(object):
def __init__(self, raw, prefix, command, def __init__(self, raw, prefix, command,
params, nick, user, host, paraml, msg): params, nick, user, host, paraml, msg):
self.raw = raw self.raw = raw
@ -99,7 +104,9 @@ class Input(object):
else: else:
self.chan = "" self.chan = ""
class FakeBot(object): class FakeBot(object):
def __init__(self, bot, input, func): def __init__(self, bot, input, func):
self.bot = bot self.bot = bot
self.persist_dir = bot.persist_dir self.persist_dir = bot.persist_dir

View File

@ -2,6 +2,7 @@ import re
import hook import hook
@hook.sieve @hook.sieve
def sieve_suite(bot, input, func, args): def sieve_suite(bot, input, func, args):
events = args.get('events', ['PRIVMSG']) events = args.get('events', ['PRIVMSG'])
@ -16,7 +17,8 @@ def sieve_suite(bot, input, func, args):
args.setdefault('prefix', True) args.setdefault('prefix', True)
if args.get('prefix', True): if args.get('prefix', True):
hook = (r'^(?:[.!]|' if input.chan != input.nick else r'^(?:[.!]?|') + bot.nick +r'[:,]*\s*)' + hook hook = (r'^(?:[.!]|' if input.chan != input.nick else r'^(?:[.!]?|') \
+ bot.nick +r'[:,]*\s*)' + hook
input.re = re.match(hook, input.msg, flags=re.I) input.re = re.match(hook, input.msg, flags=re.I)
if input.re is None: if input.re is None:

4
irc.py
View File

@ -8,6 +8,7 @@ import Queue
queue = Queue.Queue queue = Queue.Queue
def decode(txt): def decode(txt):
for codec in ('utf-8', 'iso-8859-1', 'shift_jis', 'cp1252'): for codec in ('utf-8', 'iso-8859-1', 'shift_jis', 'cp1252'):
try: try:
@ -16,8 +17,10 @@ def decode(txt):
continue continue
return txt.decode('utf-8', 'ignore') return txt.decode('utf-8', 'ignore')
class crlf_tcp(asynchat.async_chat): class crlf_tcp(asynchat.async_chat):
"Handles tcp connections that consist of utf-8 lines ending with crlf" "Handles tcp connections that consist of utf-8 lines ending with crlf"
def __init__(self, host, port): def __init__(self, host, port):
asynchat.async_chat.__init__(self) asynchat.async_chat.__init__(self)
self.set_terminator('\r\n') self.set_terminator('\r\n')
@ -56,6 +59,7 @@ irc_noprefix_re = re.compile(r'()(.*?) (.*)')
irc_param_re = re.compile(r'(?:^|(?<= ))(:.*|[^ ]+)') irc_param_re = re.compile(r'(?:^|(?<= ))(:.*|[^ ]+)')
irc_netmask_re = re.compile(r':?([^!@]*)!?([^@]*)@?(.*)') irc_netmask_re = re.compile(r':?([^!@]*)!?([^@]*)@?(.*)')
class irc(object): class irc(object):
"handles the IRC protocol" "handles the IRC protocol"
#see the docs/ folder for more information on the protocol #see the docs/ folder for more information on the protocol

View File

@ -4,8 +4,13 @@ import htmlentitydefs
import re import re
import hook import hook
########### from http://effbot.org/zone/re-sub.htm#unescape-html ############# ########### from http://effbot.org/zone/re-sub.htm#unescape-html #############
def unescape(text): def unescape(text):
def fixup(m): def fixup(m):
text = m.group(0) text = m.group(0)
if text[:2] == "&#": if text[:2] == "&#":
@ -24,12 +29,15 @@ def unescape(text):
except KeyError: except KeyError:
pass pass
return text # leave as is return text # leave as is
return re.sub("&#?\w+;", fixup, text) return re.sub("&#?\w+;", fixup, text)
############################################################################## ##############################################################################
languages = 'ja fr de ko ru zh'.split(); languages = 'ja fr de ko ru zh'.split()
language_pairs = zip(languages[:-1], languages[1:]) language_pairs = zip(languages[:-1], languages[1:])
def goog_trans(text, slang, tlang): def goog_trans(text, slang, tlang):
req_url = 'http://ajax.googleapis.com/ajax/services/language/translate' \ req_url = 'http://ajax.googleapis.com/ajax/services/language/translate' \
'?v=1.0&q=%s&langpair=%s' '?v=1.0&q=%s&langpair=%s'
@ -38,10 +46,11 @@ def goog_trans(text, slang, tlang):
parsed = yaml.load(json) parsed = yaml.load(json)
if not 200 <= parsed['responseStatus'] < 300: if not 200 <= parsed['responseStatus'] < 300:
print parsed print parsed
raise IOError, 'error with the translation server: %d: %s' % ( raise IOError('error with the translation server: %d: %s' % (
parsed['responseStatus'], '') parsed['responseStatus'], ''))
return unescape(parsed['responseData']['translatedText']) return unescape(parsed['responseData']['translatedText'])
def babel_gen(inp): def babel_gen(inp):
for language in languages: for language in languages:
inp = inp.encode('utf8') inp = inp.encode('utf8')
@ -50,6 +59,7 @@ def babel_gen(inp):
print language, trans, inp print language, trans, inp
yield language, trans, inp yield language, trans, inp
@hook.command @hook.command
def babel(inp): def babel(inp):
try: try:
@ -57,6 +67,7 @@ def babel(inp):
except IOError, e: except IOError, e:
return e return e
@hook.command @hook.command
def babelext(inp): def babelext(inp):
try: try:

View File

@ -9,6 +9,7 @@ import hook
BUFFER_SIZE = 5000 BUFFER_SIZE = 5000
MAX_STEPS = 1000000 MAX_STEPS = 1000000
@hook.command @hook.command
def bf(input): def bf(input):
"""Runs a Brainfuck program.""" """Runs a Brainfuck program."""
@ -37,7 +38,6 @@ def bf(input):
steps = 0 steps = 0
memory = [0] * BUFFER_SIZE #initial memory area memory = [0] * BUFFER_SIZE #initial memory area
rightmost = 0 rightmost = 0
output = "" #we'll save the output here output = "" #we'll save the output here
# the main program loop: # the main program loop:

View File

@ -2,6 +2,7 @@ import urllib
import hook import hook
@hook.command('god') @hook.command('god')
@hook.command @hook.command
def bible(inp): def bible(inp):

View File

@ -12,6 +12,7 @@ valid_diceroll_re = re.compile(r'^[+-]?(\d+|\d*d\d+)([+-](\d+|\d*d\d+))*$')
sign_re = re.compile(r'[+-]?(?:\d*d)?\d+') sign_re = re.compile(r'[+-]?(?:\d*d)?\d+')
split_re = re.compile(r'([\d+-]*)d?(\d*)') split_re = re.compile(r'([\d+-]*)d?(\d*)')
def nrolls(count, n): def nrolls(count, n):
"roll an n-sided die count times" "roll an n-sided die count times"
if n < 2: #it's a coin if n < 2: #it's a coin
@ -26,6 +27,7 @@ def nrolls(count, n):
return int(random.normalvariate(.5*(1+n)*count, return int(random.normalvariate(.5*(1+n)*count,
(((n+1)*(2*n+1)/6.-(.5*(1+n))**2)*count)**.5)) (((n+1)*(2*n+1)/6.-(.5*(1+n))**2)*count)**.5))
@hook.command @hook.command
def dice(input): def dice(input):
".dice <diceroll> - simulates dicerolls, e.g. .dice 2d20-d5+4 roll 2 " \ ".dice <diceroll> - simulates dicerolls, e.g. .dice 2d20-d5+4 roll 2 " \

View File

@ -1,5 +1,6 @@
import hook import hook
#@hook.command #@hook.command
def goonsay(bot, input): def goonsay(bot, input):
bot.say(' __________ /') bot.say(' __________ /')

View File

@ -2,14 +2,17 @@ import hashlib
import hook import hook
@hook.command @hook.command
def md5(input): def md5(input):
return hashlib.md5(input).hexdigest() return hashlib.md5(input).hexdigest()
@hook.command @hook.command
def sha1(input): def sha1(input):
return hashlib.sha1(input).hexdigest() return hashlib.sha1(input).hexdigest()
@hook.command @hook.command
def hash(input): def hash(input):
return ', '.join(x + ": " + getattr(hashlib, x)(input).hexdigest() return ', '.join(x + ": " + getattr(hashlib, x)(input).hexdigest()

View File

@ -3,27 +3,32 @@ def _isfunc(x):
return True return True
return False return False
def _hook_add(func, add): def _hook_add(func, add):
if not hasattr(func, '_skybot_hook'): if not hasattr(func, '_skybot_hook'):
func._skybot_hook = [] func._skybot_hook = []
func._skybot_hook.append(add) func._skybot_hook.append(add)
def _make_sig(f): def _make_sig(f):
return f.func_code.co_filename, f.func_name, f.func_code.co_firstlineno return f.func_code.co_filename, f.func_name, f.func_code.co_firstlineno
def sieve(func): def sieve(func):
if func.func_code.co_argcount != 4: if func.func_code.co_argcount != 4:
raise ValueError, \ raise ValueError(
'sieves must take 4 arguments: (bot, input, func, args)' 'sieves must take 4 arguments: (bot, input, func, args)')
_hook_add(func, ['sieve', (_make_sig(func), func)]) _hook_add(func, ['sieve', (_make_sig(func), func)])
return func return func
def command(func=None, hook=None, **kwargs): def command(func=None, hook=None, **kwargs):
args = {} args = {}
def command_wrapper(func): def command_wrapper(func):
if func.func_code.co_argcount not in (1, 2): if func.func_code.co_argcount not in (1, 2):
raise ValueError, \ raise ValueError(
'commands must take 1 or 2 arguments: (inp) or (bot, input)' 'commands must take 1 or 2 arguments: (inp) or (bot, input)')
args.setdefault('name', func.func_name) args.setdefault('name', func.func_name)
args.setdefault('hook', args['name'] + r'(?:\s+|$)(.*)') args.setdefault('hook', args['name'] + r'(?:\s+|$)(.*)')
_hook_add(func, ['command', (_make_sig(func), func, args)]) _hook_add(func, ['command', (_make_sig(func), func, args)])
@ -39,12 +44,13 @@ def command(func=None, hook=None, **kwargs):
else: else:
return command_wrapper(func) return command_wrapper(func)
def event(arg=None, **kwargs): def event(arg=None, **kwargs):
args = kwargs args = kwargs
def event_wrapper(func): def event_wrapper(func):
if func.func_code.co_argcount != 2: if func.func_code.co_argcount != 2:
raise ValueError, \ raise ValueError('events must take 2 arguments: (bot, input)')
'events must take 2 arguments: (bot, input)'
args['name'] = func.func_name args['name'] = func.func_name
args['prefix'] = False args['prefix'] = False
args.setdefault('events', '*') args.setdefault('events', '*')

View File

@ -6,6 +6,7 @@ posts everything buttbot says to the iambuttbot twitter account
import urllib import urllib
import hook import hook
@hook.command(hook=r'(.*)', prefix=False, ignorebots=False) @hook.command(hook=r'(.*)', prefix=False, ignorebots=False)
def iambuttbot(bot, input): def iambuttbot(bot, input):
if input.nick.lower() != 'buttbot': if input.nick.lower() != 'buttbot':

View File

@ -16,13 +16,16 @@ log_fds = {} # '%(net)s %(chan)s' : (filename, fd)
timestamp_format = '%H:%M:%S' timestamp_format = '%H:%M:%S'
def get_log_filename(dir, network, chan): def get_log_filename(dir, network, chan):
return os.path.join(dir, 'log', gmtime('%Y'), network, return os.path.join(dir, 'log', gmtime('%Y'), network,
gmtime('%%s.%m-%d.log') % chan).lower() gmtime('%%s.%m-%d.log') % chan).lower()
def gmtime(format): def gmtime(format):
return time.strftime(format, time.gmtime()) return time.strftime(format, time.gmtime())
def get_log_fd(dir, network, chan): def get_log_fd(dir, network, chan):
fn = get_log_filename(dir, network, chan) fn = get_log_filename(dir, network, chan)
cache_key = '%s %s' % (network, chan) cache_key = '%s %s' % (network, chan)
@ -40,6 +43,7 @@ def get_log_fd(dir, network, chan):
return fd return fd
@hook.event(ignorebots=False) @hook.event(ignorebots=False)
def log(bot, input): def log(bot, input):
".remember <word> <data> -- maps word to data in the memory" ".remember <word> <data> -- maps word to data in the memory"

View File

@ -1,5 +1,6 @@
import hook import hook
@hook.event('KICK INVITE') @hook.event('KICK INVITE')
def rejoin(bot, input): def rejoin(bot, input):
print input.command, input.inp print input.command, input.inp

View File

@ -5,6 +5,7 @@ import hook
re_lineends = re.compile(r'[\r\n]*') re_lineends = re.compile(r'[\r\n]*')
@hook.command @hook.command
def py(input): def py(input):
res = urllib.urlopen("http://eval.appspot.com/eval?statement=%s" % res = urllib.urlopen("http://eval.appspot.com/eval?statement=%s" %

View File

@ -13,6 +13,7 @@ import hook
lock = thread.allocate_lock() lock = thread.allocate_lock()
memory = {} memory = {}
def load_memory(filename, mtimes={}): def load_memory(filename, mtimes={}):
if not os.path.exists(filename): if not os.path.exists(filename):
return {} return {}
@ -22,15 +23,18 @@ def load_memory(filename, mtimes={}):
return dict((x.split(None, 1)[0].lower(), x.strip()) for x in return dict((x.split(None, 1)[0].lower(), x.strip()) for x in
codecs.open(filename, 'r', 'utf-8')) codecs.open(filename, 'r', 'utf-8'))
def save_memory(filename, memory): def save_memory(filename, memory):
out = codecs.open(filename, 'w', 'utf-8') out = codecs.open(filename, 'w', 'utf-8')
out.write('\n'.join(sorted(memory.itervalues()))) out.write('\n'.join(sorted(memory.itervalues())))
out.flush() out.flush()
out.close() out.close()
def make_filename(dir, chan): def make_filename(dir, chan):
return os.path.join(dir, 'memory') return os.path.join(dir, 'memory')
@hook.command @hook.command
def remember(bot, input): def remember(bot, input):
".remember <word> <data> -- maps word to data in the memory" ".remember <word> <data> -- maps word to data in the memory"
@ -53,6 +57,7 @@ def remember(bot, input):
memory[filename][low] = input.inp.strip() memory[filename][low] = input.inp.strip()
save_memory(filename, memory[filename]) save_memory(filename, memory[filename])
@hook.command @hook.command
def forget(bot, input): def forget(bot, input):
".forget <word> -- forgets the mapping that word had" ".forget <word> -- forgets the mapping that word had"
@ -74,6 +79,7 @@ def forget(bot, input):
del memory[filename][low] del memory[filename][low]
save_memory(filename, memory[filename]) save_memory(filename, memory[filename])
@hook.command(hook='\?(.+)', prefix=False) @hook.command(hook='\?(.+)', prefix=False)
def question(bot, input): def question(bot, input):
"?<word> -- shows what data is associated with word" "?<word> -- shows what data is associated with word"

View File

@ -8,6 +8,7 @@ from lxml import etree
import hook import hook
@hook.command @hook.command
def twitter(bot, input): def twitter(bot, input):
".twitter <user> - gets most recent tweet from <user>" ".twitter <user> - gets most recent tweet from <user>"

View File

@ -3,6 +3,7 @@ import urllib
import hook import hook
@hook.command('u') @hook.command('u')
@hook.command @hook.command
def urban(inp): def urban(inp):

View File

@ -13,6 +13,7 @@ import hook
lock = thread.allocate_lock() lock = thread.allocate_lock()
stalk = {} stalk = {}
def load_stalk(filename, mtimes={}): def load_stalk(filename, mtimes={}):
if not os.path.exists(filename): if not os.path.exists(filename):
return {} return {}
@ -22,12 +23,14 @@ def load_stalk(filename, mtimes={}):
return dict(x.strip().split(None, 1) for x in return dict(x.strip().split(None, 1) for x in
codecs.open(filename, 'r', 'utf-8')) codecs.open(filename, 'r', 'utf-8'))
def save_stalk(filename, houses): def save_stalk(filename, houses):
out = codecs.open(filename, 'w', 'utf-8') out = codecs.open(filename, 'w', 'utf-8')
out.write('\n'.join('%s %s' % x for x in sorted(houses.iteritems()))) #_heh_ out.write('\n'.join('%s %s' % x for x in sorted(houses.iteritems()))) #heh
out.flush() out.flush()
out.close() out.close()
@hook.command @hook.command
def weather(bot, input): def weather(bot, input):
".weather <location> -- queries the google weather API for weather data" ".weather <location> -- queries the google weather API for weather data"

View File

@ -12,10 +12,12 @@ search_url = api_prefix + "?action=opensearch&search=%s&format=xml"
paren_re = re.compile('\s*\(.*\)$') paren_re = re.compile('\s*\(.*\)$')
@hook.command(hook='w(\s+.*|$)') @hook.command(hook='w(\s+.*|$)')
@hook.command @hook.command
def wiki(query): def wiki(query):
'''.w/.wiki <phrase> -- gets first sentence of wikipedia article on <phrase>''' '''.w/.wiki <phrase> -- gets first sentence of wikipedia ''' \
'''article on <phrase>'''
if not query.strip(): if not query.strip():
return wiki.__doc__ return wiki.__doc__

View File

@ -6,6 +6,7 @@ import hook
locale.setlocale(locale.LC_ALL, "") locale.setlocale(locale.LC_ALL, "")
def ytdata(id): def ytdata(id):
url = 'http://gdata.youtube.com/feeds/api/videos/' + id url = 'http://gdata.youtube.com/feeds/api/videos/' + id
x = etree.parse(url) x = etree.parse(url)
@ -31,6 +32,7 @@ def ytdata(id):
youtube_re = re.compile(r'.*youtube.*v=([-_a-z0-9]+)', flags=re.IGNORECASE) youtube_re = re.compile(r'.*youtube.*v=([-_a-z0-9]+)', flags=re.IGNORECASE)
#@hook.command(hook=r'(.*)', prefix=False) #@hook.command(hook=r'(.*)', prefix=False)
def youtube(inp): def youtube(inp):
m = youtube_re.match(inp) m = youtube_re.match(inp)