Esempio n. 1
0
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()
Esempio n. 2
0
class CpuWidget(Widget):
    delay = 0

    def __init__(self, app, main_window):
        super().__init__(app, main_window)
        self.percentage = None
        self._icon_label = QtWidgets.QLabel()
        self._text_label = QtWidgets.QLabel()
        self._chart = Chart(QtCore.QSize(80, main_window.height()))

        self.set_icon(self._icon_label, 'chip')

        container = QtWidgets.QWidget()
        container.setLayout(QtWidgets.QHBoxLayout(margin=0, spacing=6))
        container.layout().addWidget(self._icon_label)
        container.layout().addWidget(self._text_label)
        container.layout().addWidget(self._chart)
        main_window[0].layout().addWidget(container)

    def refresh_impl(self):
        if self.percentage is None:
            self.percentage = psutil.cpu_percent(interval=0.1)
        else:
            self.percentage = psutil.cpu_percent(interval=1)

    def render_impl(self):
        self._text_label.setText('%05.1f%%' % (self.percentage))
        self._chart.addPoint('#f00', self.percentage)
        self._chart.repaint()
Esempio n. 3
0
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()
Esempio n. 4
0
class NetworkUsageWidget(Widget):
    delay = 1

    def __init__(self, app, main_window):
        super().__init__(app, main_window)
        self.net_in = 0
        self.net_out = 0
        self.network_enabled = False
        try:
            self._rx_path = None
            self._tx_path = None
            for interface in glob.glob('/sys/class/net/*'):
                state = read_file(os.path.join(interface, 'operstate'))
                if state.lower() == 'up':
                    self._rx_path = os.path.join(
                        interface, 'statistics', 'rx_bytes')
                    self._tx_path = os.path.join(
                        interface, 'statistics', 'tx_bytes')
                    self.network_enabled = True
        except:
            pass

        if not self.network_enabled:
            return

        self._old_rx_bytes = int(read_file(self._rx_path))
        self._old_tx_bytes = int(read_file(self._tx_path))
        self._net_in_icon_label = QtWidgets.QLabel()
        self._net_in_text_label = QtWidgets.QLabel()
        self._net_out_icon_label = QtWidgets.QLabel()
        self._net_out_text_label = QtWidgets.QLabel()
        self._chart = Chart(QtCore.QSize(80, main_window.height()))

        self.set_icon(self._net_in_icon_label, 'arrow-down')
        self.set_icon(self._net_out_icon_label, 'arrow-up')

        container = QtWidgets.QWidget()
        container.setLayout(QtWidgets.QHBoxLayout(margin=0, spacing=6))
        container.layout().addWidget(self._net_in_icon_label)
        container.layout().addWidget(self._net_in_text_label)
        container.layout().addWidget(self._net_out_icon_label)
        container.layout().addWidget(self._net_out_text_label)
        container.layout().addWidget(self._chart)
        main_window[0].layout().addWidget(container)
        self._chart.repaint()

    def refresh_impl(self):
        if self.network_enabled:
            rx_bytes = int(read_file(self._rx_path))
            tx_bytes = int(read_file(self._tx_path))
            self.net_in = (rx_bytes - self._old_rx_bytes) / 1
            self.net_out = (tx_bytes - self._old_tx_bytes) / 1
            self._old_rx_bytes = rx_bytes
            self._old_tx_bytes = tx_bytes

    def render_impl(self):
        if self.network_enabled:
            self._net_in_text_label.setText(
                '%04.0f KB/s' % (self.net_in / 1024.0))
            self._net_out_text_label.setText(
                '%04.0f KB/s' % (self.net_out / 1024.0))
            self._chart.addPoint('#0b0', self.net_in)
            self._chart.addPoint('#f00', self.net_out)
            self._chart.repaint()
Esempio n. 5
0
File: net.py Progetto: rr-/dotfiles
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()