summaryrefslogtreecommitdiff
path: root/swiftclient/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'swiftclient/utils.py')
-rw-r--r--swiftclient/utils.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/swiftclient/utils.py b/swiftclient/utils.py
index 33d89a5..a038dcc 100644
--- a/swiftclient/utils.py
+++ b/swiftclient/utils.py
@@ -25,3 +25,33 @@ def config_true_value(value):
"""
return value is True or \
(isinstance(value, basestring) and value.lower() in TRUE_VALUES)
+
+
+def prt_bytes(bytes, human_flag):
+ """
+ convert a number > 1024 to printable format, either in 4 char -h format as
+ with ls -lh or return as 12 char right justified string
+ """
+
+ if human_flag:
+ suffix = ''
+ mods = list('KMGTPEZY')
+ temp = float(bytes)
+ if temp > 0:
+ while (temp > 1023):
+ try:
+ suffix = mods.pop(0)
+ except IndexError:
+ break
+ temp /= 1024.0
+ if suffix != '':
+ if temp >= 10:
+ bytes = '%3d%s' % (temp, suffix)
+ else:
+ bytes = '%.1f%s' % (temp, suffix)
+ if suffix == '': # must be < 1024
+ bytes = '%4s' % bytes
+ else:
+ bytes = '%12s' % bytes
+
+ return(bytes)