[PATCH 10/13] Implement and use a decorator for initialization needed

Vratislav Podzimek vpodzime at redhat.com
Mon Mar 10 13:22:16 UTC 2014


Some functions/callbacks need the Custom spoke to be initialized to run. It's
easier to have the code in one place and reuse it as a decorator.

Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
---
 pyanaconda/ui/gui/spokes/custom.py | 37 +++++++++++++++++++++----------------
 1 file changed, 21 insertions(+), 16 deletions(-)

diff --git a/pyanaconda/ui/gui/spokes/custom.py b/pyanaconda/ui/gui/spokes/custom.py
index faaa1f7..24e8dab 100644
--- a/pyanaconda/ui/gui/spokes/custom.py
+++ b/pyanaconda/ui/gui/spokes/custom.py
@@ -84,6 +84,8 @@ from pyanaconda.ui.gui.categories.system import SystemCategory
 from gi.repository import Gdk, Gtk
 from gi.repository.AnacondaWidgets import MountpointSelector
 
+from functools import wraps
+
 import logging
 log = logging.getLogger("anaconda")
 
@@ -120,6 +122,18 @@ unrecoverable_error_msg = N_("Storage configuration reset due to unrecoverable "
 partition_only_format_types = ["efi", "macefi", "prepboot", "biosboot",
                                "appleboot"]
 
+def needs_init(func):
+    """Decorator for functions that require the spoke to be initialized to run"""
+
+    @wraps(func)
+    def decorated(self, *args, **kwargs):
+        if not self._initialized:
+            return
+        else:
+            return func(self, *args, **kwargs)
+
+    return decorated
+
 class CustomPartitioningSpoke(NormalSpoke, StorageChecker):
     builderObjects = ["customStorageWindow", "containerStore",
                       "partitionStore", "raidStoreFiltered", "raidLevelStore",
@@ -604,12 +618,13 @@ class CustomPartitioningSpoke(NormalSpoke, StorageChecker):
             if not replaced:
                 log.warning("failed to replace device: %s", s._device)
 
+    @needs_init
     def _save_right_side(self, selector):
         """ Save settings from RHS and apply changes to the device.
 
             This method must never trigger a call to self._do_refresh.
         """
-        if not self._initialized or not selector:
+        if not selector:
             return
 
         device = selector.device
@@ -1708,10 +1723,8 @@ class CustomPartitioningSpoke(NormalSpoke, StorageChecker):
             if parent.kids == 0 and not parent.isDisk:
                 self._destroy_device(parent)
 
+    @needs_init
     def _show_mountpoint(self, page=None, mountpoint=None):
-        if not self._initialized:
-            return
-
         # Make sure there's something displayed on the RHS.  If a page and
         # mountpoint within that page is given, display that.  Otherwise, just
         # default to the first selector available.
@@ -2019,10 +2032,8 @@ class CustomPartitioningSpoke(NormalSpoke, StorageChecker):
 
         self._clear_current_selector()
 
+    @needs_init
     def on_selector_clicked(self, selector):
-        if not self._initialized:
-            return
-
         # Take care of the previously chosen selector.
         if self._current_selector and self._initialized and \
            self._current_selector != selector:
@@ -2088,10 +2099,8 @@ class CustomPartitioningSpoke(NormalSpoke, StorageChecker):
         self._removeButton.set_sensitive(not selector.device.protected)
         return True
 
+    @needs_init
     def on_page_clicked(self, page, mountpointToShow=None):
-        if not self._initialized:
-            return
-
         log.debug("page clicked: %s", page.pageTitle)
         if self._current_selector:
             self._save_current_selector()
@@ -2207,10 +2216,8 @@ class CustomPartitioningSpoke(NormalSpoke, StorageChecker):
         self._encryptCheckbox.set_sensitive(device_type != DEVICE_TYPE_BTRFS)
         fancy_set_sensitive(self._fsCombo, active)
 
+    @needs_init
     def on_fs_type_changed(self, combo):
-        if not self._initialized:
-            return
-
         new_type = combo.get_active_text()
         if new_type is None:
             return
@@ -2289,10 +2296,8 @@ class CustomPartitioningSpoke(NormalSpoke, StorageChecker):
         else:
             map(really_hide, [self._containerLabel, self._containerCombo, self._modifyContainerButton])
 
+    @needs_init
     def on_device_type_changed(self, combo):
-        if not self._initialized:
-            return
-
         new_type = self._get_current_device_type()
         log.debug("device_type_changed: %s %s", new_type, combo.get_active_text())
         if new_type is None:
-- 
1.8.5.3



More information about the anaconda-patches mailing list