diff options
Diffstat (limited to 'glusternagios/utils.py')
-rw-r--r-- | glusternagios/utils.py | 17 |
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 |