diff options
author | Nico Schlömer <nico.schloemer@gmail.com> | 2021-12-09 02:07:10 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-09 02:07:10 +0100 |
commit | 5069b39d9f402da0a6abd15a826817457b472915 (patch) | |
tree | d92d0a865b151c852288e68d518f629d4077a70e | |
parent | 3a71c35ffd6994b6d56fd6c311172faeb3cb0d5c (diff) | |
parent | 38fa52f0c32034b3978e3be97a2c0eaf0d5d539e (diff) |
Merge pull request #44 from nschloe/specify-net-devicev0.0.15
Specify net interface
-rw-r--r-- | .github/workflows/ci.yml | 6 | ||||
-rw-r--r-- | README.md | 27 | ||||
-rw-r--r-- | setup.cfg | 2 | ||||
-rw-r--r-- | src/tiptop/_app.py | 84 | ||||
-rw-r--r-- | src/tiptop/_net.py | 42 | ||||
-rw-r--r-- | tox.ini | 4 |
6 files changed, 123 insertions, 42 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 04effac..e81631c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,7 +23,9 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ["3.7", "3.8", "3.9", "3.10"] + # Can't check on Python 3.10 yet as it has a different output on tiptop -h + # https://docs.python.org/3/whatsnew/3.10.html#argparse + python-version: ["3.7", "3.8", "3.9"] steps: - uses: actions/setup-python@v2 with: @@ -32,7 +34,7 @@ jobs: - name: Test with tox run: | pip install tox - tox -- --cov pyfoobar --cov-report xml --cov-report term + tox - name: Submit to codecov uses: codecov/codecov-action@v1 if: ${{ matrix.python-version == '3.9' }} @@ -7,6 +7,7 @@ [![PyPI pyversions](https://img.shields.io/pypi/pyversions/tiptop.svg?style=flat-square)](https://pypi.org/project/tiptop/) [![GitHub stars](https://img.shields.io/github/stars/nschloe/tiptop.svg?style=flat-square&logo=github&label=Stars&logoColor=white)](https://github.com/nschloe/tiptop) [![Downloads](https://pepy.tech/badge/tiptop/month?style=flat-square)](https://pepy.tech/project/tiptop) + <!--[![PyPi downloads](https://img.shields.io/pypi/dm/tiptop.svg?style=flat-square)](https://pypistats.org/packages/tiptop)--> [![Discord](https://img.shields.io/static/v1?logo=discord&label=chat&message=on%20discord&color=7289da&style=flat-square)](https://discord.gg/Z6DMsJh4Hr) @@ -19,11 +20,14 @@ [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg?style=flat-square)](https://github.com/psf/black) tiptop is a command-line system monitoring tool in the spirit of -[top](https://en.wikipedia.org/wiki/Top_(software)). It displays various +[top](<https://en.wikipedia.org/wiki/Top_(software)>). It displays various interesting system stats, graphs it, and works on Linux and macOS. Install and run with -``` + +<!--pytest-codeblocks: skip--> + +```sh pip install tiptop tiptop ``` @@ -32,6 +36,25 @@ tiptop <img alt="screenshot" src="https://raw.githubusercontent.com/nschloe/tiptop/gh-pages/screenshot.png" width="100%"/> </p> +For all options, see + +```sh +tiptop -h +``` + +<!--pytest-codeblocks: expected-output--> + +``` +usage: tiptop [-h] [--version] [--net NET] + +Command-line system monitor. + +optional arguments: + -h, --help show this help message and exit + --version, -v display version information + --net NET, -n NET network interface to display (default: auto) +``` + tiptop uses [Textual](https://github.com/willmcgugan/textual/) for layouting and [psutil](https://github.com/giampaolo/psutil) for fetching system data. Other top alternatives in alphabetical order: @@ -1,6 +1,6 @@ [metadata] name = tiptop -version = 0.0.14 +version = 0.0.15 author = Nico Schlömer author_email = nico.schloemer@gmail.com description = A better top diff --git a/src/tiptop/_app.py b/src/tiptop/_app.py index de5c25f..bb4722c 100644 --- a/src/tiptop/_app.py +++ b/src/tiptop/_app.py @@ -1,3 +1,14 @@ +from __future__ import annotations + +import argparse +from sys import version_info + +try: + # Python 3.8+ + from importlib import metadata +except ImportError: + import importlib_metadata as metadata + from textual.app import App from ._cpu import CPU @@ -6,23 +17,46 @@ from ._mem import Mem from ._net import Net from ._procs_list import ProcsList +# class TiptopApp(App): +# async def on_mount(self) -> None: +# await self.view.dock(InfoLine(), edge="top", size=1, name="info") +# await self.view.dock(CPU(), edge="top", size=14, name="cpu") +# await self.view.dock(ProcsList(), edge="right", size=70, name="proc") +# await self.view.dock(Mem(), edge="top", size=20, name="mem") +# await self.view.dock(Net(), edge="bottom", name="net") +# +# async def on_load(self, _): +# await self.bind("i", "view.toggle('info')", "Toggle info") +# await self.bind("c", "view.toggle('cpu')", "Toggle cpu") +# await self.bind("m", "view.toggle('mem')", "Toggle mem") +# await self.bind("n", "view.toggle('net')", "Toggle net") +# await self.bind("p", "view.toggle('proc')", "Toggle proc") +# await self.bind("q", "quit", "quit") + + +def run(argv=None): + parser = argparse.ArgumentParser( + description="Command-line system monitor.", + formatter_class=argparse.RawTextHelpFormatter, + ) + + parser.add_argument( + "--version", + "-v", + action="version", + version=_get_version_text(), + help="display version information", + ) -def run(): - # class TiptopApp(App): - # async def on_mount(self) -> None: - # await self.view.dock(InfoLine(), edge="top", size=1, name="info") - # await self.view.dock(CPU(), edge="top", size=14, name="cpu") - # await self.view.dock(ProcsList(), edge="right", size=70, name="proc") - # await self.view.dock(Mem(), edge="top", size=20, name="mem") - # await self.view.dock(Net(), edge="bottom", name="net") - # - # async def on_load(self, _): - # await self.bind("i", "view.toggle('info')", "Toggle info") - # await self.bind("c", "view.toggle('cpu')", "Toggle cpu") - # await self.bind("m", "view.toggle('mem')", "Toggle mem") - # await self.bind("n", "view.toggle('net')", "Toggle net") - # await self.bind("p", "view.toggle('proc')", "Toggle proc") - # await self.bind("q", "quit", "quit") + parser.add_argument( + "--net", + "-n", + type=str, + default=None, + help="network interface to display (default: auto)", + ) + + args = parser.parse_args(argv) # with a grid class TiptopApp(App): @@ -52,7 +86,7 @@ def run(): area0=InfoLine(), area1=CPU(), area2=Mem(), - area3=Net(), + area3=Net(args.net), area4=ProcsList(), ) @@ -60,3 +94,19 @@ def run(): await self.bind("q", "quit", "quit") TiptopApp.run(log="textual.log") + + +def _get_version_text(): + python_version = f"{version_info.major}.{version_info.minor}.{version_info.micro}" + + try: + __version__ = metadata.version("tiptop") + except metadata.PackageNotFoundError: + __version__ = "" + + return "\n".join( + [ + f"tiptop {__version__} [Python {python_version}]", + "Copyright (c) 2021 Nico Schlömer", + ] + ) diff --git a/src/tiptop/_net.py b/src/tiptop/_net.py index 6eb30ae..93b7755 100644 --- a/src/tiptop/_net.py +++ b/src/tiptop/_net.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import socket import psutil @@ -12,25 +14,31 @@ from .braille_stream import BrailleStream class Net(Widget): - def on_mount(self): - # try to find non-lo and non-docker interface that is up - stats = psutil.net_if_stats() - score_dict = {} - for name, stats in stats.items(): - if not stats.isup: - score_dict[name] = 0 - continue - - if name.startswith("lo") or name.startswith("docker"): - score_dict[name] = 1 - continue - - score_dict[name] = 2 + def __init__(self, interface: str | None = None): + if interface is None: + # try to find non-lo and non-docker interface that is up + stats = psutil.net_if_stats() + score_dict = {} + for name, stats in stats.items(): + if not stats.isup: + score_dict[name] = 0 + continue + + if name.startswith("lo") or name.startswith("docker"): + score_dict[name] = 1 + continue + + score_dict[name] = 2 + + # Get key with max score + # https://stackoverflow.com/a/280156/353337 + self.interface = max(score_dict, key=score_dict.get) + else: + self.interface = interface - # Get key with max score - # https://stackoverflow.com/a/280156/353337 - self.interface = max(score_dict, key=score_dict.get) + super().__init__() + def on_mount(self): self.last_net = None self.max_recv_bytes_s = 0 self.max_recv_bytes_s_str = "" @@ -6,7 +6,5 @@ isolated_build = True deps = pytest pytest-codeblocks - pytest-cov -extras = all commands = - pytest {posargs} + pytest {posargs} --codeblocks |