[blivet f20/master 1/5] Add a function to translate sizes from English

David Shea dshea at redhat.com
Thu Nov 21 20:32:58 UTC 2013


Size specs are expected to be translated to the current locale, so
translate constant size specs before passing them to the Size
constructor.
---
 blivet/__init__.py | 12 ++++++------
 blivet/size.py     | 16 ++++++++++++++++
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/blivet/__init__.py b/blivet/__init__.py
index dbd8b69..327389b 100644
--- a/blivet/__init__.py
+++ b/blivet/__init__.py
@@ -85,7 +85,7 @@ import util
 import arch
 from flags import flags
 from platform import platform as _platform
-from size import Size
+from size import Size, xlate_size
 
 import shelve
 import contextlib
@@ -876,7 +876,7 @@ class Blivet(object):
             should_clear = self.shouldClear(disk, clearPartType=clearPartType,
                                             clearPartDisks=[disk.name])
             if should_clear:
-                free[disk.name] = (Size(spec="%f mb" % disk.size), 0)
+                free[disk.name] = (Size(spec=xlate_size("%f mb" % disk.size)), 0)
                 continue
 
             disk_free = 0
@@ -899,8 +899,8 @@ class Blivet(object):
             elif disk.format.type is None:
                 disk_free = disk.size
 
-            free[disk.name] = (Size(spec="%f mb" % disk_free),
-                               Size(spec="%f mb" % fs_free))
+            free[disk.name] = (Size(spec=xlate_size("%f mb" % disk_free)),
+                               Size(spec=xlate_size("%f mb" % fs_free)))
 
         return free
 
@@ -1595,8 +1595,8 @@ class Blivet(object):
                                     "'biosboot' type partition."))
 
         if not swaps:
-            installed = Size(spec="%s kb" % util.total_memory())
-            required = Size(spec="%s kb" % isys.EARLY_SWAP_RAM)
+            installed = Size(spec=xlate_size("%s kb" % util.total_memory()))
+            required = Size(spec=xlate_size("%s kb" % isys.EARLY_SWAP_RAM))
 
             if installed < required:
                 errors.append(_("You have not specified a swap partition.  "
diff --git a/blivet/size.py b/blivet/size.py
index 6c3eb90..d9af722 100644
--- a/blivet/size.py
+++ b/blivet/size.py
@@ -99,6 +99,22 @@ def _parseSpec(spec):
 
     raise ValueError("invalid size specification", spec)
 
+def xlate_size(spec):
+    """Translate English size specifiers into the current locale."""
+
+    m = re.match(r'(.*?)([A-Za-z]*)$', spec.strip())
+    if not m:
+        raise ValueError("invalid size specification", spec)
+
+    specifier = m.groups()[1].lower()
+    
+    # Look for MB, megabyte, megabytes, translate prefix and byte separately
+    for ending in ['byte', 'bytes', 'b']:
+        if specifier.endswith(ending):
+            return m.groups()[0] + _(specifier[:-len(ending)]) + _(ending)
+    else:
+        return m.groups()[0] + _(specifier)
+
 class Size(Decimal):
     """ Common class to represent storage device and filesystem sizes.
         Can handle parsing strings such as 45MB or 6.7GB to initialize
-- 
1.8.4.2



More information about the anaconda-patches mailing list