[anaconda:master 2/2] Make size_from_input() and size_from_entry() methods handier.
mulhern
amulhern at redhat.com
Fri Oct 17 22:06:48 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_entry() on a lower bound.
* Add method docstrings.
Should not change any observable behavior, as size_from_input() seems to
be only invoked by size_from_entry() method.
Signed-off-by: mulhern <amulhern at redhat.com>
---
pyanaconda/storage_utils.py | 13 +++++----
pyanaconda/ui/gui/spokes/custom.py | 7 +++--
.../ui/gui/spokes/lib/custom_storage_helpers.py | 34 +++++++++++++++++++---
3 files changed, 42 insertions(+), 12 deletions(-)
diff --git a/pyanaconda/storage_utils.py b/pyanaconda/storage_utils.py
index 7d26e78..16a1d5a 100644
--- a/pyanaconda/storage_utils.py
+++ b/pyanaconda/storage_utils.py
@@ -91,7 +91,14 @@ NAMED_DEVICE_TYPES = (DEVICE_TYPE_BTRFS, DEVICE_TYPE_LVM, DEVICE_TYPE_MD, DEVICE
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"""
+ """ Get a Size object from an input string.
+
+ :param str input_str: a string forming some representation of a size
+ :returns: a Size object corresponding to input_str
+ :rtype: :class:`blivet.size.Size` or NoneType
+
+ If no units specified, defaults to MiB.
+ """
if not input_str:
# Nothing to parse
@@ -106,10 +113,6 @@ def size_from_input(input_str):
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..9712e09 100644
--- a/pyanaconda/ui/gui/spokes/custom.py
+++ b/pyanaconda/ui/gui/spokes/custom.py
@@ -144,6 +144,9 @@ 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")
+
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 +928,7 @@ 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)
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..c64674e 100644
--- a/pyanaconda/ui/gui/spokes/lib/custom_storage_helpers.py
+++ b/pyanaconda/ui/gui/spokes/lib/custom_storage_helpers.py
@@ -78,9 +78,26 @@ 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):
+ """ 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
+ :returns: a Size object corresponding to the text in the entry field
+ :rtype: :class:`blivet.size.Size` or NoneType
+
+ If no units specified in the field, defaults to MiB.
+
+ Rounds up to lower_bound, if value in entry field corresponds
+ to a smaller value. The default for lower_bound is None.
+ """
size_text = entry.get_text().decode("utf-8").strip()
- return size_from_input(size_text)
+ size = size_from_input(size_text)
+ 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 +311,9 @@ 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")
+
def __init__(self, *args, **kwargs):
self.mountpoints = kwargs.pop("mountpoints", [])
GUIObject.__init__(self, *args, **kwargs)
@@ -320,7 +340,10 @@ 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
+ )
self.window.destroy()
def refresh(self):
@@ -440,6 +463,9 @@ 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")
+
def __init__(self, *args, **kwargs):
GUIDialogInputCheckHandler.__init__(self)
@@ -575,7 +601,7 @@ 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)
if size is None:
size = SIZE_POLICY_MAX
else:
--
1.9.3
More information about the anaconda-patches
mailing list