Skip to content

Commit

Permalink
feat(widget/traffic): add interface option and logging for network in…
Browse files Browse the repository at this point in the history
…terface selection

- Introduced an 'interface' parameter to allow users to specify the network interface.
- Added logging to provide debug information when the interface is set to "Auto".
- Updated validation schema to include the new 'interface' option.
  • Loading branch information
amnweb committed Jan 17, 2025
1 parent 60072f2 commit 4345f0b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/core/validation/widgets/yasb/traffic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
DEFAULTS = {
"label": "\ueb01 \ueab4 {download_speed} | \ueab7 {upload_speed}",
"label_alt": "\ueb01 \ueab4 {upload_speed} | \ueab7 {download_speed}",
"interface": "Auto",
"update_interval": 1000,
"animation": {
'enabled': True,
Expand All @@ -18,6 +19,11 @@
VALIDATION_SCHEMA = {
"label": {"type": "string", "default": DEFAULTS["label"]},
"label_alt": {"type": "string", "default": DEFAULTS["label_alt"]},
"interface": {
"type": "string",
"required": False,
"default": DEFAULTS["interface"]
},
"update_interval": {
"type": "integer",
"default": DEFAULTS["update_interval"],
Expand Down
22 changes: 18 additions & 4 deletions src/core/widgets/yasb/traffic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import re
import psutil
import logging
from settings import DEBUG
from humanize import naturalsize
from core.widgets.base import BaseWidget
from core.validation.widgets.yasb.traffic import VALIDATION_SCHEMA
Expand All @@ -19,6 +21,7 @@ def __init__(
self,
label: str,
label_alt: str,
interface: str,
update_interval: int,
animation: dict[str, str],
container_padding: dict[str, int],
Expand All @@ -32,7 +35,7 @@ def __init__(
self._label_alt_content = label_alt
self._animation = animation
self._padding = container_padding

self._interface = interface
# Construct container
self._widget_container_layout: QHBoxLayout = QHBoxLayout()
self._widget_container_layout.setSpacing(0)
Expand All @@ -53,7 +56,11 @@ def __init__(
self.callback_right = callbacks["on_right"]
self.callback_middle = callbacks["on_middle"]
self.callback_timer = "update_label"

if DEBUG:
if self._interface == "Auto":
logging.debug("Network Interface: Auto")
else:
logging.debug(f"Network Interface: {self._interface}")
self.start_timer()

def _toggle_label(self):
Expand Down Expand Up @@ -123,10 +130,17 @@ def _update_label(self):
else:
active_widgets[widget_index].setText(part)
widget_index += 1


def _get_speed(self) -> list[str]:
current_io = psutil.net_io_counters()
if self._interface == "Auto":
current_io = psutil.net_io_counters()
else:
io_counters = psutil.net_io_counters(pernic=True)
if self._interface not in io_counters:
return "N/A", "N/A"
current_io = io_counters[self._interface]

upload_diff = current_io.bytes_sent - self.bytes_sent
download_diff = current_io.bytes_recv - self.bytes_recv

Expand Down

0 comments on commit 4345f0b

Please sign in to comment.