From 890e8a91bce3be5415786e3cdff1ebb898a32928 Mon Sep 17 00:00:00 2001 From: enkore Date: Thu, 7 Mar 2013 18:09:05 +0100 Subject: [PATCH] Add network module (3.4), close #4 --- README.md | 41 +++++++++++++++++++++-- i3pystatus/network.py | 75 +++++++++++++++++++++++++++++++++++++++++++ setup.py | 2 +- 3 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 i3pystatus/network.py diff --git a/README.md b/README.md index 37d1828..97283f9 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ Available formatters for format and alert_format_\*: * `battery_ident` — (default: `BAT0`) -* `format` — (default: `{status} {remaining}`) +* `format` — (default: `{status} {remaining_hm}`) * `alert` — Display a libnotify-notification on low battery (default: `False`) * `alert_percentage` — (default: `10`) * `alert_format_title` — (default: `Low battery`) @@ -224,7 +224,6 @@ The `backends` setting determines the backends to use. Currently available are: > > Requires > * python-dbus -> * python-gobject2 > > > @@ -247,6 +246,44 @@ unread posts in any bookmark in the mods.de forums. +### network + + +Display network information about a interface. + +Requires the PyPI package `netifaces-py3`. + +Available formatters: +* {inteface} same as setting +* {name} same as setting +* {v4} IPv4 address +* {v4mask} subnet mask +* {v6} IPv6 address +* {v6mask} subnet mask +* {mac} MAC of interface + +Not available addresses (i.e. no IPv6 connectivity) are replaced with empty strings. + + +* `interface` — Interface to obtain information for, i.e. eth0 (default: `eth0`) +* `format_up` — (default: `{interface}: {v4}`) +* `color_up` — (default: `#00FF00`) +* `format_down` — (default: `{interface}`) +* `color_down` — (default: `#FF0000`) +* `name` — (default: `eth0`) + + + +### parcel + + + +* `instance` — Tracker instance +* `format` — (default: `{name}:{progress}`) +* `name` + + + ### regex diff --git a/i3pystatus/network.py b/i3pystatus/network.py new file mode 100644 index 0000000..2f9ca0e --- /dev/null +++ b/i3pystatus/network.py @@ -0,0 +1,75 @@ + +from itertools import zip_longest + +# PyPI: netifaces-py3 +import netifaces + +from i3pystatus import IntervalModule + +class Network(IntervalModule): + """ + Display network information about a interface. + + Requires the PyPI package `netifaces-py3`. + + Available formatters: + * {inteface} same as setting + * {name} same as setting + * {v4} IPv4 address + * {v4mask} subnet mask + * {v6} IPv6 address + * {v6mask} subnet mask + * {mac} MAC of interface + + Not available addresses (i.e. no IPv6 connectivity) are replaced with empty strings. + """ + + settings = ( + ("interface", "Interface to obtain information for, i.e. eth0"), + "format_up", "color_up", + "format_down", "color_down", + "name" + ) + + name = interface = "eth0" + format_up = "{interface}: {v4}" + format_down = "{interface}" + color_up = "#00FF00" + color_down = "#FF0000" + + def init(self): + if self.interface not in netifaces.interfaces(): + raise RuntimeError("Unknown inteface {iface}!".format(iface=self.inteface)) + + self.baseinfo = { + "interface": self.interface, + "name": self.name, + "mac": netifaces.ifaddresses(self.interface)[netifaces.AF_PACKET][0]["addr"], + } + + def run(self): + info = netifaces.ifaddresses(self.interface) + up = netifaces.AF_INET in info or netifaces.AF_INET6 in info + fdict = dict(zip_longest(["v4", "v4mask", "v6", "v6mask"], [], fillvalue="")) + fdict.update(self.baseinfo) + + if up: + format = self.format_up + color = self.color_up + if netifaces.AF_INET in info: + v4 = info[netifaces.AF_INET][0] + fdict["v4"] = v4["addr"] + fdict["v4mask"] = v4["netmask"] + if netifaces.AF_INET6 in info: + v6 = info[netifaces.AF_INET6][0] + fdict["v6"] = v6["addr"] + fdict["v6mask"] = v6["netmask"] + else: + format = self.format_down + color = self.color_down + + self.output = { + "full_text": format.format(**fdict), + "color": color, + "instance": self.interface + } diff --git a/setup.py b/setup.py index c57844b..ba48dae 100755 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from setuptools import setup setup(name="i3pystatus", - version="3.3", + version="3.4", description="Like i3status, this generates status line for i3bar / i3wm", url="http://github.com/enkore/i3pystatus", license="MIT",