[PATCH 08/13] Make code to get Size instance from user's input reusable

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


This code may come handy outside of the GUI code.

Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
---
 pyanaconda/storage_utils.py                        | 50 ++++++++++++++++++++++
 .../ui/gui/spokes/lib/custom_storage_helpers.py    | 23 +---------
 2 files changed, 52 insertions(+), 21 deletions(-)
 create mode 100644 pyanaconda/storage_utils.py

diff --git a/pyanaconda/storage_utils.py b/pyanaconda/storage_utils.py
new file mode 100644
index 0000000..b971111
--- /dev/null
+++ b/pyanaconda/storage_utils.py
@@ -0,0 +1,50 @@
+#
+# 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): Vratislav Podzimek <vpodzime at redhat.com>
+#
+
+"""UI-independent storage utility functions"""
+
+import re
+import locale
+
+from blivet.size import Size
+from blivet.errors import SizeParamsError
+
+def size_from_input(input_str):
+    """Get size from user's input"""
+
+    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
+    if re.search(r'[\d.%s]$' % locale.nl_langinfo(locale.RADIXCHAR), input_str):
+        input_str += "MiB"
+
+    try:
+        size = Size(spec=input_str)
+    except (SizeParamsError, ValueError):
+        return None
+    else:
+        # Minimium size for ui-created partitions is 1MiB.
+        if size.convertTo(spec="MiB") < 1:
+            size = Size(spec="1 MiB")
+
+    return size
diff --git a/pyanaconda/ui/gui/spokes/lib/custom_storage_helpers.py b/pyanaconda/ui/gui/spokes/lib/custom_storage_helpers.py
index 26280e0..ae62476 100644
--- a/pyanaconda/ui/gui/spokes/lib/custom_storage_helpers.py
+++ b/pyanaconda/ui/gui/spokes/lib/custom_storage_helpers.py
@@ -29,12 +29,12 @@ import re
 
 from pyanaconda.product import productName
 from pyanaconda.iutil import lowerASCII
+from pyanaconda.storage_utils import size_from_input
 from pyanaconda.ui.gui import GUIObject
 from pyanaconda.ui.gui.utils import fancy_set_sensitive, really_hide, really_show
 from pyanaconda.i18n import _, N_, C_
 
 from blivet.size import Size
-from blivet.errors import SizeParamsError
 from blivet.platform import platform
 from blivet.formats import getFormat
 from blivet.devicefactory import SIZE_POLICY_AUTO
@@ -62,26 +62,7 @@ CONTAINER_TYPE_NAMES = {DEVICE_TYPE_LVM: N_("Volume Group"),
 
 def size_from_entry(entry):
     size_text = entry.get_text().decode("utf-8").strip()
-
-    # Nothing to parse
-    if not size_text:
-        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
-    if re.search(r'[\d.%s]$' % locale.nl_langinfo(locale.RADIXCHAR), size_text):
-        size_text += "MiB"
-
-    try:
-        size = Size(spec=size_text)
-    except (SizeParamsError, ValueError):
-        return None
-    else:
-        # Minimium size for ui-created partitions is 1MiB.
-        if size.convertTo(spec="MiB") < 1:
-            size = Size(spec="1 MiB")
-
-    return size
+    return size_from_input(size_text)
 
 class UIStorageFilter(logging.Filter):
     def filter(self, record):
-- 
1.8.5.3



More information about the anaconda-patches mailing list