-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from FrnkPsycho/traffic_widget
feat: add network traffic widget and some fixes
- Loading branch information
Showing
7 changed files
with
185 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
DEFAULTS = { | ||
"label": "\ueb01 \ueab4 {download_speed} | \ueab7 {upload_speed}", | ||
"label_alt": "\ueb01 \ueab4 {upload_speed} | \ueab7 {download_speed}", | ||
"update_interval": 1000, | ||
"callbacks": { | ||
"on_left": "toggle_label", | ||
"on_middle": "do_nothing", | ||
"on_right": "do_nothing", | ||
}, | ||
} | ||
|
||
VALIDATION_SCHEMA = { | ||
"label": {"type": "string", "default": DEFAULTS["label"]}, | ||
"label_alt": {"type": "string", "default": DEFAULTS["label_alt"]}, | ||
"update_interval": { | ||
"type": "integer", | ||
"default": DEFAULTS["update_interval"], | ||
"min": 0, | ||
"max": 60000, | ||
}, | ||
"callbacks": { | ||
"type": "dict", | ||
"schema": { | ||
"on_left": { | ||
"type": "string", | ||
"default": DEFAULTS["callbacks"]["on_left"], | ||
}, | ||
"on_middle": { | ||
"type": "string", | ||
"default": DEFAULTS["callbacks"]["on_middle"], | ||
}, | ||
"on_right": { | ||
"type": "string", | ||
"default": DEFAULTS["callbacks"]["on_right"], | ||
}, | ||
}, | ||
"default": DEFAULTS["callbacks"], | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import psutil | ||
from humanize import naturalsize | ||
from core.widgets.base import BaseWidget | ||
from core.validation.widgets.yasb.traffic import VALIDATION_SCHEMA | ||
from PyQt6.QtWidgets import QLabel | ||
|
||
|
||
class TrafficWidget(BaseWidget): | ||
validation_schema = VALIDATION_SCHEMA | ||
|
||
# initialize io counters | ||
io = psutil.net_io_counters() | ||
bytes_sent = io.bytes_sent | ||
bytes_recv = io.bytes_recv | ||
interval = 1 # second(s) | ||
|
||
def __init__( | ||
self, | ||
label: str, | ||
label_alt: str, | ||
update_interval: int, | ||
callbacks: dict[str, str], | ||
): | ||
super().__init__(update_interval, class_name="traffic-widget") | ||
self.interval = update_interval // 1000 | ||
|
||
self._show_alt_label = False | ||
self._label_content = label | ||
self._label_alt_content = label_alt | ||
|
||
self._label = QLabel() | ||
self._label_alt = QLabel() | ||
self._label.setProperty("class", "label") | ||
self._label_alt.setProperty("class", "label alt") | ||
self.widget_layout.addWidget(self._label) | ||
self.widget_layout.addWidget(self._label_alt) | ||
|
||
self.register_callback("toggle_label", self._toggle_label) | ||
self.register_callback("update_label", self._update_label) | ||
|
||
self.callback_left = callbacks["on_left"] | ||
self.callback_right = callbacks["on_right"] | ||
self.callback_middle = callbacks["on_middle"] | ||
self.callback_timer = "update_label" | ||
|
||
self._label.show() | ||
self._label_alt.hide() | ||
|
||
self.start_timer() | ||
|
||
def _toggle_label(self): | ||
self._show_alt_label = not self._show_alt_label | ||
|
||
if self._show_alt_label: | ||
self._label.hide() | ||
self._label_alt.show() | ||
else: | ||
self._label.show() | ||
self._label_alt.hide() | ||
|
||
self._update_label() | ||
|
||
def _update_label(self): | ||
# Update the active label at each timer interval | ||
active_label = self._label_alt if self._show_alt_label else self._label | ||
active_label_content = self._label_alt_content if self._show_alt_label else self._label_content | ||
active_label_formatted = active_label_content | ||
|
||
try: | ||
upload_speed, download_speed = self._get_speed() | ||
except Exception: | ||
upload_speed, download_speed = "N/A", "N/A" | ||
|
||
label_options = [ | ||
("{upload_speed}", upload_speed), | ||
("{download_speed}", download_speed), | ||
] | ||
|
||
for option, value in label_options: | ||
active_label_formatted = active_label_formatted.replace(option, str(value)) | ||
|
||
active_label.setText(active_label_formatted) | ||
|
||
def _get_speed(self) -> [str, str]: | ||
current_io = psutil.net_io_counters() | ||
upload_diff = current_io.bytes_sent - self.bytes_sent | ||
download_diff = current_io.bytes_recv - self.bytes_recv | ||
|
||
if upload_diff < 1024: | ||
upload_speed = f"{upload_diff} B/s" | ||
else: | ||
upload_speed = naturalsize( | ||
(current_io.bytes_sent - self.bytes_sent) // self.interval, | ||
) + "/s" | ||
|
||
if download_diff < 1024: | ||
download_speed = f"{download_diff} B/s" | ||
else: | ||
download_speed = naturalsize( | ||
(current_io.bytes_recv - self.bytes_recv) // self.interval, | ||
) + "/s" | ||
|
||
self.bytes_sent = current_io.bytes_sent | ||
self.bytes_recv = current_io.bytes_recv | ||
return upload_speed, download_speed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters