[kupfer: 24/51] Split helplib into obj.helplib and weaklib



commit d4cd7b488cca25472caee15854903cb0ca48b533
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Sat Jan 9 20:05:02 2010 +0100

    Split helplib into obj.helplib and weaklib

 kupfer/obj/helplib.py           |   94 -------------------------------------
 kupfer/plugin/core/selection.py |    2 +-
 kupfer/scheduler.py             |    2 +-
 kupfer/weaklib.py               |   97 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 99 insertions(+), 96 deletions(-)
---
diff --git a/kupfer/obj/helplib.py b/kupfer/obj/helplib.py
index 4f50669..992b2af 100644
--- a/kupfer/obj/helplib.py
+++ b/kupfer/obj/helplib.py
@@ -5,8 +5,6 @@ This module is a part of the program Kupfer, see the main program file for
 more information.
 """
 
-import weakref
-
 import gio
 
 class PicklingHelperMixin (object):
@@ -84,94 +82,6 @@ class FilesystemWatchMixin (object):
 				self.monitor_include_file(file1)):
 			self.mark_for_update()
 
-class WeakCallback (object):
-	"""A Weak Callback object that will keep a reference to
-	the connecting object with weakref semantics.
-
-	This allows object A to pass a callback method to object S,
-	without object S keeping A alive.
-	"""
-	def __init__(self, mcallback):
-		"""Create a new Weak Callback calling the method @mcallback"""
-		obj = mcallback.im_self
-		attr = mcallback.im_func.__name__
-		self.wref = weakref.ref(obj, self.object_deleted)
-		self.callback_attr = attr
-		self.token = None
-
-	def __call__(self, *args, **kwargs):
-		obj = self.wref()
-		if obj:
-			attr = getattr(obj, self.callback_attr)
-			attr(*args, **kwargs)
-		else:
-			self.default_callback(*args, **kwargs)
-
-	def default_callback(self, *args, **kwargs):
-		"""Called instead of callback when expired"""
-		pass
-
-	def object_deleted(self, wref):
-		"""Called when callback expires"""
-		pass
-
-class DbusWeakCallback (WeakCallback):
-	"""
-	Will use @token if set as follows:
-		token.remove()
-	"""
-	def object_deleted(self, wref):
-		if self.token:
-			self.token.remove()
-			self.token = None
-
-def dbus_signal_connect_weakly(bus, signal, mcallback, **kwargs):
-	"""
-	Connect method @mcallback to dbus signal using a weak callback
-
-	Connect to @signal on @bus, passing on all keyword arguments
-	"""
-	weak_cb = DbusWeakCallback(mcallback)
-	weak_cb.token = bus.add_signal_receiver(weak_cb, signal, **kwargs)
-
-class GobjectWeakCallback (WeakCallback):
-	"""
-	Will use @token if set as follows:
-		sender.disconnect(token)
-	"""
-	__senders = {}
-
-	def object_deleted(self, wref):
-		sender = self.__senders.pop(self.token, None)
-		if sender:
-			sender.disconnect(self.token)
-
-	@classmethod
-	def _connect(cls, sender, signal, mcallback, *user_args):
-		# We save references to the sender in a class variable,
-		# this is the only way to have it accessible when obj expires.
-		wc = cls(mcallback)
-		wc.token = sender.connect(signal, wc, *user_args)
-		cls.__senders[wc.token] = sender
-
-def gobject_connect_weakly(sender, signal, mcallback, *user_args):
-	"""Connect weakly to GObject @sender's @signal,
-	with a callback method @mcallback
-
-	>>> import gtk
-	>>> btn = gtk.Button()
-	>>> class Handler (object):
-	...   def handle(self): pass
-	...   def __del__(self): print "deleted"
-	...
-	>>> h = Handler()
-	>>> gobject_connect_weakly(btn, "clicked", h.handle)
-	>>> del h
-	deleted
-	>>>
-	"""
-	GobjectWeakCallback._connect(sender, signal, mcallback, *user_args)
-
 def reverse_action(action, rank=0):
 	"""Return a reversed version a three-part action
 
@@ -209,7 +119,3 @@ def reverse_action(action, rank=0):
 			return None
 	ReverseAction.__name__ = "Reverse" + action.__name__
 	return ReverseAction
-
-if __name__ == '__main__':
-	import doctest
-	doctest.testmod()
diff --git a/kupfer/plugin/core/selection.py b/kupfer/plugin/core/selection.py
index 1fd4eb3..f0e2366 100644
--- a/kupfer/plugin/core/selection.py
+++ b/kupfer/plugin/core/selection.py
@@ -2,7 +2,7 @@ import gtk
 
 from kupfer.objects import Source, Leaf
 from kupfer.objects import TextLeaf, SourceLeaf
-from kupfer.helplib import gobject_connect_weakly
+from kupfer.weaklib import gobject_connect_weakly
 from kupfer import kupferstring
 
 __kupfer_name__ = _("Selected Text")
diff --git a/kupfer/scheduler.py b/kupfer/scheduler.py
index 9efe61e..eaf821f 100644
--- a/kupfer/scheduler.py
+++ b/kupfer/scheduler.py
@@ -2,7 +2,7 @@
 import gobject
 
 from kupfer import pretty
-from kupfer.obj.helplib import gobject_connect_weakly
+from kupfer.weaklib import gobject_connect_weakly
 
 _scheduler = None
 
diff --git a/kupfer/weaklib.py b/kupfer/weaklib.py
new file mode 100644
index 0000000..e69f602
--- /dev/null
+++ b/kupfer/weaklib.py
@@ -0,0 +1,97 @@
+"""
+This module is a part of the program Kupfer, see the main program file for
+more information.
+"""
+import weakref
+
+class WeakCallback (object):
+	"""A Weak Callback object that will keep a reference to
+	the connecting object with weakref semantics.
+
+	This allows object A to pass a callback method to object S,
+	without object S keeping A alive.
+	"""
+	def __init__(self, mcallback):
+		"""Create a new Weak Callback calling the method @mcallback"""
+		obj = mcallback.im_self
+		attr = mcallback.im_func.__name__
+		self.wref = weakref.ref(obj, self.object_deleted)
+		self.callback_attr = attr
+		self.token = None
+
+	def __call__(self, *args, **kwargs):
+		obj = self.wref()
+		if obj:
+			attr = getattr(obj, self.callback_attr)
+			attr(*args, **kwargs)
+		else:
+			self.default_callback(*args, **kwargs)
+
+	def default_callback(self, *args, **kwargs):
+		"""Called instead of callback when expired"""
+		pass
+
+	def object_deleted(self, wref):
+		"""Called when callback expires"""
+		pass
+
+class DbusWeakCallback (WeakCallback):
+	"""
+	Will use @token if set as follows:
+		token.remove()
+	"""
+	def object_deleted(self, wref):
+		if self.token:
+			self.token.remove()
+			self.token = None
+
+def dbus_signal_connect_weakly(bus, signal, mcallback, **kwargs):
+	"""
+	Connect method @mcallback to dbus signal using a weak callback
+
+	Connect to @signal on @bus, passing on all keyword arguments
+	"""
+	weak_cb = DbusWeakCallback(mcallback)
+	weak_cb.token = bus.add_signal_receiver(weak_cb, signal, **kwargs)
+
+class GobjectWeakCallback (WeakCallback):
+	"""
+	Will use @token if set as follows:
+		sender.disconnect(token)
+	"""
+	__senders = {}
+
+	def object_deleted(self, wref):
+		sender = self.__senders.pop(self.token, None)
+		if sender:
+			sender.disconnect(self.token)
+
+	@classmethod
+	def _connect(cls, sender, signal, mcallback, *user_args):
+		# We save references to the sender in a class variable,
+		# this is the only way to have it accessible when obj expires.
+		wc = cls(mcallback)
+		wc.token = sender.connect(signal, wc, *user_args)
+		cls.__senders[wc.token] = sender
+
+def gobject_connect_weakly(sender, signal, mcallback, *user_args):
+	"""Connect weakly to GObject @sender's @signal,
+	with a callback method @mcallback
+
+	>>> import gtk
+	>>> btn = gtk.Button()
+	>>> class Handler (object):
+	...   def handle(self): pass
+	...   def __del__(self): print "deleted"
+	...
+	>>> h = Handler()
+	>>> gobject_connect_weakly(btn, "clicked", h.handle)
+	>>> del h
+	deleted
+	>>>
+	"""
+	GobjectWeakCallback._connect(sender, signal, mcallback, *user_args)
+
+if __name__ == '__main__':
+	import doctest
+	doctest.testmod()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]