[PATCH 4/8] Reduce duplicated code between the GUI and TUI.

Chris Lumens clumens at redhat.com
Thu Mar 14 18:48:47 UTC 2013


getDisks and size_str now exist in precisely one place.
---
 pyanaconda/ui/gui/spokes/lib/cart.py   |  9 +----
 pyanaconda/ui/gui/spokes/lib/resize.py |  9 +----
 pyanaconda/ui/gui/spokes/storage.py    | 48 +----------------------
 pyanaconda/ui/lib/disks.py             | 72 ++++++++++++++++++++++++++++++++++
 pyanaconda/ui/tui/spokes/storage.py    | 39 +-----------------
 5 files changed, 76 insertions(+), 101 deletions(-)
 create mode 100644 pyanaconda/ui/lib/disks.py

diff --git a/pyanaconda/ui/gui/spokes/lib/cart.py b/pyanaconda/ui/gui/spokes/lib/cart.py
index 39d572a..3d2e242 100644
--- a/pyanaconda/ui/gui/spokes/lib/cart.py
+++ b/pyanaconda/ui/gui/spokes/lib/cart.py
@@ -22,6 +22,7 @@
 
 from gi.repository import Gtk
 
+from pyanaconda.ui.lib.disks import size_str
 from pyanaconda.ui.gui import GUIObject
 from blivet.size import Size
 
@@ -39,14 +40,6 @@ FREE_SPACE_COL = 3
 NAME_COL = 4
 ID_COL = 5
 
-def size_str(mb):
-    if isinstance(mb, Size):
-        spec = str(mb)
-    else:
-        spec = "%s mb" % mb
-
-    return str(Size(spec=spec)).upper()
-
 class SelectedDisksDialog(GUIObject):
     builderObjects = ["selected_disks_dialog", "disk_store", "disk_tree_view"]
     mainWidgetName = "selected_disks_dialog"
diff --git a/pyanaconda/ui/gui/spokes/lib/resize.py b/pyanaconda/ui/gui/spokes/lib/resize.py
index 7baf894..5cc0407 100644
--- a/pyanaconda/ui/gui/spokes/lib/resize.py
+++ b/pyanaconda/ui/gui/spokes/lib/resize.py
@@ -23,6 +23,7 @@ from __future__ import division
 
 from gi.repository import Gtk
 
+from pyanaconda.ui.lib.disks import size_str
 from pyanaconda.ui.gui import GUIObject
 from blivet.size import Size
 
@@ -47,14 +48,6 @@ PRESERVE = N_("Preserve")
 SHRINK = N_("Shrink")
 DELETE = N_("Delete")
 
-def size_str(mb):
-    if isinstance(mb, Size):
-        spec = str(mb)
-    else:
-        spec = "%s mb" % mb
-
-    return str(Size(spec=spec)).upper()
-
 class ResizeDialog(GUIObject):
     builderObjects = ["actionStore", "diskStore", "resizeDialog"]
     mainWidgetName = "resizeDialog"
diff --git a/pyanaconda/ui/gui/spokes/storage.py b/pyanaconda/ui/gui/spokes/storage.py
index 889dc57..3e80c80 100644
--- a/pyanaconda/ui/gui/spokes/storage.py
+++ b/pyanaconda/ui/gui/spokes/storage.py
@@ -41,6 +41,7 @@
 from gi.repository import Gdk, GLib, Gtk
 from gi.repository import AnacondaWidgets
 from pyanaconda.ui import communication
+from pyanaconda.ui.lib.disks import getDisks, size_str
 from pyanaconda.ui.gui import GUIObject
 from pyanaconda.ui.gui.spokes import NormalSpoke
 from pyanaconda.ui.gui.spokes.lib.cart import SelectedDisksDialog
@@ -73,53 +74,6 @@ log = logging.getLogger("anaconda")
 
 __all__ = ["StorageSpoke"]
 
-class FakeDiskLabel(object):
-    def __init__(self, free=0):
-        self.free = free
-
-class FakeDisk(object):
-    def __init__(self, name, size=0, free=0, partitioned=True, vendor=None,
-                 model=None, serial=None, removable=False):
-        self.name = name
-        self.size = size
-        self.format = FakeDiskLabel(free=free)
-        self.partitioned = partitioned
-        self.vendor = vendor
-        self.model = model
-        self.serial = serial
-        self.removable = removable
-
-    @property
-    def description(self):
-        return "%s %s" % (self.vendor, self.model)
-
-def getDisks(devicetree, fake=False):
-    if not fake:
-        devices = devicetree.devices + devicetree._hidden
-        disks = [d for d in devices if d.isDisk and
-                                       d.size > 0 and
-                                       not d.format.hidden and
-                                       not (d.protected and
-                                            d.removable)]
-    else:
-        disks = []
-        disks.append(FakeDisk("sda", size=300000, free=10000, serial="00001",
-                              vendor="Seagate", model="Monster"))
-        disks.append(FakeDisk("sdb", size=300000, free=300000, serial="00002",
-                              vendor="Seagate", model="Monster"))
-        disks.append(FakeDisk("sdc", size=8000, free=2100, removable=True,
-                              vendor="SanDisk", model="Cruzer", serial="00003"))
-
-    return disks
-
-def size_str(mb):
-    if isinstance(mb, Size):
-        spec = str(mb)
-    else:
-        spec = "%s mb" % mb
-
-    return str(Size(spec=spec)).upper()
-
 class InstallOptions1Dialog(GUIObject):
     builderObjects = ["options1_dialog"]
     mainWidgetName = "options1_dialog"
diff --git a/pyanaconda/ui/lib/disks.py b/pyanaconda/ui/lib/disks.py
new file mode 100644
index 0000000..e78f08e
--- /dev/null
+++ b/pyanaconda/ui/lib/disks.py
@@ -0,0 +1,72 @@
+# User interface library functions for filesystem/disk space checking
+#
+# Copyright (C) 2013  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 Lehman <dlehman at redhat.com>
+#                    Chris Lumens <clumens at redhat.com>
+#
+
+from blivet.size import Size
+
+__all__ = ["FakeDiskLabel", "FakeDisk", "getDisks", "size_str"]
+
+class FakeDiskLabel(object):
+    def __init__(self, free=0):
+        self.free = free
+
+class FakeDisk(object):
+    def __init__(self, name, size=0, free=0, partitioned=True, vendor=None,
+                 model=None, serial=None, removable=False):
+        self.name = name
+        self.size = size
+        self.format = FakeDiskLabel(free=free)
+        self.partitioned = partitioned
+        self.vendor = vendor
+        self.model = model
+        self.serial = serial
+        self.removable = removable
+
+    @property
+    def description(self):
+        return "%s %s" % (self.vendor, self.model)
+
+def getDisks(devicetree, fake=False):
+    if not fake:
+        devices = devicetree.devices + devicetree._hidden
+        disks = [d for d in devices if d.isDisk and
+                                       d.size > 0 and
+                                       not d.format.hidden and
+                                       not (d.protected and
+                                            d.removable)]
+    else:
+        disks = []
+        disks.append(FakeDisk("sda", size=300000, free=10000, serial="00001",
+                              vendor="Seagate", model="Monster"))
+        disks.append(FakeDisk("sdb", size=300000, free=300000, serial="00002",
+                              vendor="Seagate", model="Monster"))
+        disks.append(FakeDisk("sdc", size=8000, free=2100, removable=True,
+                              vendor="SanDisk", model="Cruzer", serial="00003"))
+
+    return disks
+
+def size_str(mb):
+    if isinstance(mb, Size):
+        spec = str(mb)
+    else:
+        spec = "%s mb" % mb
+
+    return str(Size(spec=spec)).upper()
diff --git a/pyanaconda/ui/tui/spokes/storage.py b/pyanaconda/ui/tui/spokes/storage.py
index 58428fc..2d0b23c 100644
--- a/pyanaconda/ui/tui/spokes/storage.py
+++ b/pyanaconda/ui/tui/spokes/storage.py
@@ -22,6 +22,7 @@
 # which has the same license and authored by David Lehman <dlehman at redhat.com>
 #
 
+from pyanaconda.ui.lib.disks import getDisks
 from pyanaconda.ui.tui.spokes import NormalTUISpoke
 from pyanaconda.ui.tui.simpleline import TextWidget, CheckboxWidget
 
@@ -51,44 +52,6 @@ CLEARNONE = _("Use Free Space")
 parttypes = {CLEARALL: CLEARPART_TYPE_ALL, CLEARLINUX: CLEARPART_TYPE_LINUX,
              CLEARNONE: CLEARPART_TYPE_NONE}
 
-class FakeDiskLabel(object):
-    def __init__(self, free=0):
-        self.free = free
-
-class FakeDisk(object):
-    def __init__(self, name, size=0, free=0, partitioned=True, vendor=None,
-                 model=None, serial=None, removable=False):
-        self.name = name
-        self.size = size
-        self.format = FakeDiskLabel(free=free)
-        self.partitioned = partitioned
-        self.vendor = vendor
-        self.model = model
-        self.serial = serial
-        self.removable = removable
-
-    @property
-    def description(self):
-        return "%s %s" % (self.vendor, self.model)
-
-def getDisks(devicetree, fake=False):
-    if not fake:
-        disks = [d for d in devicetree.devices if d.isDisk and
-                                                  d.size > 0 and
-                                                  not d.format.hidden and
-                                                  not (d.protected and
-                                                       d.removable)]
-    else:
-        disks = []
-        disks.append(FakeDisk("sda", size=300000, free=10000, serial="00001",
-                              vendor="Seagate", model="Monster"))
-        disks.append(FakeDisk("sdb", size=300000, free=300000, serial="00002",
-                              vendor="Seagate", model="Monster"))
-        disks.append(FakeDisk("sdc", size=8000, free=2100, removable=True,
-                              vendor="SanDisk", model="Cruzer", serial="00003"))
-
-    return disks
-
 class StorageSpoke(NormalTUISpoke):
     title = _("Install Destination")
     category = "destination"
-- 
1.8.1.2



More information about the anaconda-patches mailing list