[blivet:master+? 7/9] Rewrite convertTo().

mulhern amulhern at redhat.com
Thu Oct 23 17:57:07 UTC 2014


Use _parseUnits() to locate the divisor.
Make convertTo() work with translated specs as well.

Thereby fix a small bug where the spec was lower-cased as if it were locale
specific, but the prefixes to match against were untranslated.

Change default to "", which means bytes, just like "b".

This change emphasises that _parseSpec() and convertTo() should agree on
what a spec means.

Signed-off-by: mulhern <amulhern at redhat.com>
---
 blivet/size.py | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)

diff --git a/blivet/size.py b/blivet/size.py
index f858c51..12a1102 100644
--- a/blivet/size.py
+++ b/blivet/size.py
@@ -323,23 +323,16 @@ class Size(Decimal):
     def __mod__(self, other, context=None):
         return Size(Decimal.__mod__(self, other, context=context))
 
-    def convertTo(self, spec="b"):
-        """ Return the size in the units indicated by the specifier.  The
-            specifier can be prefixes from the _DECIMAL_PREFIXES and
-            _BINARY_PREFIXES lists combined with 'b' or 'B' for abbreviations)
-            or 'bytes' (for prefixes like kilo or mega). The size is returned
-            as a Decimal.
-        """
-        spec = spec.lower()
-
-        if spec in _BYTES:
-            return Decimal(self)
+    def convertTo(self, spec="", xlate=False):
+        """ Return the size in the units indicated by the specifier.
 
-        for factor, prefix, abbr in _PREFIXES:
-            check = _makeSpecs(prefix, abbr, False)
-
-            if spec in check:
-                return Decimal(self) / Decimal(factor)
+            :param str spec: a units specifier
+            :returns: a numeric value in the units indicated by the specifier
+            :rtype: Decimal
+        """
+        divisor = _parseUnits(spec, xlate)
+        if divisor:
+            return Decimal(self) / Decimal(divisor)
 
         return None
 
-- 
1.9.3



More information about the anaconda-patches mailing list