From 87c0b170d40c9585daeff6615146e507fdd005a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20B=2E=20Moretti?= Date: Tue, 30 Apr 2019 19:50:16 -0300 Subject: [PATCH 1/9] Create an alternate network traffic module --- bumblebee/modules/network_traffic.py | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 bumblebee/modules/network_traffic.py diff --git a/bumblebee/modules/network_traffic.py b/bumblebee/modules/network_traffic.py new file mode 100644 index 0000000..1b0c315 --- /dev/null +++ b/bumblebee/modules/network_traffic.py @@ -0,0 +1,42 @@ +"""Displays network traffic +""" + +import psutil +import netifaces +import bytefmt + +import bumblebee.input +import bumblebee.output +import bumblebee.engine + +class Module(bumblebee.engine.Module): + def __init__(self, engine, config): + super(Module, self).__init__(engine, config, + bumblebee.output.Widget(full_text=self.utilization) + ) + + self._default_adapter = netifaces.gateways()['default'][netifaces.AF_INET][1] + + self._download_tx, self._upload_tx = self.network_tx() + + def utilization(self, widget): + return "{0} {1}".format( + bytefmt.humanize(self._final_download_tx), + bytefmt.humanize(self._final_upload_tx) + ) + + def update(self, widgets): + download_tx, upload_tx = self.network_tx() + + self._final_download_tx = (download_tx - self._download_tx) + self._final_upload_tx = (upload_tx - self._upload_tx) + + self._download_tx, self._upload_tx = download_tx, upload_tx + + def network_tx(self): + io_counters = psutil.net_io_counters(pernic=True) + network = io_counters[self._default_adapter] + + return network.bytes_recv, network.bytes_sent + + From a2fb344aa765932f54ba857f5e7d6f409c78493a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20B=2E=20Moretti?= Date: Mon, 13 May 2019 20:14:13 -0300 Subject: [PATCH 2/9] Create widget with minwidth --- bumblebee/modules/network_traffic.py | 73 ++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 20 deletions(-) diff --git a/bumblebee/modules/network_traffic.py b/bumblebee/modules/network_traffic.py index 1b0c315..7a3fd27 100644 --- a/bumblebee/modules/network_traffic.py +++ b/bumblebee/modules/network_traffic.py @@ -3,40 +3,73 @@ import psutil import netifaces -import bytefmt import bumblebee.input import bumblebee.output import bumblebee.engine +import bumblebee.util class Module(bumblebee.engine.Module): def __init__(self, engine, config): - super(Module, self).__init__(engine, config, - bumblebee.output.Widget(full_text=self.utilization) - ) + super(Module, self).__init__(engine, config) - self._default_adapter = netifaces.gateways()['default'][netifaces.AF_INET][1] + self._bandwidth = BandwidthInfo() - self._download_tx, self._upload_tx = self.network_tx() - - def utilization(self, widget): - return "{0} {1}".format( - bytefmt.humanize(self._final_download_tx), - bytefmt.humanize(self._final_upload_tx) - ) + self._download_tx = self._bandwidth.bytes_recv() + self._upload_tx = self._bandwidth.bytes_sent() def update(self, widgets): - download_tx, upload_tx = self.network_tx() - - self._final_download_tx = (download_tx - self._download_tx) - self._final_upload_tx = (upload_tx - self._upload_tx) + 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 - def network_tx(self): - io_counters = psutil.net_io_counters(pernic=True) - network = io_counters[self._default_adapter] + def update_widgets(self, widgets, download_rate, upload_rate): + del widgets[:] - return network.bytes_recv, network.bytes_sent + widgets.extend(( + TrafficWidget(download_rate, ''), + TrafficWidget(upload_rate, '') + )) +class BandwidthInfo: + 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) + + +class TrafficWidget: + def __new__(self, text, icon): + widget = bumblebee.output.Widget() + widget.set('theme.minwidth', '00000000KiB/s') + widget.full_text(self.humanize(text, icon)) + + return widget + + @staticmethod + def humanize(text, icon): + humanized_byte_format = bumblebee.util.bytefmt(text) + return '{0} {1}/s'.format(icon, humanized_byte_format) From e9afaa2e1be3702f8a7f348d204e010fd3ab6b7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20B=2E=20Moretti?= Date: Mon, 13 May 2019 20:29:09 -0300 Subject: [PATCH 3/9] Update widget build to use keyword args --- bumblebee/modules/network_traffic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bumblebee/modules/network_traffic.py b/bumblebee/modules/network_traffic.py index 7a3fd27..8ebe77c 100644 --- a/bumblebee/modules/network_traffic.py +++ b/bumblebee/modules/network_traffic.py @@ -33,8 +33,8 @@ class Module(bumblebee.engine.Module): del widgets[:] widgets.extend(( - TrafficWidget(download_rate, ''), - TrafficWidget(upload_rate, '') + TrafficWidget(text=download_rate, icon=''), + TrafficWidget(text=upload_rate, icon='') )) From c0706070abc007397285138b24cdb9723a8dfed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20B=2E=20Moretti?= Date: Mon, 13 May 2019 20:32:16 -0300 Subject: [PATCH 4/9] Add encoding header --- bumblebee/modules/network_traffic.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bumblebee/modules/network_traffic.py b/bumblebee/modules/network_traffic.py index 8ebe77c..a107693 100644 --- a/bumblebee/modules/network_traffic.py +++ b/bumblebee/modules/network_traffic.py @@ -1,3 +1,6 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + """Displays network traffic """ From 3031077713139ef4c576a122f5392c517724bf3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20B=2E=20Moretti?= Date: Mon, 13 May 2019 20:53:31 -0300 Subject: [PATCH 5/9] Add Python 2.7 support --- bumblebee/modules/network_traffic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bumblebee/modules/network_traffic.py b/bumblebee/modules/network_traffic.py index a107693..06e8f73 100644 --- a/bumblebee/modules/network_traffic.py +++ b/bumblebee/modules/network_traffic.py @@ -41,7 +41,7 @@ class Module(bumblebee.engine.Module): )) -class BandwidthInfo: +class BandwidthInfo(object): def __init__(self): io_counters = self.io_counters() self.network = io_counters[self.default_network_adapter()] @@ -64,7 +64,7 @@ class BandwidthInfo: return psutil.net_io_counters(pernic=True) -class TrafficWidget: +class TrafficWidget(object): def __new__(self, text, icon): widget = bumblebee.output.Widget() widget.set('theme.minwidth', '00000000KiB/s') From 5dec1adc977a8b23b8c3666e07b7d7ed60a3f54b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20B=2E=20Moretti?= Date: Fri, 21 Jun 2019 13:09:58 -0300 Subject: [PATCH 6/9] Suppress errors when no gateways are found --- bumblebee/modules/network_traffic.py | 35 +++++++++++++++++----------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/bumblebee/modules/network_traffic.py b/bumblebee/modules/network_traffic.py index 06e8f73..548deb9 100644 --- a/bumblebee/modules/network_traffic.py +++ b/bumblebee/modules/network_traffic.py @@ -16,21 +16,27 @@ class Module(bumblebee.engine.Module): def __init__(self, engine, config): super(Module, self).__init__(engine, config) - self._bandwidth = BandwidthInfo() + try: + self._bandwidth = BandwidthInfo() - self._download_tx = self._bandwidth.bytes_recv() - self._upload_tx = self._bandwidth.bytes_sent() + self._download_tx = self._bandwidth.bytes_recv() + self._upload_tx = self._bandwidth.bytes_sent() + except: + pass def update(self, widgets): - download_tx = self._bandwidth.bytes_recv() - upload_tx = self._bandwidth.bytes_sent() + try: + 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) + download_rate = (download_tx - self._download_tx) + upload_rate = (upload_tx - self._upload_tx) - self.update_widgets(widgets, download_rate, upload_rate) + self.update_widgets(widgets, download_rate, upload_rate) - self._download_tx, self._upload_tx = download_tx, upload_tx + self._download_tx, self._upload_tx = download_tx, upload_tx + except: + pass def update_widgets(self, widgets, download_rate, upload_rate): del widgets[:] @@ -43,8 +49,7 @@ class Module(bumblebee.engine.Module): class BandwidthInfo(object): def __init__(self): - io_counters = self.io_counters() - self.network = io_counters[self.default_network_adapter()] + self.bandwidth() def bytes_recv(self): return self.bandwidth().bytes_recv @@ -57,8 +62,12 @@ class BandwidthInfo(object): return io_counters[self.default_network_adapter()] def default_network_adapter(self): - gateways = netifaces.gateways() - return gateways['default'][netifaces.AF_INET][1] + gateway = netifaces.gateways()['default'] + + if (len(gateway) == 0): + raise 'No default gateway found' + + return gateway[netifaces.AF_INET][1] def io_counters(self): return psutil.net_io_counters(pernic=True) From 55478e2e8d202de22ba68e6ae8ba53de6f4cd810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20B=2E=20Moretti?= Date: Fri, 21 Jun 2019 13:14:49 -0300 Subject: [PATCH 7/9] Remove useless __new__ --- bumblebee/modules/network_traffic.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/bumblebee/modules/network_traffic.py b/bumblebee/modules/network_traffic.py index 548deb9..ad819ec 100644 --- a/bumblebee/modules/network_traffic.py +++ b/bumblebee/modules/network_traffic.py @@ -48,9 +48,6 @@ class Module(bumblebee.engine.Module): class BandwidthInfo(object): - def __init__(self): - self.bandwidth() - def bytes_recv(self): return self.bandwidth().bytes_recv From 21e2f46564648cb473ec7d23a7b57fce03f7e00b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20B=2E=20Moretti?= Date: Tue, 9 Jul 2019 22:27:59 -0300 Subject: [PATCH 8/9] Add network traffic icons & minor refactor --- bumblebee/modules/network_traffic.py | 60 +++++++++++++++++++++------- themes/icons/ascii.json | 4 ++ themes/icons/awesome-fonts.json | 4 ++ themes/icons/ionicons.json | 4 ++ 4 files changed, 57 insertions(+), 15 deletions(-) diff --git a/bumblebee/modules/network_traffic.py b/bumblebee/modules/network_traffic.py index ad819ec..a8aead2 100644 --- a/bumblebee/modules/network_traffic.py +++ b/bumblebee/modules/network_traffic.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- """Displays network traffic +No extra configuratio needed. """ import psutil @@ -12,7 +13,11 @@ import bumblebee.output import bumblebee.engine import bumblebee.util +WIDGET_NAME = 'network_traffic' + class Module(bumblebee.engine.Module): + """Bumblebee main module """ + def __init__(self, engine, config): super(Module, self).__init__(engine, config) @@ -21,9 +26,21 @@ class Module(bumblebee.engine.Module): self._download_tx = self._bandwidth.bytes_recv() self._upload_tx = self._bandwidth.bytes_sent() - except: + except Exception: + """ We do not want do explode anything """ pass + @classmethod + def state(cls, widget): + """Return the widget state""" + + if widget.name == '{}.rx'.format(WIDGET_NAME): + return 'rx' + elif widget.name == '{}.tx'.format(WIDGET_NAME): + return 'tx' + + return None + def update(self, widgets): try: download_tx = self._bandwidth.bytes_recv() @@ -35,50 +52,63 @@ class Module(bumblebee.engine.Module): self.update_widgets(widgets, download_rate, upload_rate) self._download_tx, self._upload_tx = download_tx, upload_tx - except: + except Exception: + """ We do not want do explode anything """ pass - def update_widgets(self, widgets, download_rate, upload_rate): + @classmethod + def update_widgets(cls, widgets, download_rate, upload_rate): + """Update tx/rx widgets with new rates""" del widgets[:] widgets.extend(( - TrafficWidget(text=download_rate, icon=''), - TrafficWidget(text=upload_rate, icon='') + TrafficWidget(text=download_rate, direction='tx'), + TrafficWidget(text=upload_rate, direction='rx') )) class BandwidthInfo(object): + """Get received/sent bytes from network adapter""" + def bytes_recv(self): + """Return received bytes""" return self.bandwidth().bytes_recv def bytes_sent(self): + """Return sent bytes""" return self.bandwidth().bytes_sent def bandwidth(self): + """Return bandwidth information""" io_counters = self.io_counters() return io_counters[self.default_network_adapter()] - def default_network_adapter(self): + @classmethod + def default_network_adapter(cls): + """Return default active network adapter""" gateway = netifaces.gateways()['default'] - if (len(gateway) == 0): + if not gateway: raise 'No default gateway found' return gateway[netifaces.AF_INET][1] - def io_counters(self): + @classmethod + def io_counters(cls): + """Return IO counters""" return psutil.net_io_counters(pernic=True) - class TrafficWidget(object): - def __new__(self, text, icon): - widget = bumblebee.output.Widget() - widget.set('theme.minwidth', '00000000KiB/s') - widget.full_text(self.humanize(text, icon)) + """Create a traffic widget with humanized bytes string with proper icon (up/down)""" + def __new__(cls, text, direction): + widget = bumblebee.output.Widget(name='{0}.{1}'.format(WIDGET_NAME, direction)) + widget.set('theme.minwidth', '0000000KiB/s') + widget.full_text(cls.humanize(text)) return widget @staticmethod - def humanize(text, icon): + def humanize(text): + """Return humanized bytes""" humanized_byte_format = bumblebee.util.bytefmt(text) - return '{0} {1}/s'.format(icon, humanized_byte_format) + return '{0}/s'.format(humanized_byte_format) diff --git a/themes/icons/ascii.json b/themes/icons/ascii.json index 6358442..9f345b5 100644 --- a/themes/icons/ascii.json +++ b/themes/icons/ascii.json @@ -92,6 +92,10 @@ "rx": { "prefix": "down"}, "tx": { "prefix": "up"} }, + "network_traffic": { + "rx": { "prefix": "down" }, + "tx": { "prefix": "up" } + }, "mpd": { "playing": { "prefix": ">" }, "paused": { "prefix": "||" }, diff --git a/themes/icons/awesome-fonts.json b/themes/icons/awesome-fonts.json index 5513466..f436179 100644 --- a/themes/icons/awesome-fonts.json +++ b/themes/icons/awesome-fonts.json @@ -145,6 +145,10 @@ "rx": { "prefix": "" }, "tx": { "prefix": "" } }, + "network_traffic": { + "rx": { "prefix": "" }, + "tx": { "prefix": "" } + }, "mpd": { "playing": { "prefix": "" }, "paused": { "prefix": "" }, diff --git a/themes/icons/ionicons.json b/themes/icons/ionicons.json index f2f8fc1..bd9ee4b 100644 --- a/themes/icons/ionicons.json +++ b/themes/icons/ionicons.json @@ -129,6 +129,10 @@ "rx": { "prefix": "\uf365" }, "tx": { "prefix": "\uf35f" } }, + "network_traffic": { + "rx": { "prefix": "\uf365" }, + "tx": { "prefix": "\uf35f" } + }, "mpd": { "playing": { "prefix": "\uf488" }, "paused": { "prefix": "\uf210" }, From 92a7d95fd749fcc0ad7dbc660250ab7f3ae9194b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20B=2E=20Moretti?= Date: Tue, 9 Jul 2019 22:29:34 -0300 Subject: [PATCH 9/9] Fix comment typo --- bumblebee/modules/network_traffic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bumblebee/modules/network_traffic.py b/bumblebee/modules/network_traffic.py index a8aead2..0d2067a 100644 --- a/bumblebee/modules/network_traffic.py +++ b/bumblebee/modules/network_traffic.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- """Displays network traffic -No extra configuratio needed. + * No extra configuration needed """ import psutil