From 27687ab21d6e956845d66125cf429a22b687df36 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 8 Mar 2014 23:17:39 +0100 Subject: network demo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- examples/plugins/network | 112 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 examples/plugins/network diff --git a/examples/plugins/network b/examples/plugins/network new file mode 100644 index 0000000..2bfb76b --- /dev/null +++ b/examples/plugins/network @@ -0,0 +1,112 @@ +# -*- python -*- + +# A xpybar configuration example testing the features of plugins.network + +import time +import threading + +from plugins.network import Network +from plugins.clock import Clock + + +exclude = [] + + +OUTPUT, HEIGHT, YPOS, TOP = 0, (len(Network(*exclude).devices) + 1) * 12, 24, True + + +clock = Clock(sync_to = Clock.SECONDS) + +start_ = start +def start(): + start_() + async(lambda : clock.continuous_sync(lambda : bar.invalidate())) + + +net_time = time.monotonic() +net_last = {} +def redraw(): + global net_time, net_last + + net_now = time.monotonic() + net_tdiff, net_time = net_now - net_time, net_now + net_ = Network(*exclude).devices + + def colourise(value): + colour = '39' + if value > 40: colour = '32' + if value > 8000: colour = '33' + if value > 60000: colour = '31' + return '\033[%sm%4.0f\033[0m' % (colour, value) + + def kbps(device, direction): + direction += '_bytes' + value = net_[device][direction] + if device in net_last: + value -= net_last[device][direction] + else: + value = 0 + value /= 128 * net_tdiff + return colourise(value) + + def KBps(device, direction): + direction += '_bytes' + value = net_[device][direction] + if device in net_last: + value -= net_last[device][direction] + else: + value = 0 + value /= 1024 * net_tdiff + return value + + def u(b): + unit = 0 + units = ['', 'K', 'M', 'G', 'T', 'P', 'E'] + while (unit < len(units)) and (b >= 1024): + b /= 1024 + unit += 1 + return '%.0f%s' % (b, units[unit]) + + def total(device, direction): + direction += '_bytes' + value = net_[device][direction] + return u(value) + 'B' + + def extra(device): + rc = [] + table = net_[device] + for key in table.keys(): + if key[3:] == 'bytes': + continue + if key.startswith('rx_'): + if ('tx_' + key[3:]) in table: + rc.append('%s:%s↓%s↑' % (key[3:], u(table[key]), u(table['tx_' + key[3:]]))) + else: + rc.append('%s:%s↓' % (key[3:], u(table[key]))) + elif key.startswith('tx_'): + if ('rx_' + key[3:]) not in table: + rc.append('%s:%s↑' % (key[3:], u(table[key]))) + return ' '.join(sorted(rc)) + + devsum = {} + for dev in net_.keys(): + table = net_[dev] + for key in table.keys(): + if key not in devsum: + devsum[key] = 0 + devsum[key] += table[key] + net_['total'] = devsum + + net = [(dev, + kbps(dev, 'rx'), KBps(dev, 'rx'), total(dev, 'rx'), + kbps(dev, 'tx'), KBps(dev, 'tx'), total(dev, 'tx'), + extra(dev)) + for dev in net_.keys()] + text = ['%6s: %skbps(%3.0fKB/s %s)↓ %skbps(%3.0fKB/s %s)↑ %s' % x for x in net if not x[0] == 'total'] + text += ['%6s: %skbps(%3.0fKB/s %s)↓ %skbps(%3.0fKB/s %s)↑ %s' % x for x in net if x[0] == 'total'] + text = '\n'.join(text) + net_last = net_ + + bar.clear() + bar.draw_coloured_text(0, 10, 0, 2, text) + -- cgit v1.2.3-70-g09d2