[PATCH] Add a context manager for executing code while UI signals are blocked.

Chris Lumens clumens at redhat.com
Wed Sep 24 17:45:56 UTC 2014


---
 pyanaconda/ui/gui/spokes/lib/resize.py |  8 ++++----
 pyanaconda/ui/gui/spokes/software.py   | 12 +++++-------
 pyanaconda/ui/gui/spokes/source.py     | 24 ++++++++++++------------
 pyanaconda/ui/gui/utils.py             | 13 ++++++++++++-
 4 files changed, 33 insertions(+), 24 deletions(-)

diff --git a/pyanaconda/ui/gui/spokes/lib/resize.py b/pyanaconda/ui/gui/spokes/lib/resize.py
index ffcc63c..ee9c322 100644
--- a/pyanaconda/ui/gui/spokes/lib/resize.py
+++ b/pyanaconda/ui/gui/spokes/lib/resize.py
@@ -26,7 +26,7 @@ from gi.repository import Gdk, Gtk
 
 from pyanaconda.i18n import _, C_, N_, P_
 from pyanaconda.ui.gui import GUIObject
-from pyanaconda.ui.gui.utils import escape_markup, timed_action
+from pyanaconda.ui.gui.utils import blockedHandler, escape_markup, timed_action
 from blivet.size import Size
 
 __all__ = ["ResizeDialog"]
@@ -261,9 +261,9 @@ class ResizeDialog(GUIObject):
         fivePercent = int(distance / 20)
         twentyPercent = int(distance / 5)
 
-        self._resizeSlider.handler_block_by_func(self.on_resize_value_changed)
-        self._resizeSlider.set_range(minSize, size)
-        self._resizeSlider.handler_unblock_by_func(self.on_resize_value_changed)
+        with blockedHandler(self._resizeSlider, self.on_resize_value_changed):
+            self._resizeSlider.set_range(minSize, size)
+
         self._resizeSlider.set_value(default_value)
 
         adjustment = self.builder.get_object("resizeAdjustment")
diff --git a/pyanaconda/ui/gui/spokes/software.py b/pyanaconda/ui/gui/spokes/software.py
index c60494c..7ab862c 100644
--- a/pyanaconda/ui/gui/spokes/software.py
+++ b/pyanaconda/ui/gui/spokes/software.py
@@ -30,7 +30,7 @@ from pyanaconda import constants, iutil
 from pyanaconda.ui.communication import hubQ
 from pyanaconda.ui.gui.spokes import NormalSpoke
 from pyanaconda.ui.gui.spokes.lib.detailederror import DetailedErrorDialog
-from pyanaconda.ui.gui.utils import gtk_action_wait, escape_markup
+from pyanaconda.ui.gui.utils import blockedHandler, gtk_action_wait, escape_markup
 from pyanaconda.ui.categories.software import SoftwareCategory
 
 import logging
@@ -410,9 +410,8 @@ class SoftwareSelectionSpoke(NormalSpoke):
         box = row.get_children()[0]
         button = box.get_children()[0]
 
-        button.handler_block_by_func(self.on_radio_button_toggled)
-        button.set_active(True)
-        button.handler_unblock_by_func(self.on_radio_button_toggled)
+        with blockedHandler(button, self.on_radio_button_toggled):
+            button.set_active(True)
 
         # Remove all the groups that were selected by the previously
         # selected environment.
@@ -436,9 +435,8 @@ class SoftwareSelectionSpoke(NormalSpoke):
 
         wasActive = group in self.selectedGroups
 
-        button.handler_block_by_func(self.on_checkbox_toggled)
-        button.set_active(not wasActive)
-        button.handler_unblock_by_func(self.on_checkbox_toggled)
+        with blockedHandler(button, self.on_checkbox_toggled):
+            button.set_active(not wasActive)
 
         if wasActive:
             self.selectedGroups.remove(group)
diff --git a/pyanaconda/ui/gui/spokes/source.py b/pyanaconda/ui/gui/spokes/source.py
index 78ccb27..0bfd8cf 100644
--- a/pyanaconda/ui/gui/spokes/source.py
+++ b/pyanaconda/ui/gui/spokes/source.py
@@ -38,7 +38,7 @@ from pyanaconda.ui.gui import GUIObject
 from pyanaconda.ui.gui.helpers import GUIDialogInputCheckHandler, GUISpokeInputCheckHandler
 from pyanaconda.ui.gui.spokes import NormalSpoke
 from pyanaconda.ui.categories.software import SoftwareCategory
-from pyanaconda.ui.gui.utils import fire_gtk_action
+from pyanaconda.ui.gui.utils import blockedHandler, fire_gtk_action
 from pyanaconda.iutil import ProxyString, ProxyStringError, cmp_obj_attrs
 from pyanaconda.ui.gui.utils import gtk_call_once, really_hide, really_show, fancy_set_sensitive
 from pyanaconda.threads import threadMgr, AnacondaThread
@@ -1198,9 +1198,10 @@ class SourceSpoke(NormalSpoke, GUISpokeInputCheckHandler):
             and reset the checkbox and combobox.
         """
         self._repoNameEntry.set_text("")
-        self._repoMirrorlistCheckbox.handler_block_by_func(self.on_repoMirrorlistCheckbox_toggled)
-        self._repoMirrorlistCheckbox.set_active(False)
-        self._repoMirrorlistCheckbox.handler_unblock_by_func(self.on_repoMirrorlistCheckbox_toggled)
+
+        with blockedHandler(self._repoMirrorlistCheckbox, self.on_repoMirrorlistCheckbox_toggled):
+            self._repoMirrorlistCheckbox.set_active(False)
+
         self._repoUrlEntry.set_text("")
         self._repoProtocolComboBox.set_active(0)
         self._repoProxyUrlEntry.set_text("")
@@ -1215,14 +1216,13 @@ class SourceSpoke(NormalSpoke, GUISpokeInputCheckHandler):
         """
         self._repoNameEntry.set_text(repo.name)
 
-        self._repoMirrorlistCheckbox.handler_block_by_func(self.on_repoMirrorlistCheckbox_toggled)
-        if repo.mirrorlist:
-            url = repo.mirrorlist
-            self._repoMirrorlistCheckbox.set_active(True)
-        else:
-            url = repo.baseurl
-            self._repoMirrorlistCheckbox.set_active(False)
-        self._repoMirrorlistCheckbox.handler_unblock_by_func(self.on_repoMirrorlistCheckbox_toggled)
+        with blockedHandler(self._repoMirrorlistCheckbox, self.on_repoMirrorlistCheckbox_toggled):
+            if repo.mirrorlist:
+                url = repo.mirrorlist
+                self._repoMirrorlistCheckbox.set_active(True)
+            else:
+                url = repo.baseurl
+                self._repoMirrorlistCheckbox.set_active(False)
 
         if url:
             for idx, proto in REPO_PROTO.iteritems():
diff --git a/pyanaconda/ui/gui/utils.py b/pyanaconda/ui/gui/utils.py
index f6687b7..0e3c50c 100644
--- a/pyanaconda/ui/gui/utils.py
+++ b/pyanaconda/ui/gui/utils.py
@@ -1,6 +1,6 @@
 # Miscellaneous UI functions
 #
-# Copyright (C) 2012, 2013 Red Hat, Inc.
+# Copyright (C) 2012-2014 Red Hat, Inc.
 #
 # This copyrighted material is made available to anyone wishing to use,
 # modify, copy, or redistribute it subject to the terms and conditions of
@@ -21,6 +21,8 @@
 #                    Vratislav Podzimek <vpodzime at redhat.com>
 #
 
+from contextlib import contextmanager
+
 from pyanaconda.threads import threadMgr, AnacondaThread
 
 from pyanaconda.constants import NOTICEABLE_FREEZE
@@ -288,6 +290,15 @@ def timed_action(delay=300, threshold=750, busy_cursor=True):
 
     return decorator
 
+ at contextmanager
+def blockedHandler(obj, func):
+    """Prevent a GLib signal handling function from being called during some
+       block of code.
+    """
+    obj.handler_block_by_func(func)
+    yield
+    obj.handler_unblock_by_func(func)
+
 def busyCursor():
     window = Gdk.get_default_root_window()
     if not window:
-- 
1.9.3



More information about the anaconda-patches mailing list