summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2015-02-02 07:36:48 -0800
committerJesse Morgan <jesse@jesterpm.net>2015-02-02 07:36:48 -0800
commit88c849b5b3b3d03591f3aa12c3e6daa1ea744dbf (patch)
tree3f71498d2cc450140f86714d4ad36292b86693b1
parent92af71570ab35143c8edab7f45520408751bf3c6 (diff)
Basic interface for generic data persistence.data-persistence
-rw-r--r--cannon.py13
-rw-r--r--data.py43
-rw-r--r--irc.py5
3 files changed, 56 insertions, 5 deletions
diff --git a/cannon.py b/cannon.py
index 0911245..f88f54d 100644
--- a/cannon.py
+++ b/cannon.py
@@ -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:
diff --git a/data.py b/data.py
new file mode 100644
index 0000000..c598451
--- /dev/null
+++ b/data.py
@@ -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)])
+
diff --git a/irc.py b/irc.py
index bb2a03f..60f26ca 100644
--- a/irc.py
+++ b/irc.py
@@ -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 = []