[anaconda:master 2/2] Make size_from_input() and size_from_entry() methods handier.

mulhern amulhern at redhat.com
Tue Oct 21 14:06:45 UTC 2014


* In size_from_input(), do not round up to any lower bound, just get the
size. It's more general that way.
* Parameterize size_from_input() on default units.
* Parameterize size_from_entry() on a lower bound and on default units.
* Add method docstrings.

Should not change any observable behavior.

Signed-off-by: mulhern <amulhern at redhat.com>
---
 pyanaconda/storage_utils.py                        | 22 ++++++----
 pyanaconda/ui/gui/spokes/custom.py                 | 14 +++++--
 .../ui/gui/spokes/lib/custom_storage_helpers.py    | 48 ++++++++++++++++++++--
 3 files changed, 68 insertions(+), 16 deletions(-)

diff --git a/pyanaconda/storage_utils.py b/pyanaconda/storage_utils.py
index 7d26e78..4c35184 100644
--- a/pyanaconda/storage_utils.py
+++ b/pyanaconda/storage_utils.py
@@ -90,26 +90,30 @@ AUTOPART_DEVICE_TYPES = {AUTOPART_TYPE_LVM: DEVICE_TYPE_LVM,
 NAMED_DEVICE_TYPES = (DEVICE_TYPE_BTRFS, DEVICE_TYPE_LVM, DEVICE_TYPE_MD, DEVICE_TYPE_LVM_THINP)
 CONTAINER_DEVICE_TYPES = (DEVICE_TYPE_LVM, DEVICE_TYPE_BTRFS, DEVICE_TYPE_LVM_THINP)
 
-def size_from_input(input_str):
-    """Get size from user's input"""
+def size_from_input(input_str, units=None):
+    """ Get a Size object from an input string.
+
+        :param str input_str: a string forming some representation of a size
+        :param units: use these units if none specified in input_str
+        :type units: str or NoneType
+        :returns: a Size object corresponding to input_str
+        :rtype: :class:`blivet.size.Size` or NoneType
+
+        Units default to bytes if no units in input_str or units.
+    """
 
     if not input_str:
         # Nothing to parse
         return None
 
-    # if no unit was specified, default to MiB. Assume that a string
-    # ending with anything other than a digit has a unit suffix
+    # A string ending with a digit contains no units information.
     if re.search(r'[\d.%s]$' % locale.nl_langinfo(locale.RADIXCHAR), input_str):
-        input_str += "MiB"
+        input_str += units or ""
 
     try:
         size = Size(input_str)
     except ValueError:
         return None
-    else:
-        # Minimium size for ui-created partitions is 1MiB.
-        if size.convertTo(spec="MiB") < 1:
-            size = Size("1 MiB")
 
     return size
 
diff --git a/pyanaconda/ui/gui/spokes/custom.py b/pyanaconda/ui/gui/spokes/custom.py
index 6e7038a..b3e1265 100644
--- a/pyanaconda/ui/gui/spokes/custom.py
+++ b/pyanaconda/ui/gui/spokes/custom.py
@@ -144,6 +144,12 @@ class CustomPartitioningSpoke(NormalSpoke, StorageChecker):
     # The maximum number of places to show when displaying a size
     MAX_SIZE_PLACES = 2
 
+    # If the user enters a smaller size, the GUI changes it to this value
+    MIN_SIZE_ENTRY = Size("1 MiB")
+
+    # Default to these units if the user does not enter any.
+    SIZE_UNITS_DEFAULT = "MiB"
+
     def __init__(self, data, storage, payload, instclass):
         StorageChecker.__init__(self, min_ram=isys.MIN_GUI_RAM)
         NormalSpoke.__init__(self, data, storage, payload, instclass)
@@ -925,9 +931,11 @@ class CustomPartitioningSpoke(NormalSpoke, StorageChecker):
         if old_size.humanReadable(max_places=self.MAX_SIZE_PLACES) == self._sizeEntry.get_text():
             size = old_size
         else:
-            # Note that this size may not correspond to user's entry,
-            # as size_from_entry has a default lower limit.
-            size = size_from_entry(self._sizeEntry)
+            size = size_from_entry(
+               self._sizeEntry,
+               lower_bound=self.MIN_SIZE_ENTRY,
+               units=self.SIZE_UNITS_DEFAULT
+            )
         changed_size = ((use_dev.resizable or not use_dev.exists) and
                         size != old_size)
         old_device_info["size"] = old_size
diff --git a/pyanaconda/ui/gui/spokes/lib/custom_storage_helpers.py b/pyanaconda/ui/gui/spokes/lib/custom_storage_helpers.py
index 4d0948c..32703c7 100644
--- a/pyanaconda/ui/gui/spokes/lib/custom_storage_helpers.py
+++ b/pyanaconda/ui/gui/spokes/lib/custom_storage_helpers.py
@@ -78,9 +78,29 @@ CONTAINER_TYPES = {DEVICE_TYPE_LVM:       ContainerType(N_("Volume Group"), N_("
 # These cannot be specified as mountpoints
 system_mountpoints = ["/dev", "/proc", "/run", "/sys"]
 
-def size_from_entry(entry):
+def size_from_entry(entry, lower_bound=None, units=None):
+    """ Get a Size object from an entry field.
+
+        :param lower_bound: lower bound for size returned,
+        :type lower_bound: :class:`blivet.size.Size` or NoneType
+        :param units: units to use if none obtained from entry
+        :type units: str or NoneType
+        :returns: a Size object corresponding to the text in the entry field
+        :rtype: :class:`blivet.size.Size` or NoneType
+
+        Units default to bytes if no units specified in entry or units.
+
+        Rounds up to lower_bound, if value in entry field corresponds
+        to a smaller value. The default for lower_bound is None, yielding
+        no rounding.
+    """
     size_text = entry.get_text().decode("utf-8").strip()
-    return size_from_input(size_text)
+    size = size_from_input(size_text, units=units)
+    if size is None:
+        return None
+    if lower_bound is not None and size < lower_bound:
+        return lower_bound
+    return size
 
 def populate_mountpoint_store(store, used_mountpoints):
     # sure, add whatever you want to this list. this is just a start.
@@ -294,6 +314,12 @@ class AddDialog(GUIObject):
     mainWidgetName = "addDialog"
     uiFile = "spokes/lib/custom_storage_helpers.glade"
 
+    # If the user enters a smaller size, the GUI changes it to this value
+    MIN_SIZE_ENTRY = Size("1 MiB")
+
+    # Default to these units if the user does not enter any.
+    SIZE_UNITS_DEFAULT = "MiB"
+
     def __init__(self, *args, **kwargs):
         self.mountpoints = kwargs.pop("mountpoints", [])
         GUIObject.__init__(self, *args, **kwargs)
@@ -320,7 +346,11 @@ class AddDialog(GUIObject):
         if self._error:
             return
 
-        self.size = size_from_entry(self.builder.get_object("addSizeEntry"))
+        self.size = size_from_entry(
+           self.builder.get_object("addSizeEntry"),
+           lower_bound=self.MIN_SIZE_ENTRY,
+           units=self.SIZE_UNITS_DEFAULT
+        )
         self.window.destroy()
 
     def refresh(self):
@@ -440,6 +470,12 @@ class ContainerDialog(GUIObject, GUIDialogInputCheckHandler):
     mainWidgetName = "container_dialog"
     uiFile = "spokes/lib/custom_storage_helpers.glade"
 
+    # If the user enters a smaller size, the GUI changes it to this value
+    MIN_SIZE_ENTRY = Size("1 MiB")
+
+    # Default to these units if the user does not enter any.
+    SIZE_UNITS_DEFAULT = "MiB"
+
     def __init__(self, *args, **kwargs):
         GUIDialogInputCheckHandler.__init__(self)
 
@@ -575,7 +611,11 @@ class ContainerDialog(GUIObject, GUIDialogInputCheckHandler):
             size = SIZE_POLICY_MAX
         elif idx == 2:
             if self._original_size_text != self._sizeEntry.get_text():
-                size = size_from_entry(self._sizeEntry)
+                size = size_from_entry(
+                   self._sizeEntry,
+                   lower_bound=self.MIN_SIZE_ENTRY,
+                   units=self.SIZE_UNITS_DEFAULT
+                )
                 if size is None:
                     size = SIZE_POLICY_MAX
             else:
-- 
1.9.3



More information about the anaconda-patches mailing list