diff options
-rw-r--r-- | cannon.py | 13 | ||||
-rw-r--r-- | data.py | 43 | ||||
-rw-r--r-- | irc.py | 5 |
3 files changed, 56 insertions, 5 deletions
@@ -6,20 +6,23 @@ import os class Cannon(object): - def __init__(self): - self.firecount = {} + def onAddonAdd(self, context, **kwargs): + if not getattr(context.data, "cannon", None): + context.data.cannon = dict() def onChanMsg(self, context, user, channel, targetprefix, msg): + firecount = context.data.cannon + matches = re.findall("^!fire\\s+(.*)$", msg) if matches: nickname = matches[0] if any([nickname.lower() == usr.nick.lower() for usr in channel.users]): vic = context.user(nickname) - if vic in self.firecount.keys(): - count = self.firecount[vic] + 1 + if vic in firecount.keys(): + count = firecount[vic] + 1 else: count = 1 - self.firecount[vic] = count + firecount[vic] = count if 10 <= count % 100 < 20: ordinal = "th" elif count % 10 == 1: @@ -0,0 +1,43 @@ + +class Data: + """Data is a generic storage interface for addons which need to persist data. + + Addons can access Data through the Connection.data attribute. Each addon + should add its own attribute to Data to serve as a namespace. For example, + the Foo addon might store a dict as such: + + Connection.data.foo = dict() + + Data doesn't persist anything on its own but instead relies on a persistence + addon to handle the onDataSave and onDataLoad events. + + Data fires an onDataLoaded event to inform addons that new data may be available. + """ + + def __init__(self, context, **kwargs): + """Create a Data for the given Connection context. + + Optionally pass in a dict of values to load. + """ + self.__dict__ = kwargs + self.context = context + + def set_data(self, values): + """Replace all values in this Data with the given dict.""" + contex = self.context + self.__dict__ = values + self.context = context + self.context.fireEvent([('onDataLoaded', dict(data=self), False)]) + + def merge(self, values): + """Merge a dictionary into this Data.""" + for k, v, in values.iteritems(): + setattr(self, k, v) + self.context.fireEvent([('onDataLoaded', dict(data=self), False)]) + + def save(self): + """Trigger the onDataSave event.""" + data = self.__dict__ + del data['context'] + self.context.fireEvent([('onDataSave', dict(data=self, values=data), False)]) + @@ -18,6 +18,7 @@ import new import inspect import warnings import random +import data __all__ = ["Connection", "Channel", "ChanList", "User", "UserList", "Config", "timestamp"] @@ -355,6 +356,7 @@ class Connection(object): self.connect() def _init(self): + self.data = data.Data(self) self.ipver = None self.addr = None self._connected = False @@ -437,6 +439,9 @@ class Connection(object): print >>self.log, "%s ### Log file opened" % (ts) self.log.flush() + def fireEvent(self, events): + self._event(self.getalladdons(), events) + # Used to call event handlers on all attached addons, when applicable. def _event(self, addons, events, line=None, data=None, exceptions=False): handled = [] |