From 8431b7dba0a0e32fba4cf1b5053ece0698cc10b8 Mon Sep 17 00:00:00 2001 From: Daniel Wagner Date: Thu, 8 Jul 2010 14:17:41 +0200 Subject: [PATCH] Make test-counter byte counters human readable --- test/test-counter | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/test/test-counter b/test/test-counter index c32cb9e..7c2fa4a 100755 --- a/test/test-counter +++ b/test/test-counter @@ -7,6 +7,22 @@ import dbus import dbus.service import dbus.mainloop.glib +def make_bytes_readable(bytes): + SUFFIXES = [ 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB' ] + size = 1024 + + if bytes < size: + return '' + + for suffix in SUFFIXES: + if bytes > size * 1024: + size = size * 1024 + continue + + return '%.1f %s' % (bytes / float(size), suffix) + + return '' + class Counter(dbus.service.Object): @dbus.service.method("org.moblin.connman.Counter", in_signature='', out_signature='') @@ -20,7 +36,14 @@ class Counter(dbus.service.Object): print "%s" % (path) for key in stats.keys(): val = int(stats[key]) - print " %s = %s" % (key, val) + str = " %s = %s" % (key, val) + + if key in ["RX.Bytes", "TX.Bytes"]: + hstr = make_bytes_readable(val) + if hstr: + str = "%s (%s)" % (str, hstr) + + print str if __name__ == '__main__': dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) -- 2.7.4