From b24832c22aa44d2f8b5ecddaf12e7878653af28f Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Wed, 7 Nov 2012 19:39:43 +0100 Subject: Make image sizes more readable for humans By introducing the parameter --human-readable for several functions (image-list, image-show, image-update, image-create) it's possible to convert the size in bytes to something more readable like 9.309MB or 1.375GB. Change-Id: I4e2654994361dcf330ed6d681dbed73388f159cb --- glanceclient/common/utils.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'glanceclient/common/utils.py') diff --git a/glanceclient/common/utils.py b/glanceclient/common/utils.py index 31b8530..628225d 100644 --- a/glanceclient/common/utils.py +++ b/glanceclient/common/utils.py @@ -168,3 +168,15 @@ def integrity_iter(iter, checksum): raise IOError(errno.EPIPE, 'Corrupt image download. Checksum was %s expected %s' % (md5sum, checksum)) + + +def make_size_human_readable(size): + suffix = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB'] + base = 1024.0 + + index = 0 + while size > base: + index = index + 1 + size = size / base + + return "%.3f%s" % (size, suffix[index]) -- cgit v1.2.1