class MemoryWidget(Widget): delay = 1 def __init__( self, app: QtWidgets.QApplication, main_window: QtWidgets.QWidget ) -> None: super().__init__(app, main_window) self.percentage: T.Optional[float] = None self._container = QtWidgets.QWidget(main_window) self._chart = Chart( self._container, min_width=80, scale_low=0.0, scale_high=100.0 ) layout = QtWidgets.QHBoxLayout(self._container, margin=0, spacing=6) layout.addWidget(self._chart) @property def container(self) -> QtWidgets.QWidget: return self._container def _refresh_impl(self) -> None: self.percentage = psutil.virtual_memory().percent def _render_impl(self) -> None: assert self.percentage is not None self._chart.addPoint(Colors.memory_chart_line, self.percentage) self._chart.setLabel(f"RAM {self.percentage:.1f}%") self._chart.repaint()
class CurrencyWidget(Widget): delay = 60 def __init__( self, app: QtWidgets.QApplication, main_window: QtWidgets.QWidget ) -> None: super().__init__(app, main_window) self.percentage: T.Optional[float] = None self._container = QtWidgets.QWidget(main_window) self._chart = Chart( self._container, min_width=80, scale_low=float("inf"), scale_high=0.0, ) self._quotes: T.Dict[datetime.datetime, float] = {} layout = QtWidgets.QHBoxLayout(self._container, margin=0, spacing=6) layout.addWidget(self._chart) @property def container(self) -> QtWidgets.QWidget: return self._container def _refresh_impl(self) -> None: response = requests.get( "https://www.ingturbo.pl/services/underlying/usd-pln/chart" "?period=intraday" ) response.raise_for_status() for row in response.json()["Quotes"]: time, quote = row date = datetime.datetime.fromtimestamp(time / 1000.0) self._quotes[date] = quote self._chart.clearPoints() for key, value in sorted(self._quotes.items(), key=lambda kv: kv[0]): self._chart.addPoint(Colors.currency_chart_line, value) self._chart.setLabel(f"USD {value}") def _render_impl(self) -> None: self._chart.repaint()
class NetworkUsageWidget(Widget): delay = 1 def __init__( self, app: QtWidgets.QApplication, main_window: QtWidgets.QWidget ) -> None: super().__init__(app, main_window) self.net_in = 0 self.net_out = 0 self._old_rx_bytes = 0 self._old_tx_bytes = 0 available = False try: self._rx_path: T.Optional[Path] = None self._tx_path: T.Optional[Path] = None for path in Path("/sys/class/net/").iterdir(): if "virtual" in str(path.resolve()): continue state = (path / "operstate").read_text().strip() if state.lower() == "up": self._rx_path = path / "statistics" / "rx_bytes" self._tx_path = path / "statistics" / "tx_bytes" available = True except Exception: pass if available: self._old_rx_bytes = int(self._rx_path.read_text().strip()) self._old_tx_bytes = int(self._tx_path.read_text().strip()) self._container = QtWidgets.QWidget(main_window) self._chart = Chart( self._container, min_width=120, scale_low=0.0, scale_high=1024.0 * 1024.0, ) layout = QtWidgets.QHBoxLayout(self._container, margin=0, spacing=6) layout.addWidget(self._chart) @property def container(self) -> QtWidgets.QWidget: return self._container @property def available(self) -> bool: return self._rx_path is not None and self._tx_path is not None def _refresh_impl(self) -> None: rx_bytes = int(self._rx_path.read_text().strip()) tx_bytes = int(self._tx_path.read_text().strip()) self.net_in = rx_bytes - self._old_rx_bytes self.net_out = tx_bytes - self._old_tx_bytes self._old_rx_bytes = rx_bytes self._old_tx_bytes = tx_bytes def _render_impl(self) -> None: self._chart.setLabel( f"DL {convert_speed(self.net_in)} UL {convert_speed(self.net_out)}" ) self._chart.addPoint(Colors.net_up_chart_line, self.net_in) self._chart.addPoint(Colors.net_down_chart_line, self.net_out) self._chart.update()