From 14943a69cb5a0161a6646d1bcf44cad8f2099b60 Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Fri, 21 Apr 2017 10:46:16 +0530 Subject: [PATCH 1/6] traffic module added --- bumblebee/modules/traffic.py | 45 +++++++++++++++++++++++++++++++++ themes/icons/awesome-fonts.json | 3 +++ 2 files changed, 48 insertions(+) create mode 100644 bumblebee/modules/traffic.py diff --git a/bumblebee/modules/traffic.py b/bumblebee/modules/traffic.py new file mode 100644 index 0000000..6e2a6b2 --- /dev/null +++ b/bumblebee/modules/traffic.py @@ -0,0 +1,45 @@ +import netifaces +import re + +import bumblebee.util +import bumblebee.input +import bumblebee.output +import bumblebee.engine + +class Module(bumblebee.engine.Module): + def __init__(self, engine, config): + widgets = [] + super(Module, self).__init__(engine, config, widgets) + self._exclude = tuple(filter(len, self.parameter("exclude", "lo,virbr,docker,vboxnet,veth").split(","))) + self._update_widgets(widgets) + + def update(self, widgets): + self._update_widgets(widgets) + + def _update_widgets(self, widgets): + _ifconfdata = bumblebee.util.execute('ifconfig') + interfaces = [ i for i in netifaces.interfaces() if not i.startswith(self._exclude) ] + + interface = interfaces[0] + if interface is '': + interface = 'lo' + + _block = re.compile(r"" + interface + ":(.*\n)*", re.MULTILINE) + _down = re.compile(r"RX packets .* bytes (.*) \(", re.MULTILINE) + _current_down = re.search(_down,re.search(_block,_ifconfdata).group(0)).group(1) + _up = re.compile(r"TX packets .* bytes (.*) \(", re.MULTILINE) + _current_up = re.search(_up,re.search(_block,_ifconfdata).group(0)).group(1) + + widget = self.widget("traffic") + if not widget: + widget = bumblebee.output.Widget(name="traffic") + widgets.append(widget) + _prev_up = widget.get("absup", 0) + _prev_down = widget.get("absdown", 0) + _speed_down = bumblebee.util.bytefmt(int(_current_down) - int(_prev_down)) + _speed_up = bumblebee.util.bytefmt(int(_current_up) - int(_prev_up)) + widget.full_text("{} {} {}".format(interface, _speed_down, _speed_up)) + widget.set("absdown", _current_down) + widget.set("absup", _current_up) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/themes/icons/awesome-fonts.json b/themes/icons/awesome-fonts.json index b3b2867..b12f73e 100644 --- a/themes/icons/awesome-fonts.json +++ b/themes/icons/awesome-fonts.json @@ -78,5 +78,8 @@ }, "sensors": { "prefix": "🌡" + }, + "traffic":{ + "prefix": "" } } From ed3d9bd5954c3e2d15ed7d225c966912f73a02e6 Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Fri, 21 Apr 2017 10:52:34 +0530 Subject: [PATCH 2/6] remove interface name from traffic --- bumblebee/modules/traffic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bumblebee/modules/traffic.py b/bumblebee/modules/traffic.py index 6e2a6b2..067f4a5 100644 --- a/bumblebee/modules/traffic.py +++ b/bumblebee/modules/traffic.py @@ -38,7 +38,7 @@ class Module(bumblebee.engine.Module): _prev_down = widget.get("absdown", 0) _speed_down = bumblebee.util.bytefmt(int(_current_down) - int(_prev_down)) _speed_up = bumblebee.util.bytefmt(int(_current_up) - int(_prev_up)) - widget.full_text("{} {} {}".format(interface, _speed_down, _speed_up)) + widget.full_text("{} {}".format(_speed_down, _speed_up)) widget.set("absdown", _current_down) widget.set("absup", _current_up) From d3e24c0eeacd83d3ac8b94a6be02d408bea050fd Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Fri, 21 Apr 2017 10:59:25 +0530 Subject: [PATCH 3/6] add ascii icon for traffic --- themes/icons/ascii.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/themes/icons/ascii.json b/themes/icons/ascii.json index c29c6d5..c50cbce 100644 --- a/themes/icons/ascii.json +++ b/themes/icons/ascii.json @@ -60,4 +60,7 @@ "redshift": { "day": { "prefix": "day" }, "night": { "prefix": "night" }, "transition": { "prefix": "trans" } } + "traffic": { + "prefix": "traffic" + } } From a8693dcadaf296b58ff9547d9e9dbfca606de44d Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Fri, 21 Apr 2017 11:40:02 +0530 Subject: [PATCH 4/6] seperate up and down speeds for traffic widget --- bumblebee/modules/traffic.py | 38 ++++++++++++++++++++++++--------- themes/icons/awesome-fonts.json | 3 ++- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/bumblebee/modules/traffic.py b/bumblebee/modules/traffic.py index 067f4a5..d076854 100644 --- a/bumblebee/modules/traffic.py +++ b/bumblebee/modules/traffic.py @@ -8,10 +8,21 @@ import bumblebee.engine class Module(bumblebee.engine.Module): def __init__(self, engine, config): - widgets = [] + widgets = [ + bumblebee.output.Widget(name="traffic.down"), + bumblebee.output.Widget(name="traffic.up"), + ] super(Module, self).__init__(engine, config, widgets) self._exclude = tuple(filter(len, self.parameter("exclude", "lo,virbr,docker,vboxnet,veth").split(","))) self._update_widgets(widgets) + self._status = None + + def state(self, widget): + if widget.name == "traffic.down": + return "down" + if widget.name == "traffic.up": + return "up" + return self._status def update(self, widgets): self._update_widgets(widgets) @@ -30,16 +41,23 @@ class Module(bumblebee.engine.Module): _up = re.compile(r"TX packets .* bytes (.*) \(", re.MULTILINE) _current_up = re.search(_up,re.search(_block,_ifconfdata).group(0)).group(1) - widget = self.widget("traffic") - if not widget: - widget = bumblebee.output.Widget(name="traffic") - widgets.append(widget) - _prev_up = widget.get("absup", 0) - _prev_down = widget.get("absdown", 0) + widget_down = self.widget("traffic.down") + widget_up = self.widget("traffic.up") + if not widget_down: + widget_down = bumblebee.output.Widget(name="traffic.down") + widgets.append(widget_down) + if not widget_up: + widget_up = bumblebee.output.Widget(name="traffic.down") + widgets.append(widget_up) + + _prev_down = widget_down.get("absdown", 0) _speed_down = bumblebee.util.bytefmt(int(_current_down) - int(_prev_down)) + widget_down.full_text("{}".format(_speed_down)) + widget_down.set("absdown", _current_down) + + _prev_up = widget_up.get("absup", 0) _speed_up = bumblebee.util.bytefmt(int(_current_up) - int(_prev_up)) - widget.full_text("{} {}".format(_speed_down, _speed_up)) - widget.set("absdown", _current_down) - widget.set("absup", _current_up) + widget_up.full_text("{}".format(_speed_up)) + widget_up.set("absup", _current_up) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/themes/icons/awesome-fonts.json b/themes/icons/awesome-fonts.json index b12f73e..526b6b7 100644 --- a/themes/icons/awesome-fonts.json +++ b/themes/icons/awesome-fonts.json @@ -80,6 +80,7 @@ "prefix": "🌡" }, "traffic":{ - "prefix": "" + "down": { "prefix": "" }, + "up": { "prefix": "" } } } From 8c45a63b4780ceb90f7098e625f5c1d2ab175f5c Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Fri, 21 Apr 2017 11:44:25 +0530 Subject: [PATCH 5/6] update ascii icons for traffic --- themes/icons/ascii.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/themes/icons/ascii.json b/themes/icons/ascii.json index c50cbce..3e4a559 100644 --- a/themes/icons/ascii.json +++ b/themes/icons/ascii.json @@ -61,6 +61,7 @@ "day": { "prefix": "day" }, "night": { "prefix": "night" }, "transition": { "prefix": "trans" } } "traffic": { - "prefix": "traffic" + "down": { "prefix": "down"}, + "up": {"prefix": "up"} } } From bc26cd5dd6a63e07bf22a7e594ecaf84c951c4db Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Fri, 21 Apr 2017 14:52:19 +0530 Subject: [PATCH 6/6] basic exception handling --- bumblebee/modules/traffic.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/bumblebee/modules/traffic.py b/bumblebee/modules/traffic.py index d076854..2a2509f 100644 --- a/bumblebee/modules/traffic.py +++ b/bumblebee/modules/traffic.py @@ -35,11 +35,15 @@ class Module(bumblebee.engine.Module): if interface is '': interface = 'lo' - _block = re.compile(r"" + interface + ":(.*\n)*", re.MULTILINE) - _down = re.compile(r"RX packets .* bytes (.*) \(", re.MULTILINE) - _current_down = re.search(_down,re.search(_block,_ifconfdata).group(0)).group(1) - _up = re.compile(r"TX packets .* bytes (.*) \(", re.MULTILINE) - _current_up = re.search(_up,re.search(_block,_ifconfdata).group(0)).group(1) + try: + _block = re.compile(r"" + interface + ":(.*\n)*", re.MULTILINE) + _down = re.compile(r"RX packets .* bytes (.*) \(", re.MULTILINE) + _current_down = re.search(_down,re.search(_block,_ifconfdata).group(0)).group(1) + _up = re.compile(r"TX packets .* bytes (.*) \(", re.MULTILINE) + _current_up = re.search(_up,re.search(_block,_ifconfdata).group(0)).group(1) + except: + _current_up = -1 + _current_down = -1 widget_down = self.widget("traffic.down") widget_up = self.widget("traffic.up") @@ -51,13 +55,21 @@ class Module(bumblebee.engine.Module): widgets.append(widget_up) _prev_down = widget_down.get("absdown", 0) - _speed_down = bumblebee.util.bytefmt(int(_current_down) - int(_prev_down)) + if _current_down is not -1: + _speed_down = bumblebee.util.bytefmt(int(_current_down) - int(_prev_down)) + widget_down.set("absdown", _current_down) + else: + _speed_down = bumblebee.util.bytefmt(0) + widget_down.set("absdown", _prev_down) widget_down.full_text("{}".format(_speed_down)) - widget_down.set("absdown", _current_down) _prev_up = widget_up.get("absup", 0) - _speed_up = bumblebee.util.bytefmt(int(_current_up) - int(_prev_up)) + if _current_up is not -1: + _speed_up = bumblebee.util.bytefmt(int(_current_up) - int(_prev_up)) + widget_up.set("absup", _current_up) + else: + _speed_up = bumblebee.util.bytefmt(0) + widget_up.set("absup", _prev_up) widget_up.full_text("{}".format(_speed_up)) - widget_up.set("absup", _current_up) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4