first complete implementation of the network module
This commit is contained in:
parent
c7df1926dc
commit
911230c659
1 changed files with 20 additions and 14 deletions
|
@ -1,6 +1,8 @@
|
||||||
"""
|
"""
|
||||||
A module to show currently active network connection (ethernet or wifi)
|
A module to show currently active network connection (ethernet or wifi) and connection strength if the connection is wireless.
|
||||||
and connection strength.
|
|
||||||
|
Dependencies: nm-connection-editor if users would like a graphical
|
||||||
|
network manager when left-clicking the module
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,14 +18,17 @@ class Module(core.module.Module):
|
||||||
@core.decorators.every(seconds=10)
|
@core.decorators.every(seconds=10)
|
||||||
def __init__(self, config, theme):
|
def __init__(self, config, theme):
|
||||||
super().__init__(config, theme, core.widget.Widget(self.network))
|
super().__init__(config, theme, core.widget.Widget(self.network))
|
||||||
self.__is_wireless = True
|
self.__is_wireless = False
|
||||||
self.__interface = None
|
self.__interface = None
|
||||||
self.__message = None
|
self.__message = None
|
||||||
self.__signal = -110
|
self.__signal = -110
|
||||||
|
|
||||||
# Set up event handler for left mouse click
|
# Set up event handler for left mouse click
|
||||||
core.input.register(self, button=core.input.LEFT_MOUSE, cmd="nm-connection-editor")
|
core.input.register(
|
||||||
|
self, button=core.input.LEFT_MOUSE, cmd="nm-connection-editor"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Get network information to display to the user
|
||||||
def network(self, widgets):
|
def network(self, widgets):
|
||||||
# run ip route command, tokenize output
|
# run ip route command, tokenize output
|
||||||
cmd = "ip route get 8.8.8.8"
|
cmd = "ip route get 8.8.8.8"
|
||||||
|
@ -33,7 +38,7 @@ class Module(core.module.Module):
|
||||||
|
|
||||||
# Attempt to extract a valid network interface device
|
# Attempt to extract a valid network interface device
|
||||||
try:
|
try:
|
||||||
self.__interface = route_tokens[route_tokens.index("dev") + 1]
|
self.__interface = route_tokens[route_tokens.index("dev") + 1]
|
||||||
except ValueError:
|
except ValueError:
|
||||||
self.__interface = None
|
self.__interface = None
|
||||||
|
|
||||||
|
@ -47,10 +52,13 @@ class Module(core.module.Module):
|
||||||
if self.__interface is None:
|
if self.__interface is None:
|
||||||
self.__message = " No connection"
|
self.__message = " No connection"
|
||||||
elif self.__is_wireless:
|
elif self.__is_wireless:
|
||||||
cmd = "iwgetid"
|
iw_dat = util.cli.execute("iwgetid")
|
||||||
iw_dat = util.cli.execute(cmd)
|
|
||||||
has_ssid = "ESSID" in iw_dat
|
has_ssid = "ESSID" in iw_dat
|
||||||
ssid = iw_dat[iw_dat.index(":") + 2: -2] if has_ssid else "Unknown"
|
ssid = (
|
||||||
|
iw_dat[iw_dat.index(":") + 1 :].replace('"', "").strip()
|
||||||
|
if has_ssid
|
||||||
|
else "Unknown"
|
||||||
|
)
|
||||||
|
|
||||||
# Get connection strength
|
# Get connection strength
|
||||||
cmd = "iwconfig {}".format(self.__interface)
|
cmd = "iwconfig {}".format(self.__interface)
|
||||||
|
@ -61,12 +69,13 @@ class Module(core.module.Module):
|
||||||
|
|
||||||
self.__message = self.__generate_wireles_message(ssid, self.__signal)
|
self.__message = self.__generate_wireles_message(ssid, self.__signal)
|
||||||
else:
|
else:
|
||||||
|
# Set signal to -30 as ethernet shouldn't have signal issues
|
||||||
self.__signal = -30
|
self.__signal = -30
|
||||||
self.__message = " Ethernet"
|
self.__message = " Ethernet"
|
||||||
|
|
||||||
return self.__message
|
return self.__message
|
||||||
|
|
||||||
|
# The signal is measured in decibels/milliwatt, hence the weird numbers
|
||||||
def state(self, widget):
|
def state(self, widget):
|
||||||
if self.__signal < -80:
|
if self.__signal < -80:
|
||||||
return "critical"
|
return "critical"
|
||||||
|
@ -75,10 +84,7 @@ class Module(core.module.Module):
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
# manually done for better granularity / ease of parsing strength data
|
||||||
def __generate_wireles_message(self, ssid, strength):
|
def __generate_wireles_message(self, ssid, strength):
|
||||||
computed_strength = 100 * ((strength + 100) / 70.0)
|
computed_strength = 100 * ((strength + 100) / 70.0)
|
||||||
return " {} {}%".format(ssid, int(computed_strength))
|
return " {} {}%".format(ssid, int(computed_strength))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue