Commits

Ali Afshar  committed 9076a5b

MasterProxy to aggregate many proxies by key

  • Participants
  • Parent commits 116c987

Comments (0)

Files changed (2)

File pygtkhelpers/proxy.py

         raise KeyError('There is no proxy type registered for %r' % widget)
     return proxy_type(widget)
 
+
+class MasterProxy(gobject.GObject):
+    """A controller to handle multiple proxies, and sub-masters
+
+    A MasterProxy is a bridge to reduce multiple proxies and sub-masters to a
+    single signal based on the key of the individual proxies.
+    """
+
+    gsignal('changed', object, str, object)
+
+    def __init__(self):
+        gobject.GObject.__init__(self)
+
+    def add_proxy(self, name, proxy):
+        """Add a proxy to this master
+
+        :param name: The name or key of the proxy, which will be emitted with
+                     the changed signal
+        :param proxy: The proxy instance to add
+        """
+        proxy.connect('changed', self._on_proxy_changed, name)
+
+    def add_proxy_for(self, name, widget):
+        """Create a proxy for a widget and add it to this master
+
+        :param name: The name or key of the proxy, which will be emitted with
+                     the changed signal
+        :param widget: The widget to create a proxy for
+        """
+        proxy = proxy_for(widget)
+        self.add_proxy(name, proxy)
+
+    def add_master(self, master):
+        """Add an existing master to this master and proxy its signals
+
+        :param master: The MasterProxy instance to add
+        """
+        master.connect('changed', self._on_master_changed)
+
+    def _on_proxy_changed(self, proxy, value, name):
+        self.emit('changed', proxy, name, value)
+
+    def _on_master_changed(self, master, proxy, value, name):
+        self.emit('changed', proxy, name, value)
+

File tests/test_masterproxy.py

+
+import gtk
+
+from pygtkhelpers.test import CheckCalled
+from pygtkhelpers.utils import refresh_gui
+from pygtkhelpers.proxy import MasterProxy, GtkEntryProxy
+
+def test_add_proxy():
+    m = MasterProxy()
+    e = gtk.Entry()
+    p = GtkEntryProxy(e)
+    m.add_proxy('foo', p)
+    check = CheckCalled(m, 'changed')
+    e.set_text('a')
+    refresh_gui()
+    assert check.called_count == 1
+    p.update('b')
+    refresh_gui()
+    assert check.called_count == 2
+
+def test_add_proxy_for():
+    m = MasterProxy()
+    e = gtk.Entry()
+    m.add_proxy_for('foo', e)
+    check = CheckCalled(m, 'changed')
+    e.set_text('a')
+    refresh_gui()
+    assert check.called_count == 1
+
+def add_master():
+    m = MasterProxy()
+    e = gtk.Entry()
+    m.add_proxy_for('foo', e)
+    m2 = MasterProxy()
+    m2.add_master(m)
+    check = CheckCalled(m2, 'changed')
+    e.set_text('a')
+    assert check.called_count == 1
+