Don't require importing psutil unless using the functionality it offers
This commit is contained in:
parent
2999b90b22
commit
d3e8fe9b6a
@ -1,6 +1,5 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import netifaces
|
import netifaces
|
||||||
import psutil
|
|
||||||
from i3pystatus import IntervalModule
|
from i3pystatus import IntervalModule
|
||||||
from i3pystatus.core.color import ColorRangeModule
|
from i3pystatus.core.color import ColorRangeModule
|
||||||
from i3pystatus.core.util import make_graph, round_dict, make_bar
|
from i3pystatus.core.util import make_graph, round_dict, make_bar
|
||||||
@ -135,6 +134,8 @@ class NetworkInfo():
|
|||||||
|
|
||||||
def extract_wireless_info(self, interface):
|
def extract_wireless_info(self, interface):
|
||||||
info = dict(essid="", freq="", quality=0.0, quality_bar="")
|
info = dict(essid="", freq="", quality=0.0, quality_bar="")
|
||||||
|
|
||||||
|
# Just return empty values if we're not using any Wifi functionality
|
||||||
if not self.get_wifi_info:
|
if not self.get_wifi_info:
|
||||||
return info
|
return info
|
||||||
|
|
||||||
@ -173,6 +174,8 @@ class NetworkTraffic():
|
|||||||
self.round_size = round_size
|
self.round_size = round_size
|
||||||
|
|
||||||
def update_counters(self, interface):
|
def update_counters(self, interface):
|
||||||
|
import psutil
|
||||||
|
|
||||||
self.pnic_before = self.pnic
|
self.pnic_before = self.pnic
|
||||||
counters = psutil.net_io_counters(pernic=True)
|
counters = psutil.net_io_counters(pernic=True)
|
||||||
self.pnic = counters[interface] if interface in counters else None
|
self.pnic = counters[interface] if interface in counters else None
|
||||||
@ -289,14 +292,21 @@ class Network(IntervalModule, ColorRangeModule):
|
|||||||
|
|
||||||
def init(self):
|
def init(self):
|
||||||
# Don't require importing basiciw unless using the functionality it offers.
|
# Don't require importing basiciw unless using the functionality it offers.
|
||||||
if any(s in self.format_up or s in self.format_up for s in ['essid', 'freq', 'quality', 'quality_bar']):
|
if any(s in self.format_up or s in self.format_up for s in
|
||||||
|
['essid', 'freq', 'quality', 'quality_bar']):
|
||||||
get_wifi_info = True
|
get_wifi_info = True
|
||||||
else:
|
else:
|
||||||
get_wifi_info = False
|
get_wifi_info = False
|
||||||
|
|
||||||
self.network_info = NetworkInfo(self.interface, self.ignore_interfaces, self.detached_down, self.unknown_up,
|
self.network_info = NetworkInfo(self.interface, self.ignore_interfaces, self.detached_down, self.unknown_up,
|
||||||
get_wifi_info)
|
get_wifi_info)
|
||||||
|
|
||||||
|
# Don't require importing psutil unless using the functionality it offers.
|
||||||
|
if any(s in self.format_up or s in self.format_down for s in
|
||||||
|
['bytes_sent', 'bytes_recv', 'packets_sent', 'packets_recv', 'network_graph']):
|
||||||
self.network_traffic = NetworkTraffic(self.unknown_up, self.divisor, self.round_size)
|
self.network_traffic = NetworkTraffic(self.unknown_up, self.divisor, self.round_size)
|
||||||
|
else:
|
||||||
|
self.network_traffic = None
|
||||||
|
|
||||||
self.colors = self.get_hex_color_range(self.start_color, self.end_color, int(self.upper_limit))
|
self.colors = self.get_hex_color_range(self.start_color, self.end_color, int(self.upper_limit))
|
||||||
self.kbs_arr = [0.0] * self.graph_width
|
self.kbs_arr = [0.0] * self.graph_width
|
||||||
@ -309,6 +319,7 @@ class Network(IntervalModule, ColorRangeModule):
|
|||||||
elif len(interfaces) > 0:
|
elif len(interfaces) > 0:
|
||||||
self.interface = interfaces[0]
|
self.interface = interfaces[0]
|
||||||
|
|
||||||
|
if self.network_traffic:
|
||||||
self.network_traffic.clear_counters()
|
self.network_traffic.clear_counters()
|
||||||
self.kbs_arr = [0.0] * self.graph_width
|
self.kbs_arr = [0.0] * self.graph_width
|
||||||
|
|
||||||
@ -322,13 +333,10 @@ class Network(IntervalModule, ColorRangeModule):
|
|||||||
format_values = dict(kbs="", network_graph="", bytes_sent="", bytes_recv="", packets_sent="", packets_recv="",
|
format_values = dict(kbs="", network_graph="", bytes_sent="", bytes_recv="", packets_sent="", packets_recv="",
|
||||||
interface="", v4="", v4mask="", v4cidr="", v6="", v6mask="", v6cidr="", mac="",
|
interface="", v4="", v4mask="", v4cidr="", v6="", v6mask="", v6cidr="", mac="",
|
||||||
essid="", freq="", quality="", quality_bar="")
|
essid="", freq="", quality="", quality_bar="")
|
||||||
|
color = None
|
||||||
|
if self.network_traffic:
|
||||||
network_usage = self.network_traffic.get_usage(self.interface)
|
network_usage = self.network_traffic.get_usage(self.interface)
|
||||||
format_values.update(network_usage)
|
format_values.update(network_usage)
|
||||||
|
|
||||||
network_info = self.network_info.get_info(self.interface)
|
|
||||||
format_values.update(network_info)
|
|
||||||
|
|
||||||
if self.graph_type == 'input':
|
if self.graph_type == 'input':
|
||||||
kbs = network_usage['bytes_recv']
|
kbs = network_usage['bytes_recv']
|
||||||
elif self.graph_type == 'output':
|
elif self.graph_type == 'output':
|
||||||
@ -338,12 +346,14 @@ class Network(IntervalModule, ColorRangeModule):
|
|||||||
|
|
||||||
format_values['network_graph'] = self.get_network_graph(kbs)
|
format_values['network_graph'] = self.get_network_graph(kbs)
|
||||||
format_values['kbs'] = "{0:.1f}".format(round(kbs, 2)).rjust(6)
|
format_values['kbs'] = "{0:.1f}".format(round(kbs, 2)).rjust(6)
|
||||||
format_values['interface'] = self.interface
|
|
||||||
|
|
||||||
if sysfs_interface_up(self.interface, self.unknown_up):
|
|
||||||
if self.dynamic_color:
|
|
||||||
color = self.get_gradient(kbs, self.colors, self.upper_limit)
|
color = self.get_gradient(kbs, self.colors, self.upper_limit)
|
||||||
else:
|
|
||||||
|
network_info = self.network_info.get_info(self.interface)
|
||||||
|
format_values.update(network_info)
|
||||||
|
|
||||||
|
format_values['interface'] = self.interface
|
||||||
|
if sysfs_interface_up(self.interface, self.unknown_up):
|
||||||
|
if not self.dynamic_color:
|
||||||
color = self.color_up
|
color = self.color_up
|
||||||
|
|
||||||
self.output = {
|
self.output = {
|
||||||
|
Loading…
Reference in New Issue
Block a user