summaryrefslogtreecommitdiffstats
path: root/glusternagios
diff options
context:
space:
mode:
authorSahina Bose <sabose@redhat.com>2014-03-19 19:25:16 +0530
committerBala.FA <barumuga@redhat.com>2014-04-28 16:20:46 +0530
commit7750d910bb7047c0c24f2e93dd302a445b5068ec (patch)
tree21153270f087afd912f99778b3a728d30a93f65a /glusternagios
parent576d7248699f8017f2909f548ef80df7217b57a4 (diff)
utils: Added utils method to convert size
The method converts a value from a given unit to another. Example usage: convertSize (value, 'KB', 'MB) Change-Id: If8e004076493006234be81e656eabde7d3bf9a6a Signed-off-by: Sahina Bose <sabose@redhat.com> Reviewed-on: https://cuckoo.blr.redhat.com:8443/32 Reviewed-by: Shubhendu Tripathi <shtripat@redhat.com> Reviewed-by: Nishanth Thomas <nishusemail@gmail.com>
Diffstat (limited to 'glusternagios')
-rw-r--r--glusternagios/utils.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/glusternagios/utils.py b/glusternagios/utils.py
index 44c4727..18acb77 100644
--- a/glusternagios/utils.py
+++ b/glusternagios/utils.py
@@ -486,3 +486,20 @@ def xml2dict(tree):
else:
d[tree.tag] = text
return d
+
+
+def convertSize(val, unitFrom, unitTo):
+ ''' Convert size from one unit to another
+ For example, KB to MB
+ '''
+ units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
+ unitFromIndex = units.index(unitFrom)
+ unitToIndex = units.index(unitTo)
+
+ if unitFromIndex < unitToIndex:
+ convFactor = 1 << (unitToIndex - unitFromIndex) * 10
+ return float(val) / convFactor
+ if unitFromIndex > unitToIndex:
+ convFactor = 1 << (unitFromIndex - unitToIndex) * 10
+ return float(val) * convFactor
+ return val