diff options
| author | Mattias Andrée <maandree@operamail.com> | 2014-03-08 23:17:39 +0100 | 
|---|---|---|
| committer | Mattias Andrée <maandree@operamail.com> | 2014-03-08 23:17:39 +0100 | 
| commit | 27687ab21d6e956845d66125cf429a22b687df36 (patch) | |
| tree | 46fc8a721ccaa72c12d814563ad1a357388abaa4 | |
| parent | forgot to set intro comment (diff) | |
| download | xpybar-27687ab21d6e956845d66125cf429a22b687df36.tar.gz xpybar-27687ab21d6e956845d66125cf429a22b687df36.tar.bz2 xpybar-27687ab21d6e956845d66125cf429a22b687df36.tar.xz  | |
network demo
Signed-off-by: Mattias Andrée <maandree@operamail.com>
| -rw-r--r-- | examples/plugins/network | 112 | 
1 files changed, 112 insertions, 0 deletions
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) +  | 
