[PATCH] Move GUI-specific helper classes to a separate module (#1091542)

David Shea dshea at redhat.com
Fri May 2 13:52:06 UTC 2014


This keeps Gtk dependencies out of text-mode modules that use the
helper classes.
---
 pyanaconda/ui/gui/helpers.py               | 124 +++++++++++++++++++++++++++++
 pyanaconda/ui/gui/spokes/datetime_spoke.py |   3 +-
 pyanaconda/ui/gui/spokes/password.py       |   3 +-
 pyanaconda/ui/gui/spokes/source.py         |   4 +-
 pyanaconda/ui/gui/spokes/user.py           |   3 +-
 pyanaconda/ui/helpers.py                   |  98 -----------------------
 6 files changed, 132 insertions(+), 103 deletions(-)
 create mode 100644 pyanaconda/ui/gui/helpers.py

diff --git a/pyanaconda/ui/gui/helpers.py b/pyanaconda/ui/gui/helpers.py
new file mode 100644
index 0000000..52aabca
--- /dev/null
+++ b/pyanaconda/ui/gui/helpers.py
@@ -0,0 +1,124 @@
+# Abstract base classes for GUI classes
+#
+# Copyright (C) 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
+# the GNU General Public License v.2, or (at your option) any later version.
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY expressed or implied, including the implied warranties of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
+# Public License for more details.  You should have received a copy of the
+# GNU General Public License along with this program; if not, write to the
+# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
+# source code or documentation are not subject to the GNU General Public
+# License and may only be used or replicated with the express permission of
+# Red Hat, Inc.
+#
+# Red Hat Author(s): David Shea <dshea at redhat.com
+#
+
+# This file contains abstract base classes that are specific to GUI
+# functionality. See also pyanaconda.ui.helpers.
+
+from abc import ABCMeta, abstractproperty, abstractmethod
+from gi.repository import Gtk
+
+from pyanaconda.ui.helpers import InputCheck, InputCheckHandler
+
+# Inherit abstract methods from InputCheckHandler
+# pylint: disable=abstract-method
+class GUIInputCheckHandler(InputCheckHandler):
+    """Provide InputCheckHandler functionality for Gtk input screens.
+
+       This class assumes that all input objects are of type GtkEditable and
+       attaches InputCheck.update_check_status to the changed signal.
+    """
+
+    __metaclass__ = ABCMeta
+
+    def _update_check_status(self, editable, inputcheck):
+        inputcheck.update_check_status()
+
+    def get_input(self, input_obj):
+        return input_obj.get_text()
+
+    def add_check(self, input_obj, run_check, data=None):
+        checkRef = InputCheckHandler.add_check(self, input_obj, run_check, data)
+        input_obj.connect_after("changed", self._update_check_status, checkRef)
+        return checkRef
+
+class GUIDialogInputCheckHandler(GUIInputCheckHandler):
+    """Provide InputCheckHandler functionality for Gtk dialogs.
+
+       This class provides a helper method for setting an error message
+       on an entry field. Implementors of this class must still provide
+       a set_status method in order to control the sensitivty of widgets or
+       ignore activated signals.
+    """
+
+    __metaclass__ = ABCMeta
+
+    @abstractmethod
+    def set_status(self, inputcheck):
+        if inputcheck.check_status in (InputCheck.CHECK_OK, InputCheck.CHECK_SILENT):
+            inputcheck.input_obj.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY, None)
+            inputcheck.input_obj.set_icon_tooltip_text(Gtk.EntryIconPosition.SECONDARY, "")
+        else:
+            inputcheck.input_obj.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY,
+                    "gtk-dialog-error")
+            inputcheck.input_obj.set_icon_tooltip_text(Gtk.EntryIconPosition.SECONDARY,
+                inputcheck.check_status)
+
+class GUISpokeInputCheckHandler(GUIInputCheckHandler):
+    """Provide InputCheckHandler functionality for graphical spokes.
+
+       This class implements set_status to set a message in the warning area of
+       the spoke window and provides an implementation of on_back_clicked to
+       prevent the user from exiting a spoke with bad input.
+    """
+
+    __metaclass__ = ABCMeta
+
+    def set_status(self, inputcheck):
+        """Update the warning with the input validation error from the first
+           error message.
+        """
+        failed_check = next(self.failed_checks_with_message, None)
+
+        self.clear_info()
+        if failed_check:
+            self.set_warning(failed_check.check_status)
+            self.window.show_all()
+
+    # Implemented by GUIObject
+    @abstractmethod
+    def clear_info(self):
+        pass
+
+    # Implemented by GUIObject
+    @abstractmethod
+    def set_warning(self, msg):
+        pass
+
+    # Implemented by GUIObject
+    @abstractproperty
+    def window(self):
+        pass
+
+    @abstractmethod
+    def on_back_clicked(self, window):
+        """Check whether the input validation checks allow the spoke to be exited.
+
+           Unlike NormalSpoke.on_back_clicked, this function returns a boolean value.
+           Classes implementing this class should run GUISpokeInputCheckHandler.on_back_clicked,
+           and if it succeeded, run NormalSpoke.on_back_clicked.
+        """
+        failed_check = next(self.failed_checks, None)
+
+        if failed_check:
+            failed_check.input_obj.grab_focus()
+            return False
+        else:
+            return True
diff --git a/pyanaconda/ui/gui/spokes/datetime_spoke.py b/pyanaconda/ui/gui/spokes/datetime_spoke.py
index f272c9e..ea791c2 100644
--- a/pyanaconda/ui/gui/spokes/datetime_spoke.py
+++ b/pyanaconda/ui/gui/spokes/datetime_spoke.py
@@ -30,7 +30,8 @@ from pyanaconda.ui.gui import GUIObject
 from pyanaconda.ui.gui.spokes import NormalSpoke
 from pyanaconda.ui.gui.categories.localization import LocalizationCategory
 from pyanaconda.ui.gui.utils import enlightbox, gtk_action_nowait, gtk_call_once
-from pyanaconda.ui.helpers import GUIDialogInputCheckHandler, InputCheck
+from pyanaconda.ui.gui.helpers import GUIDialogInputCheckHandler
+from pyanaconda.ui.helpers import InputCheck
 
 from pyanaconda.i18n import _, CN_
 from pyanaconda.timezone import NTP_SERVICE, get_all_regions_and_timezones, is_valid_timezone
diff --git a/pyanaconda/ui/gui/spokes/password.py b/pyanaconda/ui/gui/spokes/password.py
index 50d0136..3855758 100644
--- a/pyanaconda/ui/gui/spokes/password.py
+++ b/pyanaconda/ui/gui/spokes/password.py
@@ -26,8 +26,9 @@ from pyanaconda.users import cryptPassword, validatePassword
 
 from pyanaconda.ui.gui.spokes import NormalSpoke
 from pyanaconda.ui.gui.categories.user_settings import UserSettingsCategory
+from pyanaconda.ui.gui.helpers import GUISpokeInputCheckHandler
 from pyanaconda.ui.common import FirstbootSpokeMixIn
-from pyanaconda.ui.helpers import GUISpokeInputCheckHandler, InputCheck
+from pyanaconda.ui.helpers import InputCheck
 
 from pyanaconda.constants import PASSWORD_EMPTY_ERROR, PASSWORD_CONFIRM_ERROR_GUI,\
         PASSWORD_STRENGTH_DESC, PASSWORD_WEAK, PASSWORD_WEAK_WITH_ERROR,\
diff --git a/pyanaconda/ui/gui/spokes/source.py b/pyanaconda/ui/gui/spokes/source.py
index 17f4f6c..a76edb3 100644
--- a/pyanaconda/ui/gui/spokes/source.py
+++ b/pyanaconda/ui/gui/spokes/source.py
@@ -33,9 +33,9 @@ from pyanaconda.flags import flags
 from pyanaconda.i18n import _, N_, CN_
 from pyanaconda.image import opticalInstallMedia, potentialHdisoSources
 from pyanaconda.ui.communication import hubQ
-from pyanaconda.ui.helpers import GUIDialogInputCheckHandler, GUISpokeInputCheckHandler, InputCheck,\
-        InputCheckHandler
+from pyanaconda.ui.helpers import InputCheck, InputCheckHandler
 from pyanaconda.ui.gui import GUIObject
+from pyanaconda.ui.gui.helpers import GUIDialogInputCheckHandler, GUISpokeInputCheckHandler
 from pyanaconda.ui.gui.spokes import NormalSpoke
 from pyanaconda.ui.gui.categories.software import SoftwareCategory
 from pyanaconda.ui.gui.utils import enlightbox, fire_gtk_action
diff --git a/pyanaconda/ui/gui/spokes/user.py b/pyanaconda/ui/gui/spokes/user.py
index 62f6024..dd40c68 100644
--- a/pyanaconda/ui/gui/spokes/user.py
+++ b/pyanaconda/ui/gui/spokes/user.py
@@ -31,7 +31,8 @@ from pyanaconda.ui.gui import GUIObject
 from pyanaconda.ui.gui.categories.user_settings import UserSettingsCategory
 from pyanaconda.ui.common import FirstbootSpokeMixIn
 from pyanaconda.ui.gui.utils import enlightbox
-from pyanaconda.ui.helpers import GUISpokeInputCheckHandler, GUIDialogInputCheckHandler, InputCheck
+from pyanaconda.ui.helpers import InputCheck
+from pyanaconda.ui.gui.helpers import GUISpokeInputCheckHandler, GUIDialogInputCheckHandler
 
 from pykickstart.constants import FIRSTBOOT_RECONFIG
 from pyanaconda.constants import ANACONDA_ENVIRON, FIRSTBOOT_ENVIRON,\
diff --git a/pyanaconda/ui/helpers.py b/pyanaconda/ui/helpers.py
index e3dd253..6c90ba9 100644
--- a/pyanaconda/ui/helpers.py
+++ b/pyanaconda/ui/helpers.py
@@ -65,8 +65,6 @@ from pyanaconda.i18n import _
 import logging
 import copy
 
-from gi.repository import Gtk
-
 class StorageChecker(object):
     __metaclass__ = ABCMeta
 
@@ -384,99 +382,3 @@ class InputCheckHandler(object):
     def checks(self):
         """An iterator over all input checks"""
         return self._check_list.__iter__()
-
-# Inherit abstract methods from InputCheckHandler
-# pylint: disable=abstract-method
-class GUIInputCheckHandler(InputCheckHandler):
-    """Provide InputCheckHandler functionality for Gtk input screens.
-
-       This class assumes that all input objects are of type GtkEditable and
-       attaches InputCheck.update_check_status to the changed signal.
-    """
-
-    __metaclass__ = ABCMeta
-
-    def _update_check_status(self, editable, inputcheck):
-        inputcheck.update_check_status()
-
-    def get_input(self, input_obj):
-        return input_obj.get_text()
-
-    def add_check(self, input_obj, run_check, data=None):
-        checkRef = InputCheckHandler.add_check(self, input_obj, run_check, data)
-        input_obj.connect_after("changed", self._update_check_status, checkRef)
-        return checkRef
-
-class GUIDialogInputCheckHandler(GUIInputCheckHandler):
-    """Provide InputCheckHandler functionality for Gtk dialogs.
-
-       This class provides a helper method for setting an error message
-       on an entry field. Implementors of this class must still provide
-       a set_status method in order to control the sensitivty of widgets or
-       ignore activated signals.
-    """
-
-    __metaclass__ = ABCMeta
-
-    @abstractmethod
-    def set_status(self, inputcheck):
-        if inputcheck.check_status in (InputCheck.CHECK_OK, InputCheck.CHECK_SILENT):
-            inputcheck.input_obj.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY, None)
-            inputcheck.input_obj.set_icon_tooltip_text(Gtk.EntryIconPosition.SECONDARY, "")
-        else:
-            inputcheck.input_obj.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY,
-                    "gtk-dialog-error")
-            inputcheck.input_obj.set_icon_tooltip_text(Gtk.EntryIconPosition.SECONDARY,
-                inputcheck.check_status)
-
-class GUISpokeInputCheckHandler(GUIInputCheckHandler):
-    """Provide InputCheckHandler functionality for graphical spokes.
-
-       This class implements set_status to set a message in the warning area of
-       the spoke window and provides an implementation of on_back_clicked to
-       prevent the user from exiting a spoke with bad input.
-    """
-
-    __metaclass__ = ABCMeta
-
-    def set_status(self, inputcheck):
-        """Update the warning with the input validation error from the first
-           error message.
-        """
-        failed_check = next(self.failed_checks_with_message, None)
-
-        self.clear_info()
-        if failed_check:
-            self.set_warning(failed_check.check_status)
-            self.window.show_all()
-
-    # Implemented by GUIObject
-    @abstractmethod
-    def clear_info(self):
-        pass
-
-    # Implemented by GUIObject
-    @abstractmethod
-    def set_warning(self, msg):
-        pass
-
-    # Implemented by GUIObject
-    @abstractproperty
-    def window(self):
-        pass
-
-    @abstractmethod
-    def on_back_clicked(self, window):
-        """Check whether the input validation checks allow the spoke to be exited.
-
-           Unlike NormalSpoke.on_back_clicked, this function returns a boolean value.
-           Classes implementing this class should run GUISpokeInputCheckHandler.on_back_clicked,
-           and if it succeeded, run NormalSpoke.on_back_clicked.
-        """
-        failed_check = next(self.failed_checks, None)
-
-        if failed_check:
-            failed_check.input_obj.grab_focus()
-            return False
-        else:
-            return True
-- 
1.9.0



More information about the anaconda-patches mailing list