← Back to team overview

zeitgeist team mailing list archive

[Merge] lp:~zeitgeist/zeitgeist/bug695363 into lp:zeitgeist

 

Siegfried Gevatter has proposed merging lp:~zeitgeist/zeitgeist/bug695363 into lp:zeitgeist.

Requested reviews:
  Zeitgeist Framework Team (zeitgeist)
Related bugs:
  #695363 Python API doesn't expose DataSourceRegistry's enabled status
  https://bugs.launchpad.net/bugs/695363

For more details, see:
https://code.launchpad.net/~zeitgeist/zeitgeist/bug695363/+merge/48950

Expose DataSourceRegistry's enabled status and callback in the Python API.
-- 
https://code.launchpad.net/~zeitgeist/zeitgeist/bug695363/+merge/48950
Your team Zeitgeist Framework Team is requested to review the proposed merge of lp:~zeitgeist/zeitgeist/bug695363 into lp:zeitgeist.
=== modified file 'test/remote-test.py'
--- test/remote-test.py	2010-12-29 15:31:26 +0000
+++ test/remote-test.py	2011-02-08 17:24:45 +0000
@@ -447,6 +447,9 @@
 		self.assertEquals(len(datasources), 1)
 		self._assertDataSourceEquals(datasources[0], self._ds2)
 	
+	def testRegisterDataSourceWithCallback(self):
+		self.client.register_data_source(*self._ds1, enabled_callback=lambda x: True)
+	
 	def testRegisterDataSources(self):
 		# Insert two data-sources
 		self.client._registry.RegisterDataSource(*self._ds1)
@@ -485,8 +488,21 @@
 		ds = list(self.client._registry.GetDataSources())[0]
 		self.assertEquals(ds[DataSource.Enabled], True)
 	
+	def _create_mainloop(self):
+		mainloop = gobject.MainLoop()
+		
+		def cb_timeout():
+			mainloop.quit()
+			self.fail("Timed out -- operations not completed in reasonable time.")
+		
+		# Add an arbitrary timeout so this test won't block if it fails
+		gobject.timeout_add_seconds(30, cb_timeout)
+		
+		return mainloop
+	
 	def testDataSourceSignals(self):
-		mainloop = gobject.MainLoop()
+		mainloop = self._create_mainloop()
+		
 		global hit
 		hit = 0
 		
@@ -514,10 +530,6 @@
 		#	self.assertEquals(hit, 3)
 		#	mainloop.quit()
 		
-		def cb_timeout():
-			mainloop.quit()
-			self.fail("Timed out -- operations not completed in 1 minute.")
-		
 		# Connect to signals
 		self.client._registry.connect('DataSourceRegistered', cb_registered)
 		self.client._registry.connect('DataSourceEnabled', cb_enabled)
@@ -526,11 +538,40 @@
 		# Register data-source, disable it, enable it again
 		gobject.idle_add(self.testSetDataSourceEnabled)
 		
-		# Add an arbitrary timeout so this test won't block if it fails
-		gobject.timeout_add_seconds(30, cb_timeout)
-		
-		mainloop.run()
-
+		mainloop.run()
+	
+	def testRegisterDataSourceEnabledCallbackOnRegister(self):
+		mainloop = self._create_mainloop()
+		
+		def callback(enabled):
+			mainloop.quit()
+		self.client.register_data_source(*self._ds1, enabled_callback=callback)
+		
+		mainloop.run()
+	
+	def testRegisterDataSourceEnabledCallbackOnChange(self):
+		mainloop = self._create_mainloop()
+		global hit
+		hit = 0
+		
+		# Register a callback
+		def callback(enabled):
+			global hit
+			if hit == 0:
+				# Register callback
+				hit = 1
+			elif hit == 1:
+				# Disable callback
+				mainloop.quit()
+			else:
+				self.fail("Unexpected number of signals: %d." % hit)
+		self.client.register_data_source(*self._ds1)
+		self.client.set_data_source_enabled_callback(self._ds1[0], callback)
+		
+		# Disable the data-source
+		self.client._registry.SetDataSourceEnabled(self._ds1[0], False)
+		
+		mainloop.run()
 
 class ZeitgeistRemotePropertiesTest(testutils.RemoteTestCase):
 	

=== modified file 'zeitgeist/client.py'
--- zeitgeist/client.py	2011-02-08 12:54:20 +0000
+++ zeitgeist/client.py	2011-02-08 17:24:45 +0000
@@ -2,7 +2,7 @@
 
 # Zeitgeist
 #
-# Copyright © 2009-2010 Siegfried-Angel Gevatter Pujals <rainct@xxxxxxxxxx>
+# Copyright © 2009-2011 Siegfried-Angel Gevatter Pujals <rainct@xxxxxxxxxx>
 # Copyright © 2009 Mikkel Kamstrup Erlandsen <mikkel.kamstrup@xxxxxxxxx>
 # Copyright © 2009 Markus Korn <thekorn@xxxxxx>
 #
@@ -876,7 +876,12 @@
 		                          reply_handler=reply_handler,
 		                          error_handler=error_handler)
 	
-	def register_data_source(self, unique_id, name, description, event_templates):
+	# Data-source related class variables
+	_data_sources = {}
+	_data_sources_callback_installed = False
+	
+	def register_data_source(self, unique_id, name, description,
+		event_templates, enabled_callback=None):
 		"""
 		Register a data-source as currently running. If the data-source was
 		already in the database, its metadata (name, description and
@@ -893,13 +898,58 @@
 		:param description: data-source description (may be translated)
 		:param event_templates: list of
 			:class:`Event <zeitgeist.datamodel.Event>` templates.
+		:param enabled_callback: method to call as response with the `enabled'
+			status of the data-source, and after that every time said status
+			is toggled. See set_data_source_enabled_callback() for more
+			information.
 		"""
-		# TODO: Make it possible to access the return value!
+		
+		self._data_sources[unique_id] = {'enabled': None, 'callback': None}
+		
+		if enabled_callback is not None:
+			self.set_data_source_enabled_callback(unique_id, enabled_callback)
+
+		def _data_source_enabled_cb(unique_id, enabled):
+			if unique_id not in self._data_sources:
+				return
+			self._data_sources[unique_id]['enabled'] = enabled
+			callback = self._data_sources[unique_id]['callback']
+			if callback is not None:
+				callback(enabled)
+		
+		def _data_source_register_cb(enabled):
+			_data_source_enabled_cb(unique_id, enabled)
+
+		if not self._data_sources_callback_installed:
+			self._registry.connect('DataSourceEnabled', _data_source_enabled_cb)
+			self._data_sources_callback_installed = True
+
 		self._registry.RegisterDataSource(unique_id, name, description,
 			event_templates,
-			reply_handler=self._void_reply_handler,
+			reply_handler=_data_source_register_cb,
 			error_handler=self._void_reply_handler) # Errors are ignored
 	
+	def set_data_source_enabled_callback(self, unique_id, enabled_callback):
+		"""
+		This method may only be used after having registered the given unique_id
+		with register_data_source before.
+		
+		It registers a method to be called whenever the `enabled' status of
+		the previously registered data-source changes.
+		
+		Remember that on some systems the DataSourceRegistry extension may be
+		disabled, in which case this method will have no effect.
+		"""
+		
+		assert unique_id in self._data_sources, \
+			'set_data_source_enabled_callback() called before ' \
+			'register_data_source()'
+		
+		assert callable(enabled_callback), \
+			'enabled_callback: expected a callable method'
+		
+		self._data_sources[unique_id]['callback'] = enabled_callback
+	
 	def _check_list_or_tuple(self, collection):
 		"""
 		Raise a ValueError unless 'collection' is a list or tuple


Follow ups