diff options
author | Eugeni Dodonov <eugeni@mandriva.com> | 2009-10-13 10:15:41 -0300 |
---|---|---|
committer | Eugeni Dodonov <eugeni@mandriva.com> | 2009-10-13 10:15:41 -0300 |
commit | 1948857385fe40b069d889dda8e988bfbad56329 (patch) | |
tree | 5bb058b60a76317404b5b37c9014d866369881f5 | |
parent | 6e1769040e7ef1388134ef87e96cc52b9beaeb85 (diff) | |
download | net_monitor-1948857385fe40b069d889dda8e988bfbad56329.tar net_monitor-1948857385fe40b069d889dda8e988bfbad56329.tar.gz net_monitor-1948857385fe40b069d889dda8e988bfbad56329.tar.bz2 net_monitor-1948857385fe40b069d889dda8e988bfbad56329.tar.xz net_monitor-1948857385fe40b069d889dda8e988bfbad56329.zip |
properly format traffic numbers
-rw-r--r-- | src/monitor.py | 21 | ||||
-rwxr-xr-x | src/net_monitor | 15 |
2 files changed, 30 insertions, 6 deletions
diff --git a/src/monitor.py b/src/monitor.py index cef0771..4c7802b 100644 --- a/src/monitor.py +++ b/src/monitor.py @@ -23,6 +23,11 @@ class Monitor: # wireless modes modes = ['Auto', 'Ad-Hoc', 'Managed', 'Master', 'Repeat', 'Second', 'Monitor'] + # constants + SIZE_KB=1000 + SIZE_MB=1000**2 + SIZE_GB=1000**3 + def __init__(self): self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.net = {} @@ -161,9 +166,21 @@ class Monitor: bytes_out = 0 return bytes_in, bytes_out - def format_size(self, size): + def format_size(self, size, opt=""): """Pretty-Formats size""" - return size + # convert to float + size_f = size * 1.0 + pretty_size = None + pretty_bytes = "%d Bytes%s" % (size, opt) + if size > self.SIZE_GB: + pretty_size = "%0.2f GB%s" % (size_f / self.SIZE_GB, opt) + elif size > self.SIZE_MB: + pretty_size = "%0.2f MB%s" % (size_f / self.SIZE_MB, opt) + elif size > self.SIZE_KB: + pretty_size = "%0.2f KB%s" % (size_f / self.SIZE_KB, opt) + else: + pretty_size = pretty_bytes + return pretty_size, pretty_bytes def get_dns(self): """Returns list of DNS servers""" diff --git a/src/net_monitor b/src/net_monitor index 01dd0b1..5292406 100755 --- a/src/net_monitor +++ b/src/net_monitor @@ -439,10 +439,10 @@ class MonitorGui: histo_out = 0 # update widgets ip, mac = self.monitor.get_address(iface) - for widget, value in [('widget_in', total_in), - ('widget_out', total_out), - ('widget_speed_in', speed_in), - ('widget_speed_out', speed_out), + for widget, value in [('widget_in', self.monitor.format_size(total_in)), + ('widget_out', self.monitor.format_size(total_out)), + ('widget_speed_in', self.monitor.format_size(speed_in, "/s")), + ('widget_speed_out', self.monitor.format_size(speed_out, "/s")), ('widget_ip_address', ip), ('widget_status', status), ('widget_hw_address', mac), @@ -453,6 +453,13 @@ class MonitorGui: ('quality', "%d%%" % quality), ]: if widget in self.ifaces[iface]: + # is it absolute value or pretty-formatted number? + if value.__class__ == tuple: + pretty_size, pretty_bytes = value + if pretty_size == pretty_bytes: + value = pretty_size + else: + value = "%s (%s)" % (pretty_size, pretty_bytes) self.ifaces[iface][widget].set_text(str(value)) gobject.timeout_add(interval * 1000, self.update) |