Merge lp:~manishsinha/zeitgeist/new-blacklist-implementation into lp:zeitgeist/0.1

Status: Merged
Merged at revision: 1711
Proposed branch: lp:~manishsinha/zeitgeist/new-blacklist-implementation
Merge into: lp:zeitgeist/0.1
Diff against target: 251 lines (+103/-43)
2 files modified
_zeitgeist/engine/extensions/blacklist.py (+86/-27)
test/blacklist-test.py (+17/-16)
To merge this branch: bzr merge lp:~manishsinha/zeitgeist/new-blacklist-implementation
Reviewer Review Type Date Requested Status
Markus Korn Approve
Mikkel Kamstrup Erlandsen Approve
Review via email: mp+56469@code.launchpad.net

Description of the change

Implements the new Blacklist API as per http://wiki.zeitgeist-project.com/Zeitgeist_Blacklist

* All methods implemented
* Add signals implemented
* Unit Tests updated accordingly

To post a comment you must log in.
Revision history for this message
Mikkel Kamstrup Erlandsen (kamstrup) wrote :

35 + PUBLIC_METHODS = ["add_blacklist", "get_blacklist"]

Maybe add remove_blacklist here as well?

75 + def add_blacklist(self, event_id, event_template):
85 + def remove_blacklist(self, event_id):
104 + def AddTemplate(self, event_id, event_template):

Should the first arg not be named blacklist_id or?

143 + def RemoveTemplates(self, event_id):

Again, arg should be blacklist_id? Also is it not RemoveTemplate (ie. not plural)

review: Needs Fixing
1703. By Manish Sinha (मनीष सिन्हा)

Updated as per Mikkel's review at https://code.launchpad.net/~manishsinha/zeitgeist/new-blacklist-implementation/+merge/56469

* Added remove_template in PUBLIC_METHODS
* Renamed RemoveTemplates to RemoveTemplate
* Changed all the occourance of event_id with blacklist_id

Revision history for this message
Mikkel Kamstrup Erlandsen (kamstrup) wrote :

Ok, I am happy now. Top notch work Manish! And thanks for persevering this long haul :-)

(I think we should clarify the DBus protocol in the docstring a bit so other languages can implement it, but that can come later)

review: Approve
1704. By Manish Sinha (मनीष सिन्हा)

Updated docstrings for blacklist extension

1705. By Manish Sinha (मनीष सिन्हा)

Storing blacklists as Event object instead of simple objects to reduce time taken for each comparison

1706. By Manish Sinha (मनीष सिन्हा)

Merged with lp:zeitgeist

1707. By Manish Sinha (मनीष सिन्हा)

Merged from lp:zeitgeist

1708. By Manish Sinha (मनीष सिन्हा)

Merge fromlp:zeitgeist for 3rd time

Revision history for this message
Markus Korn (thekorn) wrote :

I'm approving too, thanks for working on this manish.
With one small note: if some user already has some blacklists defined in the old pickle format this blacklists will simply ignored by this change. But Seif, manish and me agreed on IRC that this is ok, we should just make sure to tell our users about it once this gets released (add a comment about it to NEWS)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '_zeitgeist/engine/extensions/blacklist.py'
2--- _zeitgeist/engine/extensions/blacklist.py 2011-01-17 15:54:47 +0000
3+++ _zeitgeist/engine/extensions/blacklist.py 2011-04-07 08:42:07 +0000
4@@ -3,6 +3,7 @@
5 # Zeitgeist
6 #
7 # Copyright © 2009 Mikkel Kamstrup Erlandsen <mikkel.kamstrup@gmail.com>
8+# © 2011 Manish Sinha <manishsinha@ubuntu.com>
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU Lesser General Public License as published by
12@@ -18,7 +19,7 @@
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14
15 import os
16-import pickle
17+import json
18 import dbus
19 import dbus.service
20 from xdg import BaseDirectory
21@@ -30,7 +31,7 @@
22
23 log = logging.getLogger("zeitgeist.blacklist")
24
25-CONFIG_FILE = os.path.join(constants.DATA_PATH, "blacklist.pickle")
26+CONFIG_FILE = os.path.join(constants.DATA_PATH, "blacklist.json")
27 BLACKLIST_DBUS_OBJECT_PATH = "/org/gnome/zeitgeist/blacklist"
28 BLACKLIST_DBUS_INTERFACE = "org.gnome.zeitgeist.Blacklist"
29
30@@ -47,7 +48,7 @@
31 :const:`/org/gnome/zeitgeist/blacklist` under the bus name
32 :const:`org.gnome.zeitgeist.Engine`.
33 """
34- PUBLIC_METHODS = ["set_blacklist", "get_blacklist"]
35+ PUBLIC_METHODS = ["add_blacklist", "remove_blacklist", "get_blacklist"]
36
37 def __init__ (self, engine):
38 Extension.__init__(self, engine)
39@@ -55,61 +56,119 @@
40 BLACKLIST_DBUS_OBJECT_PATH)
41 if os.path.exists(CONFIG_FILE):
42 try:
43- raw_blacklist = pickle.load(file(CONFIG_FILE))
44- self._blacklist = map(Event, raw_blacklist)
45+ pcl_file = open(CONFIG_FILE, "r")
46+ self._blacklist = {}
47+ blacklist = json.load(pcl_file)
48+ [self._blacklist.setdefault(key, Event(blacklist[key])) \
49+ for key in blacklist]
50 log.debug("Loaded blacklist config from %s"
51 % CONFIG_FILE)
52 except Exception, e:
53 log.warn("Failed to load blacklist config file %s: %s"\
54 % (CONFIG_FILE, e))
55- self._blacklist = []
56+ self._blacklist = {}
57 else:
58 log.debug("No existing blacklist config found")
59- self._blacklist = []
60+ self._blacklist = {}
61
62 def pre_insert_event(self, event, sender):
63- for tmpl in self._blacklist:
64+ for tmpl in self._blacklist.itervalues():
65 if event.matches_template(tmpl): return None
66 return event
67
68 # PUBLIC
69- def set_blacklist(self, event_templates):
70- self._blacklist = event_templates
71- map(Event._make_dbus_sendable, self._blacklist)
72-
73- out = file(CONFIG_FILE, "w")
74- pickle.dump(map(Event.get_plain, self._blacklist), out)
75- out.close()
76- log.debug("Blacklist updated: %s" % self._blacklist)
77+ def add_blacklist(self, blacklist_id, event_template):
78+ Event._make_dbus_sendable(event_template)
79+ self._blacklist[blacklist_id] = Event(event_template)
80+
81+ out = file(CONFIG_FILE, "w")
82+ json.dump(self._blacklist, out)
83+ out.close()
84+ log.debug("Blacklist added: %s" % self._blacklist)
85+
86+ # PUBLIC
87+ def remove_blacklist(self, blacklist_id):
88+ event_template = self._blacklist[blacklist_id]
89+ del self._blacklist[blacklist_id]
90+
91+ out = file(CONFIG_FILE, "w")
92+ json.dump(self._blacklist, out)
93+ out.close()
94+ log.debug("Blacklist deleted: %s" % self._blacklist)
95+
96+ return event_template
97
98 # PUBLIC
99 def get_blacklist(self):
100 return self._blacklist
101
102 @dbus.service.method(BLACKLIST_DBUS_INTERFACE,
103- in_signature="a("+constants.SIG_EVENT+")")
104- def SetBlacklist(self, event_templates):
105+ in_signature="s("+constants.SIG_EVENT+")")
106+ def AddTemplate(self, blacklist_id, event_template):
107 """
108- Set the blacklist to :const:`event_templates`. Events
109+ Set the blacklist to :const:`event_template`. Events
110 matching any these templates will be blocked from insertion
111 into the log. It is still possible to find and look up events
112 matching the blacklist which was inserted before the blacklist
113 banned them.
114
115- :param event_templates: A list of
116+ :param blacklist_id: A string identifier for a blacklist template
117+
118+ :param event_template: An object of
119 :class:`Events <zeitgeist.datamodel.Event>`
120 """
121- tmp = map(Event, event_templates)
122- self.set_blacklist(tmp)
123+ tmp = Event(event_template)
124+ self.add_blacklist(blacklist_id, tmp)
125+ self.TemplateAdded(blacklist_id, event_template)
126
127 @dbus.service.method(BLACKLIST_DBUS_INTERFACE,
128 in_signature="",
129- out_signature="a("+constants.SIG_EVENT+")")
130- def GetBlacklist(self):
131+ out_signature="a{s("+constants.SIG_EVENT+")}")
132+ def GetTemplates(self):
133 """
134- Get the current blacklist templates.
135+ Get the current list of blacklist templates.
136
137- :returns: A list of
138- :class:`Events <zeitgeist.datamodel.Event>`
139+ :returns: An dictionary of { string ,
140+ :class:`Events <zeitgeist.datamodel.Event>` }
141 """
142 return self.get_blacklist()
143+
144+ @dbus.service.method(BLACKLIST_DBUS_INTERFACE,
145+ in_signature="s",
146+ out_signature="")
147+ def RemoveTemplate(self, blacklist_id):
148+ """
149+ Remove a blacklist template which is identified by the
150+ blacklist_id provided
151+
152+ :param blacklist_id: A string identifier for a blacklist template
153+
154+ """
155+ try:
156+ event_template = self.remove_blacklist(blacklist_id)
157+ self.TemplateRemoved(blacklist_id, event_template)
158+ except KeyError:
159+ log.debug("Blacklist %s not found " % blacklist_id)
160+
161+ @dbus.service.signal(BLACKLIST_DBUS_INTERFACE,
162+ signature="s("+constants.SIG_EVENT+")")
163+ def TemplateAdded(self, blacklist_id, event_template):
164+ """
165+ Raised when a template is added
166+
167+ :param blacklist_id: The Id of the Blacklist template used for
168+ setting the Blacklist
169+ :param event_template: The blacklist template which was set
170+ """
171+ pass
172+
173+ @dbus.service.signal(BLACKLIST_DBUS_INTERFACE,
174+ signature="s("+constants.SIG_EVENT+")")
175+ def TemplateRemoved(self, blacklist_id, event_template):
176+ """
177+ Raised when a template is deleted
178+
179+ :param blacklist_id: The Id of the Blacklist template which was deleted
180+ :param event_template: The blacklist template which was deleted
181+ """
182+ pass
183
184=== modified file 'test/blacklist-test.py'
185--- test/blacklist-test.py 2010-09-22 19:21:19 +0000
186+++ test/blacklist-test.py 2011-04-07 08:42:07 +0000
187@@ -29,18 +29,19 @@
188 self.blacklist = dbus.Interface(obj, "org.gnome.zeitgeist.Blacklist")
189
190 def testClear(self):
191- self.blacklist.SetBlacklist([])
192- empty = self.blacklist.GetBlacklist()
193- self.assertEquals(empty, [])
194+ allTemplates = self.blacklist.GetTemplates()
195+ [self.blacklist.RemoveTemplate(key) for key in allTemplates.iterkeys()]
196+ newAllTemplates = self.blacklist.GetTemplates()
197+ self.assertEquals(len(newAllTemplates), 0)
198
199 def testSetOne(self):
200 orig = Event.new_for_values(interpretation=Interpretation.ACCESS_EVENT,
201 subject_uri="http://nothingtoseehere.gov")
202- self.blacklist.SetBlacklist([orig])
203- result = map(Event, self.blacklist.GetBlacklist())
204+ self.blacklist.AddTemplate("Foobar", orig)
205+ res = self.blacklist.GetTemplates()
206
207- self.assertEquals(len(result), 1)
208- result = result[0]
209+ self.assertEquals(len(res), 1)
210+ result = Event(res["Foobar"])
211 self.assertEquals(result.manifestation, "")
212 self.assertEquals(result.interpretation, Interpretation.ACCESS_EVENT)
213 self.assertEquals(len(result.subjects), 1)
214@@ -49,18 +50,17 @@
215
216 def testApplyBlacklist(self):
217 self.testSetOne()
218- ev = Event()
219- ev.interpretation = Interpretation.ACCESS_EVENT
220+ ev = Event.new_for_values(interpretation=Interpretation.ACCESS_EVENT,
221+ subject_uri="http://nothingtoseehere.gov")
222 ev.manifestation = Manifestation.USER_ACTIVITY
223 ev.actor = "app.//foo.desktop"
224- subj = ev.append_subject()
225- subj.uri = "http://nothingtoseehere.gov"
226+
227 inserted_ids = self.insertEventsAndWait([ev])
228 self.assertEquals(1, len(inserted_ids))
229- self.assertEquals(0, inserted_ids[0])
230+ self.assertEquals(0, int(inserted_ids[0]))
231
232 # Now change the event to pass the blacklist
233- subj.uri = "htpp://totallyvaliduri.com"
234+ ev.get_subjects()[0].uri = "htpp://totallyvaliduri.com"
235 inserted_ids = self.insertEventsAndWait([ev])
236 self.assertEquals(1, len(inserted_ids))
237 self.assertTrue(0 != inserted_ids[0])
238@@ -73,9 +73,10 @@
239 del self.blacklist
240 iface = ZeitgeistDBusInterface()
241 blacklist = iface.get_extension("Blacklist", "blacklist")
242- blacklist.SetBlacklist([])
243- empty = blacklist.GetBlacklist()
244- self.assertEquals(empty, [])
245+ allTemplates = blacklist.GetTemplates()
246+ [blacklist.RemoveTemplate(key) for key in allTemplates.iterkeys()]
247+ newAllTemplates = blacklist.GetTemplates()
248+ self.assertEquals(len(newAllTemplates), 0)
249
250 if __name__ == "__main__":
251 unittest.main()

Subscribers

People subscribed via source and target branches