From 01ea2d3d6c57cba74fce31bde0d1ead2150f903a Mon Sep 17 00:00:00 2001 From: urlbot Date: Tue, 2 Dec 2014 17:01:40 +0100 Subject: [PATCH] whitespace fixes according to pep8; cleanup pep8 --ignore=W191,E225,E501,E401,E302,E122,E123 *.py --- common.py | 21 ++++----- local_config.py.skel | 26 ++++++----- plugins.py | 101 +++++++++++++++++++++++++------------------ strsim.py | 7 ++- test_urlbot.py | 1 - urlbot.py | 17 +++++--- 6 files changed, 100 insertions(+), 73 deletions(-) diff --git a/common.py b/common.py index 86eb7ad..a4423e2 100644 --- a/common.py +++ b/common.py @@ -8,17 +8,18 @@ if '__main__' == __name__: import sys, os, time, pickle from local_config import conf -RATE_GLOBAL = 0x01 -RATE_NO_SILENCE = 0x02 +RATE_GLOBAL = 0x01 +RATE_NO_SILENCE = 0x02 RATE_INTERACTIVE = 0x04 -RATE_CHAT = 0x08 -RATE_URL = 0x10 +RATE_CHAT = 0x08 +RATE_URL = 0x10 BUFSIZ = 8192 -delay = 0.100 # seconds +delay = 0.100 # seconds basedir = '.' -if 2 == len(sys.argv): basedir = sys.argv[1] +if 2 == len(sys.argv): + basedir = sys.argv[1] def debug_enabled(): # return True @@ -42,7 +43,7 @@ def conf_load(): def levenshtein(a, b, return_table=False): '''returns the levenshtein distance between a and b''' # initialisize a table with 0, but the 0-rows/cols with their index - d = [[ (i if 0 == j else j if 0 == i else 0) for j in range(len(b)+1) ] for i in range(len(a)+1) ] + d = [[(i if 0 == j else j if 0 == i else 0) for j in range(len(b)+1)] for i in range(len(a)+1)] i = j = 0 for i in range(1, len(a)+1): @@ -51,9 +52,9 @@ def levenshtein(a, b, return_table=False): d[i][j] = d[i-1][j-1] else: d[i][j] = min( - d[i-1][j] + 1, # deletion - d[i][j-1] + 1, # insertion - d[i-1][j-1] + 1, # substitution + d[i-1][j] + 1, # deletion + d[i][j-1] + 1, # insertion + d[i-1][j-1] + 1, # substitution ) if return_table: diff --git a/local_config.py.skel b/local_config.py.skel index ec293fb..adbeda0 100644 --- a/local_config.py.skel +++ b/local_config.py.skel @@ -2,9 +2,13 @@ import time, sys -def _logger(a, b): sys.stderr.write('logger: %s::%s\n' %(a, b)) -try: logger -except NameError: logger = _logger +def _logger(a, b): + sys.stderr.write('logger: %s::%s\n' %(a, b)) + +try: + logger +except NameError: + logger = _logger if '__main__' == __name__: print('''this is a config file, which is not meant to be executed''') @@ -16,27 +20,25 @@ config = { 'room': 'FIXME', 'src-url': 'FIXME', - + 'bot_user': 'native-urlbot', 'bot_owner': 'FIXME', - + 'hist_max_count': 5, 'hist_max_time': 10 * 60, - + 'uptime': -time.time(), 'request_counter': 0, - - 'path_event_files': 'event_files', # XXX obsolete - 'path_cmdfifo': 'cmdfifo', # XXX obsolete + 'persistent_storage': 'urlbot.persistent', - + 'url_blacklist': [ r'^.*heise\.de/[^/]+/meldung/.*$', r'^.*wikipedia\.org/wiki/.*$' ], -# the "dice" feature will use more efficient random data (0) for given users - 'enhanced-random-user': ( 'FIXME', 'FIXME' ), + # the "dice" feature will use more efficient random data (0) for given users + 'enhanced-random-user': ('FIXME', 'FIXME'), 'tea_steep_time': (3*60 + 40), diff --git a/plugins.py b/plugins.py index 26e53b4..a86704d 100644 --- a/plugins.py +++ b/plugins.py @@ -117,7 +117,8 @@ def data_parse_other(msg): if 'args' in list(p.keys()): for a in p['args']: - if None == a: continue + if None == a: + continue if 'data' == a: args['data'] = data @@ -157,7 +158,6 @@ def command_help(args): 'ratelimit_class': RATE_GLOBAL } - cmd = None flag = False @@ -169,7 +169,7 @@ def command_help(args): if 'help' == word: flag = True - if False == flag: # no match on 'help' + if False == flag: # no match on 'help' return None if None == cmd: @@ -219,15 +219,14 @@ def command_klammer(args): if 'klammer' in args['data']: logger('plugin', 'sent karl klammer') return { - 'msg': - ( - args['reply_user'] + r''': _, Was moechten''', - args['reply_user'] + r''': ( _\_ Sie tun?''', - args['reply_user'] + r''': \0 O\ ''', - args['reply_user'] + r''': \\ \\ [ ] ja ''', - args['reply_user'] + r''': \`' ) [ ] noe''', - args['reply_user'] + r''': `'' ''' - ) + 'msg': ( + args['reply_user'] + r''': _, Was moechten''', + args['reply_user'] + r''': ( _\_ Sie tun?''', + args['reply_user'] + r''': \0 O\ ''', + args['reply_user'] + r''': \\ \\ [ ] ja ''', + args['reply_user'] + r''': \`' ) [ ] noe''', + args['reply_user'] + r''': `'' ''' + ) } def command_unicode(args): @@ -242,12 +241,11 @@ def command_unicode(args): if 'unikot' in args['data']: logger('plugin', 'sent some unicode') return { - 'msg': - ( - args['reply_user'] + ''': ┌────────┐''', - args['reply_user'] + ''': │Unicode!│''', - args['reply_user'] + ''': └────────┘''' - ) + 'msg': ( + args['reply_user'] + ''': ┌────────┐''', + args['reply_user'] + ''': │Unicode!│''', + args['reply_user'] + ''': └────────┘''' + ) } def command_source(args): @@ -276,7 +274,7 @@ def command_dice(args): if 'dice' in args['data']: if args['reply_user'] in conf('enhanced-random-user'): - rnd = 0 # this might confuse users. good. + rnd = 0 # this might confuse users. good. logger('plugin', 'sent random (enhanced)') else: rnd = random.randint(1, 6) @@ -301,8 +299,10 @@ def command_uptime(args): plural_uptime = 's' plural_request = 's' - if 1 == u: plural_uptime = '' - if 1 == conf('request_counter'): plural_request = '' + if 1 == u: + plural_uptime = '' + if 1 == conf('request_counter'): + plural_request = '' logger('plugin', 'sent statistics') return { @@ -319,7 +319,7 @@ def command_ping(args): } if 'ping' in args['data']: - rnd = random.randint(0, 3) # 1:4 + rnd = random.randint(0, 3) # 1:4 if 0 == rnd: msg = args['reply_user'] + ''': peng (You're dead now.)''' logger('plugin', 'sent pong (variant)') @@ -381,7 +381,7 @@ def command_teatimer(args): } register_event(ready, chat_write, args['reply_user'] + ': Your tea is ready!') - + return { 'msg': args['reply_user'] + ': Tea timer set to %s' % time.strftime( '%F.%T', time.localtime(ready) @@ -434,11 +434,11 @@ def command_show_blacklist(args): if 'show-blacklist' in args['data']: logger('plugin', 'sent URL blacklist') - + return { 'msg': [ args['reply_user'] + ': URL blacklist: ' + b - for b in conf('url_blacklist') + for b in conf('url_blacklist') ] } @@ -453,7 +453,7 @@ def command_show_blacklist(args): # # if 'dummy' in args['data']: # logger('plugin', 'dummy plugin called') -# +# # return { # 'msg': args['reply_user'] + ': dummy plugin called' # } @@ -468,7 +468,7 @@ def data_parse_commands(msg): data = msg['body'] words = data.split(' ') - if 2 > len(words): # need at least two words + if 2 > len(words): # need at least two words return None # don't reply if beginning of the text matches bot_user @@ -491,7 +491,8 @@ def data_parse_commands(msg): if 'args' in list(p.keys()): for a in p['args']: - if None == a: continue + if None == a: + continue if 'data' == a: args['data'] = data @@ -512,7 +513,7 @@ def data_parse_commands(msg): if None != ret: if 'msg' in list(ret.keys()): - if str == type(ret['msg']): # FIXME 2to3 + if str == type(ret['msg']): # FIXME 2to3 ratelimit_touch(RATE_CHAT) if ratelimit_exceeded(RATE_CHAT): return False @@ -551,19 +552,37 @@ funcs['command'] = ( _dir = dir() if debug_enabled(): - def _chat_write(a): logger('chat_write', a) - def _conf(a): return 'bot' - def _ratelimit_exceeded(ignored=None): return False - def _ratelimit_touch(ignored=None): return True + def _chat_write(a): + logger('chat_write', a) - try: chat_write - except NameError: chat_write = _chat_write - try: conf - except NameError: conf = _conf - try: ratelimit_exceeded - except NameError: ratelimit_exceeded = _ratelimit_exceeded - try: ratelimit_touch - except NameError: ratelimit_touch = _ratelimit_touch + def _conf(a): + return 'bot' + + def _ratelimit_exceeded(ignored=None): + return False + + def _ratelimit_touch(ignored=None): + return True + + try: + chat_write + except NameError: + chat_write = _chat_write + + try: + conf + except NameError: + conf = _conf + + try: + ratelimit_exceeded + except NameError: + ratelimit_exceeded = _ratelimit_exceeded + + try: + ratelimit_touch + except NameError: + ratelimit_touch = _ratelimit_touch logger('info', 'debugging enabled') diff --git a/strsim.py b/strsim.py index 9a98749..f03573b 100755 --- a/strsim.py +++ b/strsim.py @@ -17,7 +17,7 @@ def str_sim(a, b, do_print=False): for j in range(0, len(b_parts)-1): if a_parts[i] == b_parts[j]: out[i][j] += 1 - + if do_print: i = 0 for j in range(0, len(b_parts)): @@ -41,7 +41,10 @@ def sum_array(array): def wrapper_print(a, b, comment=''): ret = str_sim(a, b, do_print=True) - if '' != comment: comment = ' ^ ' + comment + + if '' != comment: + comment = ' ^ ' + comment + print('[%2dx%2d::%2d]%s' %(len(ret), len(ret[0]), sum_array(ret), comment)) if '__main__' == __name__: diff --git a/test_urlbot.py b/test_urlbot.py index 6fcd8ff..a3e8cca 100644 --- a/test_urlbot.py +++ b/test_urlbot.py @@ -14,4 +14,3 @@ class TestEventlooper(unittest.TestCase): broken_url = 'http://foo' result = fetch_page(url=broken_url) self.assertEqual(result, (None, None)) - diff --git a/urlbot.py b/urlbot.py index 7956183..bf5049f 100755 --- a/urlbot.py +++ b/urlbot.py @@ -40,7 +40,7 @@ def fetch_page(url): request = urllib.request.Request(url) request.add_header('User-Agent', '''Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0 Iceweasel/31.0''') response = urllib.request.urlopen(request) - html_text = response.read(BUFSIZ) # ignore more than BUFSIZ + html_text = response.read(BUFSIZ) # ignore more than BUFSIZ response.close() return (0, html_text, response.headers) except Exception as e: @@ -59,7 +59,7 @@ def extract_title(url): logger('info', 'extracting title from ' + url) (code, html_text, headers) = fetch_page(url) - + if 1 == code: return (3, 'failed: %s for %s' %(html_text, url)) @@ -71,8 +71,10 @@ def extract_title(url): if 'text/' != headers['content-type'][:len('text/')]: return (1, headers['content-type']) - charset = re.sub('.*charset=(?P\S+).*', - '\g', headers['content-type'], re.IGNORECASE) + charset = re.sub( + '.*charset=(?P\S+).*', + '\g', headers['content-type'], re.IGNORECASE + ) if '' != charset: try: @@ -92,7 +94,7 @@ def extract_title(url): try: expanded_html = parser.unescape(match) - except UnicodeDecodeError as e: # idk why this can happen, but it does + except UnicodeDecodeError as e: # idk why this can happen, but it does logger('warn', 'parser.unescape() expoded here: ' + str(e)) expanded_html = match return (0, expanded_html) @@ -118,14 +120,14 @@ def chat_write(message): mtype='groupchat' ) -def ratelimit_touch(ignored=None): # FIXME: separate counters +def ratelimit_touch(ignored=None): # FIXME: separate counters hist_ts.append(time.time()) if conf('hist_max_count') < len(hist_ts): hist_ts.pop(0) -def ratelimit_exceeded(ignored=None): # FIXME: separate counters +def ratelimit_exceeded(ignored=None): # FIXME: separate counters global hist_flag if conf('hist_max_count') < len(hist_ts): @@ -304,6 +306,7 @@ if '__main__' == __name__: while 1: try: +# FIXME: find a way to trigger them plugins.event_trigger() time.sleep(delay)