← Back to team overview

zeitgeist team mailing list archive

[Branch ~zeitgeist/zeitgeist/bluebird] Rev 226: Blacklist: implement D-Bus methods.

 

------------------------------------------------------------
revno: 226
committer: Siegfried-Angel Gevatter Pujals <siegfried@xxxxxxxxxxxx>
branch nick: bluebird
timestamp: Mon 2011-09-05 15:09:08 +0200
message:
  Blacklist: implement D-Bus methods.
added:
  test/dbus/blacklist-test.py
modified:
  extensions/blacklist.vala
  test/dbus/dsr-test.py
  test/dbus/testutils.py


--
lp:~zeitgeist/zeitgeist/bluebird
https://code.launchpad.net/~zeitgeist/zeitgeist/bluebird

Your team Zeitgeist Framework Team is subscribed to branch lp:~zeitgeist/zeitgeist/bluebird.
To unsubscribe from this branch go to https://code.launchpad.net/~zeitgeist/zeitgeist/bluebird/+edit-subscription
=== modified file 'extensions/blacklist.vala'
--- extensions/blacklist.vala	2011-09-05 10:27:02 +0000
+++ extensions/blacklist.vala	2011-09-05 13:09:08 +0000
@@ -28,17 +28,17 @@
     [DBus (name = "org.gnome.zeitgeist.Blacklist")]
     public interface RemoteBlacklist: Object
     {
-        public abstract void add_template (string blacklist_id,
+        public abstract void add_template (string template_id,
             [DBus (signature = "(asaasay)")] Variant event_template)
             throws Error;
         [DBus (signature = "a{s(asaasay)}")]
         public abstract Variant get_templates () throws Error;
-        public abstract void remove_template (string blacklist_id)
+        public abstract void remove_template (string template_id)
             throws Error;
 
-        public signal void template_added (string blacklist_id,
+        public signal void template_added (string template_id,
             [DBus (signature = "s(asaasay)")] Variant event_template);
-        public signal void template_removed (string blacklist_id,
+        public signal void template_removed (string template_id,
             [DBus (signature = "s(asassay)")] Variant event_template);
     }
 
@@ -95,33 +95,52 @@
             // FIXME: write to file.
         }
 
-        public GenericArray<Event?> pre_insert_events (
-            GenericArray<Event?> events, BusName sender)
+        public override void pre_insert_events (GenericArray<Event?> events,
+            BusName? sender)
         {
             // FIXME: do template matching...
             // for event in events:
             //     for tmpl in blacklist:
             //         if event.matches_template(tmpl): event = null
-            return events;
         }
 
-        public void add_template (string blacklist_id, Variant event_template)
+        public void add_template (string template_id, Variant event_template)
+            throws EngineError
         {
             Event template = new Event.from_variant (event_template);
-            blacklist.insert (blacklist_id, template);
+            blacklist.insert (template_id, template);
+            debug ("Added blacklist template: %s", template_id);
+            template_added (template_id, event_template);
             flush ();
         }
 
-        public void remove_template (string blacklist_id)
+        public void remove_template (string template_id)
         {
-            Event template = blacklist.lookup (blacklist_id);
-            blacklist.remove (blacklist_id);
+            Event event_template = blacklist.lookup (template_id);
+            if (blacklist.remove (template_id))
+                debug ("Removed blacklist template: %s", template_id);
+            else
+                debug ("Blacklist template \"%s\" not found.", template_id);
+            template_removed (template_id, event_template.to_variant ());
             flush ();
         }
 
         public Variant get_templates ()
         {
-            return null; //blacklist;
+            var vb = new VariantBuilder (new VariantType ("a{s(asaasay)}"));
+            {
+                var iter = HashTableIter<string, Event> (blacklist);
+                string template_id;
+                Event event_template;
+                while (iter.next (out template_id, out event_template))
+                {
+                    vb.open (new VariantType ("{s(asaasay)}"));
+                    vb.add ("s", template_id);
+                    vb.add_value (event_template.to_variant ());
+                    vb.close ();
+                }
+            }
+            return vb.end ();
         }
 
     }

=== added file 'test/dbus/blacklist-test.py'
--- test/dbus/blacklist-test.py	1970-01-01 00:00:00 +0000
+++ test/dbus/blacklist-test.py	2011-09-05 13:09:08 +0000
@@ -0,0 +1,151 @@
+#! /usr/bin/python
+# -.- coding: utf-8 -.-
+
+# remote-test.py
+#
+# Copyright © 2009 Mikkel Kamstrup Erlandsen <mikkel.kamstrup@xxxxxxxxx>
+# Copyright © 2010 Markus Korn <thekorn@xxxxxx>
+# Copyright © 2010 Siegfried-Angel Gevatter Pujals <siegfried@xxxxxxxxxxxx>
+# Copyright © 2011 Manish Sinha <manishsinha@xxxxxxxxxx>
+# Copyright © 2011 Collabora Ltd.
+#             By Siegfried-Angel Gevatter Pujals <siegfried@xxxxxxxxxxxx>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, either version 2.1 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Update python path to use local zeitgeist module
+import sys
+import os
+import unittest
+import gobject
+
+sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
+from zeitgeist.client import ZeitgeistDBusInterface
+from zeitgeist.datamodel import *
+from testutils import RemoteTestCase
+
+class BlacklistTest(RemoteTestCase):
+
+	def __init__(self, methodName):
+		super(BlacklistTest, self).__init__(methodName)
+		self.blacklist = None
+
+	def setUp(self):
+		# lazy import to get a chance to use the private bus
+		import dbus
+		
+		# We set up the connection lazily in order to wait for the
+		# engine to come up
+		super(BlacklistTest, self).setUp()
+		obj = dbus.SessionBus().get_object("org.gnome.zeitgeist.Engine",
+			"/org/gnome/zeitgeist/blacklist")
+		self.blacklist = dbus.Interface(obj, "org.gnome.zeitgeist.Blacklist")
+
+	def testClear(self):
+		# Insert a blacklist template
+		self.blacklist.AddTemplate("unicorns",
+			Event.new_for_values(subject_uri="a"))
+		self.assertTrue(len(self.blacklist.GetTemplates()) > 0)
+		
+		# Now remove all existing templates...
+		allTemplates = self.blacklist.GetTemplates()
+		[self.blacklist.RemoveTemplate(key) for key in allTemplates.iterkeys()]
+		
+		# And ensure that they are indeed gone.
+		self.assertEquals(len(self.blacklist.GetTemplates()), 0)
+
+	def testSetOne(self):
+		orig = Event.new_for_values(interpretation=Interpretation.ACCESS_EVENT,
+			subject_uri="http://nothingtoseehere.gov";)
+		self.blacklist.AddTemplate("Foobar", orig)
+		res = self.blacklist.GetTemplates()
+		
+		self.assertEquals(len(res), 1)
+		self.assertEventsEqual(orig, Event(res["Foobar"]))
+
+	def testApplyBlacklist(self):
+		self.testSetOne()
+		ev = Event.new_for_values(interpretation=Interpretation.ACCESS_EVENT,
+			subject_uri="http://nothingtoseehere.gov";)
+		ev.manifestation = Manifestation.USER_ACTIVITY
+		ev.actor = "app.//foo.desktop"
+
+		inserted_ids = self.insertEventsAndWait([ev])
+		self.assertEquals(1, len(inserted_ids))
+		self.assertEquals(0, int(inserted_ids[0]))
+
+		# Now change the event to pass the blacklist
+		ev.get_subjects()[0].uri = "htpp://totallyvaliduri.com"
+		inserted_ids = self.insertEventsAndWait([ev])
+		self.assertEquals(1, len(inserted_ids))
+		self.assertTrue(0 != inserted_ids[0])
+
+	def _get_blacklist_iface(self):
+		"""
+		Create a blacklist interface using the get_extension() method
+		from client.py.
+		"""
+		del self.blacklist
+		iface = ZeitgeistDBusInterface()
+		blacklist = iface.get_extension("Blacklist", "blacklist")
+		return blacklist
+
+	def testBlacklistUsingClientDBusInterface(self):
+		"""
+		Ensure that get_extension() from client.py method works correctly.
+		"""
+		blacklist = self._get_blacklist_iface()
+		allTemplates = blacklist.GetTemplates()
+		[blacklist.RemoveTemplate(key) for key in allTemplates.iterkeys()]
+		newAllTemplates = blacklist.GetTemplates()
+		self.assertEquals(len(newAllTemplates), 0)
+
+	def testBlacklistSignals(self):
+		self.blacklist = self._get_blacklist_iface()
+		mainloop = self.create_mainloop()
+
+		template1 = Event.new_for_values(
+			interpretation=Interpretation.ACCESS_EVENT,
+			subject_uri="http://nothingtoseehere.gov";)
+
+		global hit
+		hit = 0
+
+		def cb_added(template_id, event_template):
+			global hit
+			self.assertEquals(hit, 0)
+			hit = 1
+			self.assertEquals(template_id, "TestTemplate")
+			self.assertEventsEqual(template1, event_template)
+
+		def cb_removed(template_id, event_template):
+			global hit
+			self.assertEquals(hit, 1)
+			hit = 2
+			self.assertEquals(template_id, "TestTemplate")
+			self.assertEventsEqual(template1, event_template)
+			mainloop.quit()
+
+		# Connect to signals
+		self.blacklist.connect('TemplateAdded', cb_added)
+		self.blacklist.connect('TemplateRemoved', cb_removed)
+
+		def launch_tests():
+			self.blacklist.AddTemplate("TestTemplate", template1)
+			self.blacklist.RemoveTemplate("TestTemplate")
+		gobject.idle_add(launch_tests)
+
+		mainloop.run()
+
+if __name__ == "__main__":
+	unittest.main()

=== modified file 'test/dbus/dsr-test.py'
--- test/dbus/dsr-test.py	2011-09-05 09:53:08 +0000
+++ test/dbus/dsr-test.py	2011-09-05 13:09:08 +0000
@@ -199,8 +199,8 @@
 		
 		# Retrieve a data-source from an id that has not been registered
 		self.assertRaises(DBusException,
-				  self.client._registry.GetDataSourceFromId,
-				  self._ds2[0])
+			self.client._registry.GetDataSourceFromId,
+			self._ds2[0])
 
 	def testDataSourceSignals(self):
 		mainloop = self.create_mainloop()

=== modified file 'test/dbus/testutils.py'
--- test/dbus/testutils.py	2011-08-21 22:45:35 +0000
+++ test/dbus/testutils.py	2011-09-05 13:09:08 +0000
@@ -346,8 +346,8 @@
 		return popo
 	
 	def assertEventsEqual(self, ev1, ev2):
-		ev1 = self.get_plain_event(ev1)
-		ev2 = self.get_plain_event(ev2)
+		ev1 = self.get_plain_event(Event(ev1))
+		ev2 = self.get_plain_event(Event(ev2))
 		if ev1 is not NULL_EVENT and ev2 is not NULL_EVENT:
 			if (ev1[0][0] and not ev2[0][0]) or (ev2[0][0] and not ev1[0][0]):
 				ev1[0][0] = ev2[0][0] = "" # delete IDs