Skip to content

Commit

Permalink
Added EventSource. Useful for later.
Browse files Browse the repository at this point in the history
  • Loading branch information
AstraLuma committed Nov 22, 2011
1 parent a2a9a57 commit cce0798
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
57 changes: 57 additions & 0 deletions event.py
@@ -0,0 +1,57 @@
"""
Defines a mix-in class for events.
"""
import threading, traceback

class EventSource(object):
"""
This is a mix-in for classes that want to produce events for other modules
to subscribe to.
"""
__calls = None
def __init__(self, *p, **kw):
super(EventSource, self).__init__(*p, **kw)
self.__calls = {}

def connect(self, event, func, thread=True):
"""es.connect(str, callable, [bool])
Registers your callback against the named event. If thread is True, the
callback will be called in a new thread.
"""
event = str(event)
e = self.__calls.setdefault(event, {})
e[func] = thread

def disconnect(self, event, func):
"""es.disconnect(str, callable)
Removes the given callback from the named event. If it's not there,
ValueError is raised.
"""
try:
del self.__calls[event][func]
except KeyError:
raise ValueError

def emit(self, event, *p, **kw):
"""es.emit(str, ...)
Throws the event and calls the callbacks for it. Any additional
parameters are passed to the callbacks.
"""
try:
calls = self.__calls[event]
except KeyError:
return

for func, thread in calls.items():
if thread:
t = threading.Thread(target=func, args=p, kwargs=kw)
t.setDaemon(True)
t.start()
else:
try:
func(*p, **kw)
except KeyboardInterrupt:
raise
except:
print >> sys.stderr, "Error in event %s, ignored" % event
traceback.print_exc()
14 changes: 10 additions & 4 deletions modules/nicktracker.py
Expand Up @@ -5,8 +5,8 @@
Licensed under the Eiffel Forum License 2.
"""
#TODO: Users of identified nicks start with a '~'. How can we use this?

import time, bot, re, datetime, threading
from __future__ import absolute_import
import time, bot, re, datetime, threading, event

storage = {} # This is used to store the data from INFO

Expand Down Expand Up @@ -65,9 +65,12 @@ def __new__(cls, bot, text, origin, bytes, match, event, args):
self.owner = self.canonnick.lower() == bot.config.owner.lower()
return self

class NickTracker(object):
class NickTracker(event.EventSource):
"""
The API for modules to access the nick database.
Events defined:
* have-account: nick, account, status
"""
# This is used to store the nick<->accounts mapping
nicks = None # A dict: {nick.lower() : {'account': account, 'status': UNREGISTERED..LOGGEDIN }, ...}
Expand Down Expand Up @@ -182,6 +185,9 @@ def _updatelive(self, account, nick, status):
self.accounts[account.lower()].remove(nick.lower())
except (KeyError, ValueError):
pass

if data['status'] > 0 and data['account']:
self.emit('have-account', nick, data['account'], status)

def _updateinfo(self, data):
"""
Expand Down Expand Up @@ -220,7 +226,7 @@ def processlist(phenny):
pass
else:
query_acc(phenny, nick)
time.sleep(5) # The guideline is one message every 2 seconds.
time.sleep(5) # The guideline is one message every 2 seconds, and irc.py enforces 3.

###################
# QUERY FUNCTIONS #
Expand Down

0 comments on commit cce0798

Please sign in to comment.