whitespace fixes according to pep8; cleanup
pep8 --ignore=W191,E225,E501,E401,E302,E122,E123 *.py
This commit is contained in:
21
common.py
21
common.py
@@ -8,17 +8,18 @@ if '__main__' == __name__:
|
|||||||
import sys, os, time, pickle
|
import sys, os, time, pickle
|
||||||
from local_config import conf
|
from local_config import conf
|
||||||
|
|
||||||
RATE_GLOBAL = 0x01
|
RATE_GLOBAL = 0x01
|
||||||
RATE_NO_SILENCE = 0x02
|
RATE_NO_SILENCE = 0x02
|
||||||
RATE_INTERACTIVE = 0x04
|
RATE_INTERACTIVE = 0x04
|
||||||
RATE_CHAT = 0x08
|
RATE_CHAT = 0x08
|
||||||
RATE_URL = 0x10
|
RATE_URL = 0x10
|
||||||
|
|
||||||
BUFSIZ = 8192
|
BUFSIZ = 8192
|
||||||
delay = 0.100 # seconds
|
delay = 0.100 # seconds
|
||||||
|
|
||||||
basedir = '.'
|
basedir = '.'
|
||||||
if 2 == len(sys.argv): basedir = sys.argv[1]
|
if 2 == len(sys.argv):
|
||||||
|
basedir = sys.argv[1]
|
||||||
|
|
||||||
def debug_enabled():
|
def debug_enabled():
|
||||||
# return True
|
# return True
|
||||||
@@ -42,7 +43,7 @@ def conf_load():
|
|||||||
def levenshtein(a, b, return_table=False):
|
def levenshtein(a, b, return_table=False):
|
||||||
'''returns the levenshtein distance between a and b'''
|
'''returns the levenshtein distance between a and b'''
|
||||||
# initialisize a table with 0, but the 0-rows/cols with their index
|
# 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
|
i = j = 0
|
||||||
for i in range(1, len(a)+1):
|
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]
|
d[i][j] = d[i-1][j-1]
|
||||||
else:
|
else:
|
||||||
d[i][j] = min(
|
d[i][j] = min(
|
||||||
d[i-1][j] + 1, # deletion
|
d[i-1][j] + 1, # deletion
|
||||||
d[i][j-1] + 1, # insertion
|
d[i][j-1] + 1, # insertion
|
||||||
d[i-1][j-1] + 1, # substitution
|
d[i-1][j-1] + 1, # substitution
|
||||||
)
|
)
|
||||||
|
|
||||||
if return_table:
|
if return_table:
|
||||||
|
|||||||
@@ -2,9 +2,13 @@
|
|||||||
|
|
||||||
import time, sys
|
import time, sys
|
||||||
|
|
||||||
def _logger(a, b): sys.stderr.write('logger: %s::%s\n' %(a, b))
|
def _logger(a, b):
|
||||||
try: logger
|
sys.stderr.write('logger: %s::%s\n' %(a, b))
|
||||||
except NameError: logger = _logger
|
|
||||||
|
try:
|
||||||
|
logger
|
||||||
|
except NameError:
|
||||||
|
logger = _logger
|
||||||
|
|
||||||
if '__main__' == __name__:
|
if '__main__' == __name__:
|
||||||
print('''this is a config file, which is not meant to be executed''')
|
print('''this is a config file, which is not meant to be executed''')
|
||||||
@@ -26,8 +30,6 @@ config = {
|
|||||||
'uptime': -time.time(),
|
'uptime': -time.time(),
|
||||||
'request_counter': 0,
|
'request_counter': 0,
|
||||||
|
|
||||||
'path_event_files': 'event_files', # XXX obsolete
|
|
||||||
'path_cmdfifo': 'cmdfifo', # XXX obsolete
|
|
||||||
'persistent_storage': 'urlbot.persistent',
|
'persistent_storage': 'urlbot.persistent',
|
||||||
|
|
||||||
'url_blacklist': [
|
'url_blacklist': [
|
||||||
@@ -35,8 +37,8 @@ config = {
|
|||||||
r'^.*wikipedia\.org/wiki/.*$'
|
r'^.*wikipedia\.org/wiki/.*$'
|
||||||
],
|
],
|
||||||
|
|
||||||
# the "dice" feature will use more efficient random data (0) for given users
|
# the "dice" feature will use more efficient random data (0) for given users
|
||||||
'enhanced-random-user': ( 'FIXME', 'FIXME' ),
|
'enhanced-random-user': ('FIXME', 'FIXME'),
|
||||||
|
|
||||||
'tea_steep_time': (3*60 + 40),
|
'tea_steep_time': (3*60 + 40),
|
||||||
|
|
||||||
|
|||||||
95
plugins.py
95
plugins.py
@@ -117,7 +117,8 @@ def data_parse_other(msg):
|
|||||||
|
|
||||||
if 'args' in list(p.keys()):
|
if 'args' in list(p.keys()):
|
||||||
for a in p['args']:
|
for a in p['args']:
|
||||||
if None == a: continue
|
if None == a:
|
||||||
|
continue
|
||||||
|
|
||||||
if 'data' == a:
|
if 'data' == a:
|
||||||
args['data'] = data
|
args['data'] = data
|
||||||
@@ -157,7 +158,6 @@ def command_help(args):
|
|||||||
'ratelimit_class': RATE_GLOBAL
|
'ratelimit_class': RATE_GLOBAL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
cmd = None
|
cmd = None
|
||||||
flag = False
|
flag = False
|
||||||
|
|
||||||
@@ -169,7 +169,7 @@ def command_help(args):
|
|||||||
if 'help' == word:
|
if 'help' == word:
|
||||||
flag = True
|
flag = True
|
||||||
|
|
||||||
if False == flag: # no match on 'help'
|
if False == flag: # no match on 'help'
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if None == cmd:
|
if None == cmd:
|
||||||
@@ -219,15 +219,14 @@ def command_klammer(args):
|
|||||||
if 'klammer' in args['data']:
|
if 'klammer' in args['data']:
|
||||||
logger('plugin', 'sent karl klammer')
|
logger('plugin', 'sent karl klammer')
|
||||||
return {
|
return {
|
||||||
'msg':
|
'msg': (
|
||||||
(
|
args['reply_user'] + r''': _, Was moechten''',
|
||||||
args['reply_user'] + r''': _, Was moechten''',
|
args['reply_user'] + r''': ( _\_ Sie tun?''',
|
||||||
args['reply_user'] + r''': ( _\_ Sie tun?''',
|
args['reply_user'] + r''': \0 O\ ''',
|
||||||
args['reply_user'] + r''': \0 O\ ''',
|
args['reply_user'] + r''': \\ \\ [ ] ja ''',
|
||||||
args['reply_user'] + r''': \\ \\ [ ] ja ''',
|
args['reply_user'] + r''': \`' ) [ ] noe''',
|
||||||
args['reply_user'] + r''': \`' ) [ ] noe''',
|
args['reply_user'] + r''': `'' '''
|
||||||
args['reply_user'] + r''': `'' '''
|
)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def command_unicode(args):
|
def command_unicode(args):
|
||||||
@@ -242,12 +241,11 @@ def command_unicode(args):
|
|||||||
if 'unikot' in args['data']:
|
if 'unikot' in args['data']:
|
||||||
logger('plugin', 'sent some unicode')
|
logger('plugin', 'sent some unicode')
|
||||||
return {
|
return {
|
||||||
'msg':
|
'msg': (
|
||||||
(
|
args['reply_user'] + ''': ┌────────┐''',
|
||||||
args['reply_user'] + ''': ┌────────┐''',
|
args['reply_user'] + ''': │Unicode!│''',
|
||||||
args['reply_user'] + ''': │Unicode!│''',
|
args['reply_user'] + ''': └────────┘'''
|
||||||
args['reply_user'] + ''': └────────┘'''
|
)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def command_source(args):
|
def command_source(args):
|
||||||
@@ -276,7 +274,7 @@ def command_dice(args):
|
|||||||
|
|
||||||
if 'dice' in args['data']:
|
if 'dice' in args['data']:
|
||||||
if args['reply_user'] in conf('enhanced-random-user'):
|
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)')
|
logger('plugin', 'sent random (enhanced)')
|
||||||
else:
|
else:
|
||||||
rnd = random.randint(1, 6)
|
rnd = random.randint(1, 6)
|
||||||
@@ -301,8 +299,10 @@ def command_uptime(args):
|
|||||||
plural_uptime = 's'
|
plural_uptime = 's'
|
||||||
plural_request = 's'
|
plural_request = 's'
|
||||||
|
|
||||||
if 1 == u: plural_uptime = ''
|
if 1 == u:
|
||||||
if 1 == conf('request_counter'): plural_request = ''
|
plural_uptime = ''
|
||||||
|
if 1 == conf('request_counter'):
|
||||||
|
plural_request = ''
|
||||||
|
|
||||||
logger('plugin', 'sent statistics')
|
logger('plugin', 'sent statistics')
|
||||||
return {
|
return {
|
||||||
@@ -319,7 +319,7 @@ def command_ping(args):
|
|||||||
}
|
}
|
||||||
|
|
||||||
if 'ping' in args['data']:
|
if 'ping' in args['data']:
|
||||||
rnd = random.randint(0, 3) # 1:4
|
rnd = random.randint(0, 3) # 1:4
|
||||||
if 0 == rnd:
|
if 0 == rnd:
|
||||||
msg = args['reply_user'] + ''': peng (You're dead now.)'''
|
msg = args['reply_user'] + ''': peng (You're dead now.)'''
|
||||||
logger('plugin', 'sent pong (variant)')
|
logger('plugin', 'sent pong (variant)')
|
||||||
@@ -438,7 +438,7 @@ def command_show_blacklist(args):
|
|||||||
return {
|
return {
|
||||||
'msg': [
|
'msg': [
|
||||||
args['reply_user'] + ': URL blacklist: ' + b
|
args['reply_user'] + ': URL blacklist: ' + b
|
||||||
for b in conf('url_blacklist')
|
for b in conf('url_blacklist')
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -468,7 +468,7 @@ def data_parse_commands(msg):
|
|||||||
data = msg['body']
|
data = msg['body']
|
||||||
words = data.split(' ')
|
words = data.split(' ')
|
||||||
|
|
||||||
if 2 > len(words): # need at least two words
|
if 2 > len(words): # need at least two words
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# don't reply if beginning of the text matches bot_user
|
# 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()):
|
if 'args' in list(p.keys()):
|
||||||
for a in p['args']:
|
for a in p['args']:
|
||||||
if None == a: continue
|
if None == a:
|
||||||
|
continue
|
||||||
|
|
||||||
if 'data' == a:
|
if 'data' == a:
|
||||||
args['data'] = data
|
args['data'] = data
|
||||||
@@ -512,7 +513,7 @@ def data_parse_commands(msg):
|
|||||||
|
|
||||||
if None != ret:
|
if None != ret:
|
||||||
if 'msg' in list(ret.keys()):
|
if 'msg' in list(ret.keys()):
|
||||||
if str == type(ret['msg']): # FIXME 2to3
|
if str == type(ret['msg']): # FIXME 2to3
|
||||||
ratelimit_touch(RATE_CHAT)
|
ratelimit_touch(RATE_CHAT)
|
||||||
if ratelimit_exceeded(RATE_CHAT):
|
if ratelimit_exceeded(RATE_CHAT):
|
||||||
return False
|
return False
|
||||||
@@ -551,19 +552,37 @@ funcs['command'] = (
|
|||||||
_dir = dir()
|
_dir = dir()
|
||||||
|
|
||||||
if debug_enabled():
|
if debug_enabled():
|
||||||
def _chat_write(a): logger('chat_write', a)
|
def _chat_write(a):
|
||||||
def _conf(a): return 'bot'
|
logger('chat_write', a)
|
||||||
def _ratelimit_exceeded(ignored=None): return False
|
|
||||||
def _ratelimit_touch(ignored=None): return True
|
|
||||||
|
|
||||||
try: chat_write
|
def _conf(a):
|
||||||
except NameError: chat_write = _chat_write
|
return 'bot'
|
||||||
try: conf
|
|
||||||
except NameError: conf = _conf
|
def _ratelimit_exceeded(ignored=None):
|
||||||
try: ratelimit_exceeded
|
return False
|
||||||
except NameError: ratelimit_exceeded = _ratelimit_exceeded
|
|
||||||
try: ratelimit_touch
|
def _ratelimit_touch(ignored=None):
|
||||||
except NameError: ratelimit_touch = _ratelimit_touch
|
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')
|
logger('info', 'debugging enabled')
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,10 @@ def sum_array(array):
|
|||||||
|
|
||||||
def wrapper_print(a, b, comment=''):
|
def wrapper_print(a, b, comment=''):
|
||||||
ret = str_sim(a, b, do_print=True)
|
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))
|
print('[%2dx%2d::%2d]%s' %(len(ret), len(ret[0]), sum_array(ret), comment))
|
||||||
|
|
||||||
if '__main__' == __name__:
|
if '__main__' == __name__:
|
||||||
|
|||||||
@@ -14,4 +14,3 @@ class TestEventlooper(unittest.TestCase):
|
|||||||
broken_url = 'http://foo'
|
broken_url = 'http://foo'
|
||||||
result = fetch_page(url=broken_url)
|
result = fetch_page(url=broken_url)
|
||||||
self.assertEqual(result, (None, None))
|
self.assertEqual(result, (None, None))
|
||||||
|
|
||||||
|
|||||||
15
urlbot.py
15
urlbot.py
@@ -40,7 +40,7 @@ def fetch_page(url):
|
|||||||
request = urllib.request.Request(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''')
|
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)
|
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()
|
response.close()
|
||||||
return (0, html_text, response.headers)
|
return (0, html_text, response.headers)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -71,8 +71,10 @@ def extract_title(url):
|
|||||||
if 'text/' != headers['content-type'][:len('text/')]:
|
if 'text/' != headers['content-type'][:len('text/')]:
|
||||||
return (1, headers['content-type'])
|
return (1, headers['content-type'])
|
||||||
|
|
||||||
charset = re.sub('.*charset=(?P<charset>\S+).*',
|
charset = re.sub(
|
||||||
'\g<charset>', headers['content-type'], re.IGNORECASE)
|
'.*charset=(?P<charset>\S+).*',
|
||||||
|
'\g<charset>', headers['content-type'], re.IGNORECASE
|
||||||
|
)
|
||||||
|
|
||||||
if '' != charset:
|
if '' != charset:
|
||||||
try:
|
try:
|
||||||
@@ -92,7 +94,7 @@ def extract_title(url):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
expanded_html = parser.unescape(match)
|
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))
|
logger('warn', 'parser.unescape() expoded here: ' + str(e))
|
||||||
expanded_html = match
|
expanded_html = match
|
||||||
return (0, expanded_html)
|
return (0, expanded_html)
|
||||||
@@ -118,14 +120,14 @@ def chat_write(message):
|
|||||||
mtype='groupchat'
|
mtype='groupchat'
|
||||||
)
|
)
|
||||||
|
|
||||||
def ratelimit_touch(ignored=None): # FIXME: separate counters
|
def ratelimit_touch(ignored=None): # FIXME: separate counters
|
||||||
hist_ts.append(time.time())
|
hist_ts.append(time.time())
|
||||||
|
|
||||||
if conf('hist_max_count') < len(hist_ts):
|
if conf('hist_max_count') < len(hist_ts):
|
||||||
hist_ts.pop(0)
|
hist_ts.pop(0)
|
||||||
|
|
||||||
|
|
||||||
def ratelimit_exceeded(ignored=None): # FIXME: separate counters
|
def ratelimit_exceeded(ignored=None): # FIXME: separate counters
|
||||||
global hist_flag
|
global hist_flag
|
||||||
|
|
||||||
if conf('hist_max_count') < len(hist_ts):
|
if conf('hist_max_count') < len(hist_ts):
|
||||||
@@ -304,6 +306,7 @@ if '__main__' == __name__:
|
|||||||
|
|
||||||
while 1:
|
while 1:
|
||||||
try:
|
try:
|
||||||
|
# FIXME: find a way to trigger them
|
||||||
plugins.event_trigger()
|
plugins.event_trigger()
|
||||||
|
|
||||||
time.sleep(delay)
|
time.sleep(delay)
|
||||||
|
|||||||
Reference in New Issue
Block a user