[rhel6-branch 27/30] Let convertTo take a string spec as well as a defined constant.
mulkieran
installerbot-noreply at redhat.com
Thu May 28 21:04:32 UTC 2015
From: mulhern <amulhern at redhat.com>
So the interface is compatible with previous.
Signed-off-by: mulhern <amulhern at redhat.com>
---
blivet/size.py | 3 +++
tests/size_test.py | 4 ++++
2 files changed, 7 insertions(+)
diff --git a/blivet/size.py b/blivet/size.py
index 4263662..842536e 100644
--- a/blivet/size.py
+++ b/blivet/size.py
@@ -287,9 +287,12 @@ def convertTo(self, spec=None):
""" Return the size in the units indicated by the specifier.
:param spec: a units specifier
+ :type spec: a defined constant or a string
:returns: a numeric value in the units indicated by the specifier
:rtype: Decimal
"""
+ if isinstance(spec, basestring):
+ spec = parseUnits(spec, False)
return Decimal(self) / Decimal((spec or B).factor)
def humanReadable(self, max_places=2, strip=True, min_value=1, xlate=True):
diff --git a/tests/size_test.py b/tests/size_test.py
index d9d93d9..c441bf2 100644
--- a/tests/size_test.py
+++ b/tests/size_test.py
@@ -174,6 +174,10 @@ def testConvertToPrecision(self):
self.assertEquals(s.convertTo(KiB), 1792)
self.assertEquals(s.convertTo(MiB), Decimal("1.75"))
+ def testConvertToString(self):
+ s = Size(1835008)
+ self.assertEquals(s.convertTo(MiB), s.convertTo("MiB"))
+
def testNegative(self):
s = Size("-500MiB")
self.assertEquals(s.humanReadable(), "-500 MiB")
--
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/99913b0f5409fd1365b6bc0e154535adf43d5c31
More information about the anaconda-patches
mailing list