mirror of
http://aero2k.de/t/repos/urlbot-native.git
synced 2017-09-06 15:25:38 +02:00
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
|
||||
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:
|
||||
|
||||
@@ -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''')
|
||||
@@ -26,8 +30,6 @@ config = {
|
||||
'uptime': -time.time(),
|
||||
'request_counter': 0,
|
||||
|
||||
'path_event_files': 'event_files', # XXX obsolete
|
||||
'path_cmdfifo': 'cmdfifo', # XXX obsolete
|
||||
'persistent_storage': 'urlbot.persistent',
|
||||
|
||||
'url_blacklist': [
|
||||
@@ -35,8 +37,8 @@ config = {
|
||||
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),
|
||||
|
||||
|
||||
95
plugins.py
95
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)')
|
||||
@@ -438,7 +438,7 @@ def command_show_blacklist(args):
|
||||
return {
|
||||
'msg': [
|
||||
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']
|
||||
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')
|
||||
|
||||
|
||||
@@ -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__:
|
||||
|
||||
@@ -14,4 +14,3 @@ class TestEventlooper(unittest.TestCase):
|
||||
broken_url = 'http://foo'
|
||||
result = fetch_page(url=broken_url)
|
||||
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.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:
|
||||
@@ -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<charset>\S+).*',
|
||||
'\g<charset>', headers['content-type'], re.IGNORECASE)
|
||||
charset = re.sub(
|
||||
'.*charset=(?P<charset>\S+).*',
|
||||
'\g<charset>', 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)
|
||||
|
||||
Reference in New Issue
Block a user