Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[inkblot] minor fix

git-svn-id: https://dev.upnl.org/svn/ircbot2/trunk@77 66f668a4-574d-4515-85ec-6e73d9c367eb
  • Loading branch information...
commit 6f647fa73ad94d84e88aa180c4c63679672972dd 1 parent 211e89a
@puzzlet authored
Showing with 40 additions and 26 deletions.
  1. +40 −24 inkblot.py
  2. +0 −2  lib/whois.py
View
64 inkblot.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python
-# coding:utf-8
+# -*- encoding: utf-8 -*-
import os
import sys
import imp
@@ -14,23 +14,23 @@ def periodic(period):
"""Decorate a class instance method so that the method would be
periodically executed by irclib framework.
"""
- def decorator(f):
- def new_f(self, *args):
+ def decorator(fun):
+ def new_fun(self, *args):
try:
- f(self, *args)
+ fun(self, *args)
except StopIteration:
return
finally:
- self.ircobj.execute_delayed(period, new_f, (self,) + args)
- return new_f
+ self.ircobj.execute_delayed(period, new_fun, (self,) + args)
+ return new_fun
return decorator
class Inkblot(BufferingBot):
def __init__(self, config_file_name):
self.config = None
self.config_file_name = config_file_name
- self.config_timestamp = self.get_config_time()
- self.config = eval(open(self.config_file_name).read())
+ self.config_timestamp = self._get_config_time()
+ self.config = self._get_config_data()
server = self.config['server']
nickname = self.config['nickname']
@@ -40,29 +40,41 @@ def __init__(self, config_file_name):
self.handlers = collections.defaultdict(list)
- self.version = self.config['version']
self.load_plugins()
self.connection.add_global_handler('welcome', self._on_connected)
- self.check_config_file()
- def _on_connected(self, connection, event):
- for chan in self.config['channels']:
- self.connection.join(chan)
+ self.version = self.config['version']
+ self._check_config_file()
- def get_config_time(self):
+ def _get_config_time(self):
+ if not os.access(self.config_file_name, os.F_OK):
+ return -1
return os.stat(self.config_file_name).st_mtime
+ def _get_config_data(self):
+ if not os.access(self.config_file_name, os.R_OK):
+ return None
+ try:
+ return eval(open(self.config_file_name).read())
+ except SyntaxError:
+ traceback.print_exc()
+ return None
+
@periodic(1)
- def check_config_file(self):
+ def _check_config_file(self):
try:
- t = self.get_config_time()
+ t = self._get_config_time()
if t <= self.config_timestamp:
return
self.reload()
except Exception:
traceback.print_exc()
+ def _on_connected(self, connection, event):
+ for chan in self.config['channels']:
+ self.connection.join(chan)
+
def reply(self, event, message):
try:
message = message.encode('utf-8')
@@ -79,11 +91,11 @@ def reply(self, event, message):
self.buffer.push(Message('privmsg', (reply_to, message)))
def reload(self):
- print("reloading...")
data = eval(open(self.config_file_name).read())
- self.config = data
if self.version >= data['version']:
return
+ print("reloading...")
+ self.config = data
self.config_timestamp = os.stat(self.config_file_name).st_mtime
self.version = data['version']
irclib.DEBUG = data.get('debug', False)
@@ -98,30 +110,33 @@ def load_plugins(self):
plugin_names.append(x[:-3])
for plugin_name in plugin_names:
try:
- fp, filename, opt = imp.find_module(plugin_name, [import_path])
+ file_obj, filename, opt = imp.find_module(plugin_name,
+ [import_path])
except ImportError:
traceback.print_exc()
continue
try:
- plugin = imp.load_module(plugin_name, fp, filename, opt)
+ plugin = imp.load_module(plugin_name, file_obj, filename, opt)
self.load_plugin(plugin)
except Exception:
traceback.print_exc()
finally:
- if fp:
- fp.close()
+ if file_obj:
+ file_obj.close()
def load_plugin(self, plugin):
for action in ['privmsg', 'pubmsg']:
if hasattr(plugin, 'on_'+action):
def handler(connection, event):
try:
- getattr(plugin, 'on_'+action).__call__(self, connection, event)
+ getattr(plugin, 'on_'+action).__call__(
+ self, connection, event)
except:
reply = event.target() # XXX freenode only
tb = traceback.format_exc()
print(tb)
- self.buffer.push(Message('privmsg', (reply, tb.splitlines()[-1])))
+ self.buffer.push(
+ Message('privmsg', (reply, tb.splitlines()[-1])))
self.handlers[action].append(handler)
self.connection.add_global_handler(action, handler, 0)
self.plugins.append(plugin)
@@ -133,6 +148,7 @@ def reload_plugins(self): # XXX
self.load_plugins()
INKBLOT_ROOT = os.path.dirname(os.path.abspath(__file__))
+
def main():
profile = None
if len(sys.argv) > 1:
View
2  lib/whois.py
@@ -141,8 +141,6 @@ def IPWhois(addr):
wdigest = ipwregion['ARIN'][3]
while 1:
for server, suffix, redir, digest in list(ipwregion.values()):
- print(repr(redir))
- print(repr(winfo))
if redir and redir.search(winfo):
winfo = Whois(ip+suffix, server)
wdigest = digest
Please sign in to comment.
Something went wrong with that request. Please try again.