From aae684170d6c32e29e87dfb04614372d01fb8f71 Mon Sep 17 00:00:00 2001 From: urlbot Date: Sat, 27 Sep 2014 09:19:46 +0200 Subject: [PATCH] moved common stuff to common.py; import adjustments --- common.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ plugins.py | 16 ++++------------ urlbot.py | 38 ++------------------------------------ 3 files changed, 50 insertions(+), 48 deletions(-) create mode 100644 common.py diff --git a/common.py b/common.py new file mode 100644 index 0000000..8cb5335 --- /dev/null +++ b/common.py @@ -0,0 +1,44 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +if '__main__' == __name__: + print '''this is a library file, which is not meant to be executed''' + exit(-1) + +import sys, os, time + +RATE_GLOBAL = 0x01 +RATE_NO_SILENCE = 0x02 +RATE_INTERACTIVE = 0x04 +RATE_CHAT = 0x08 +RATE_URL = 0x10 + +BUFSIZ = 8192 +delay = 0.100 # seconds + +basedir = '.' +if 2 == len(sys.argv): basedir = sys.argv[1] + +event_files_dir = os.path.join(basedir, 'event_files') +fifo_path = os.path.join(basedir, 'cmdfifo') + +def debug_enabled(): +# return True + return False + +def e(data): + if data: + if unicode == type(data): + return data.encode('utf8') + elif str == type(data): + return data.encode('string-escape') + else: + return data + else: + return "''" + +def logger(severity, message): +# sev = ( 'err', 'warn', 'info' ) +# if severity in sev: + args = (sys.argv[0], time.strftime('%Y-%m-%d.%H:%M:%S'), severity, message) + sys.stderr.write(e('%s %s %s: %s' % args) + '\n') diff --git a/plugins.py b/plugins.py index 7ea860f..af6bc6a 100644 --- a/plugins.py +++ b/plugins.py @@ -5,11 +5,9 @@ if '__main__' == __name__: print '''this is a plugin file, which is not meant to be executed''' exit(-1) -RATE_GLOBAL = 0x01 -RATE_NO_SILENCE = 0x02 -RATE_INTERACTIVE = 0x04 -RATE_CHAT = 0x08 -RATE_URL = 0x10 +import time, random +from local_config import conf +from common import * plugins = {} plugins['parse'] = [] @@ -290,11 +288,9 @@ funcs['command'] = ( _dir = dir() -debug = False -if debug: +if debug_enabled(): def _chat_write(a): _logger('chat_write', a) def _conf(a): return 'bot' - def _logger(a, b): print 'logger: %s::%s' %(a, b) def _ratelimit_exceeded(ignored=None): return False def _ratelimit_touch(ignored=None): return True @@ -302,14 +298,10 @@ if debug: except NameError: chat_write = _chat_write try: conf except NameError: conf = _conf - try: logger - except NameError: logger = _logger try: ratelimit_exceeded except NameError: ratelimit_exceeded = _ratelimit_exceeded try: ratelimit_touch except NameError: ratelimit_touch = _ratelimit_touch - try: random - except NameError: import random def register(func_type, auto=False): plugins[func_type] = [] diff --git a/urlbot.py b/urlbot.py index 7f9b85f..b397dd7 100755 --- a/urlbot.py +++ b/urlbot.py @@ -1,17 +1,9 @@ #!/usr/bin/python # -*- coding: utf-8 -*- -import sys, os, re, time, urllib, pickle, random, HTMLParser, stat +import sys, os, re, time, urllib, pickle, HTMLParser, stat from local_config import conf, set_conf - -BUFSIZ = 8192 -delay = 0.100 # seconds - -basedir = '.' -if 2 == len(sys.argv): basedir = sys.argv[1] - -event_files_dir = os.path.join(basedir, 'event_files') -fifo_path = os.path.join(basedir, 'cmdfifo') +from common import * # rate limiting to 5 messages per 10 minutes hist_ts = [] @@ -19,27 +11,6 @@ hist_flag = True parser = None -def debug_enabled(): -# return True - return False - -def e(data): - if data: - if unicode == type(data): - return data.encode('utf8') - elif str == type(data): - return data.encode('string-escape') - else: - return data - else: - return "''" - -def logger(severity, message): -# sev = ( 'err', 'warn', 'info' ) -# if severity in sev: - args = (sys.argv[0], time.strftime('%Y-%m-%d.%H:%M:%S'), severity, message) - sys.stderr.write(e('%s %s %s: %s' % args) + '\n') - class urllib_user_agent_wrapper(urllib.FancyURLopener): version = '''Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0 Iceweasel/31.0''' @@ -232,14 +203,9 @@ def get_version_git(): import plugins plugins.chat_write = chat_write -plugins.conf = conf -plugins.logger = logger plugins.ratelimit_exceeded = ratelimit_exceeded plugins.ratelimit_touch = ratelimit_touch -plugins.random = random -plugins.time = time - plugins.register_all() if '__main__' == __name__: