From 4fda0f9f4a00533cbc3f1e060411cd03ff2a3e1b Mon Sep 17 00:00:00 2001 From: Neil Williams Date: Sat, 22 Dec 2012 00:54:16 -0800 Subject: [PATCH] plugins: Add hook for watching Thing commits. --- r2/r2/lib/db/thing.py | 16 +++++++++------- r2/r2/lib/plugin.py | 7 +++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/r2/r2/lib/db/thing.py b/r2/r2/lib/db/thing.py index 5345bf530..e2a6bc60a 100644 --- a/r2/r2/lib/db/thing.py +++ b/r2/r2/lib/db/thing.py @@ -256,20 +256,20 @@ class DataThing(object): # (including the return in the above branch) begin() + to_set = self._dirties.copy() if keys: keys = tup(keys) - to_set = dict((k, self._dirties[k][1]) - for k in keys if self._dirties.has_key(k)) - else: - to_set = dict((k, v[1]) for k, v in self._dirties.iteritems()) + for key in to_set.keys(): + if key not in keys: + del to_set[key] data_props = {} thing_props = {} - for k, v in to_set.iteritems(): + for k, (old_value, new_value) in to_set.iteritems(): if k.startswith('_'): - thing_props[k[1:]] = v + thing_props[k[1:]] = new_value else: - data_props[k] = v + data_props[k] = new_value if data_props: self._set_data(self._type_id, @@ -297,6 +297,8 @@ class DataThing(object): if lock: lock.release() + g.plugins.on_thing_commit(self, to_set) + @classmethod def _load_multi(cls, need): need = tup(need) diff --git a/r2/r2/lib/plugin.py b/r2/r2/lib/plugin.py index 7d00690fa..0467dada9 100644 --- a/r2/r2/lib/plugin.py +++ b/r2/r2/lib/plugin.py @@ -78,6 +78,9 @@ class Plugin(object): def load_controllers(self): pass + def on_thing_commit(self, thing, changes): + pass + class PluginLoader(object): def __init__(self, plugin_names=None): @@ -137,3 +140,7 @@ class PluginLoader(object): def load_controllers(self): for plugin in self: plugin.load_controllers() + + def on_thing_commit(self, thing, changes): + for plugin in self: + plugin.on_thing_commit(thing, changes)