bumblebee-status/bumblebee/modules/network_traffic.py

79 lines
2.1 KiB
Python
Raw Normal View History

2019-05-14 01:32:16 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Displays network traffic
"""
import psutil
import netifaces
import bumblebee.input
import bumblebee.output
import bumblebee.engine
2019-05-14 01:14:13 +02:00
import bumblebee.util
class Module(bumblebee.engine.Module):
def __init__(self, engine, config):
2019-05-14 01:14:13 +02:00
super(Module, self).__init__(engine, config)
2019-05-14 01:14:13 +02:00
self._bandwidth = BandwidthInfo()
2019-05-14 01:14:13 +02:00
self._download_tx = self._bandwidth.bytes_recv()
self._upload_tx = self._bandwidth.bytes_sent()
def update(self, widgets):
2019-05-14 01:14:13 +02:00
download_tx = self._bandwidth.bytes_recv()
upload_tx = self._bandwidth.bytes_sent()
download_rate = (download_tx - self._download_tx)
upload_rate = (upload_tx - self._upload_tx)
self.update_widgets(widgets, download_rate, upload_rate)
self._download_tx, self._upload_tx = download_tx, upload_tx
2019-05-14 01:14:13 +02:00
def update_widgets(self, widgets, download_rate, upload_rate):
del widgets[:]
widgets.extend((
TrafficWidget(text=download_rate, icon=''),
TrafficWidget(text=upload_rate, icon='')
2019-05-14 01:14:13 +02:00
))
2019-05-14 01:53:31 +02:00
class BandwidthInfo(object):
2019-05-14 01:14:13 +02:00
def __init__(self):
io_counters = self.io_counters()
self.network = io_counters[self.default_network_adapter()]
def bytes_recv(self):
return self.bandwidth().bytes_recv
def bytes_sent(self):
return self.bandwidth().bytes_sent
def bandwidth(self):
io_counters = self.io_counters()
return io_counters[self.default_network_adapter()]
def default_network_adapter(self):
gateways = netifaces.gateways()
return gateways['default'][netifaces.AF_INET][1]
def io_counters(self):
return psutil.net_io_counters(pernic=True)
2019-05-14 01:53:31 +02:00
class TrafficWidget(object):
2019-05-14 01:14:13 +02:00
def __new__(self, text, icon):
widget = bumblebee.output.Widget()
widget.set('theme.minwidth', '00000000KiB/s')
widget.full_text(self.humanize(text, icon))
2019-05-14 01:14:13 +02:00
return widget
2019-05-14 01:14:13 +02:00
@staticmethod
def humanize(text, icon):
humanized_byte_format = bumblebee.util.bytefmt(text)
return '{0} {1}/s'.format(icon, humanized_byte_format)