Python source code formatting and optimisation

pull/408/head
Axylum 2023-11-18 23:03:38 +01:00
parent ac0f6660be
commit 91466587b7
4 changed files with 1471 additions and 1064 deletions

2457
bpytop.py

File diff suppressed because it is too large Load Diff

View File

@ -1,41 +1,60 @@
import bpytop, pytest import bpytop
import pytest
from bpytop import Box, SubBox, CpuBox, MemBox, NetBox, ProcBox, Term, Draw from bpytop import Box, SubBox, CpuBox, MemBox, NetBox, ProcBox, Term, Draw
from bpytop import Graph, Fx, Meter, Color, Banner
from bpytop import Collector, CpuCollector, MemCollector, NetCollector, ProcCollector from bpytop import Collector, CpuCollector, MemCollector, NetCollector, ProcCollector
from bpytop import Graph, Fx, Meter, Color, Banner
bpytop.Term.width, bpytop.Term.height = 80, 25 bpytop.Term.width, bpytop.Term.height = 80, 25
def test_Fx_uncolor(): def test_Fx_uncolor():
assert Fx.uncolor("\x1b[38;2;102;238;142mTEST\x1b[48;2;0;0;0m") == "TEST" assert Fx.uncolor("\x1b[38;2;102;238;142mTEST\x1b[48;2;0;0;0m") == "TEST"
def test_Color(): def test_Color():
assert Color.fg("#00ff00") == "\x1b[38;2;0;255;0m" assert Color.fg("#00ff00") == "\x1b[38;2;0;255;0m"
assert Color.bg("#cc00cc") == "\x1b[48;2;204;0;204m" assert Color.bg("#cc00cc") == "\x1b[48;2;204;0;204m"
assert Color.fg(255, 255, 255) == "\x1b[38;2;255;255;255m" assert Color.fg(255, 255, 255) == "\x1b[38;2;255;255;255m"
def test_Theme(): def test_Theme():
bpytop.THEME = bpytop.Theme("Default") bpytop.THEME = bpytop.Theme("Default")
assert str(bpytop.THEME.main_fg) == "\x1b[38;2;204;204;204m" assert str(bpytop.THEME.main_fg) == "\x1b[38;2;204;204;204m"
assert list(bpytop.THEME.main_fg) == [204, 204, 204] assert list(bpytop.THEME.main_fg) == [204, 204, 204]
assert len(bpytop.THEME.gradient["cpu"]) == 101 assert len(bpytop.THEME.gradient["cpu"]) == 101
def test_Box_calc_sizes(): def test_Box_calc_sizes():
Box.calc_sizes() Box.calc_sizes()
assert CpuBox.width == MemBox.width + ProcBox.width == NetBox.width + ProcBox.width == 80 assert CpuBox.width == MemBox.width + ProcBox.width == NetBox.width + ProcBox.width == 80
assert CpuBox.height + ProcBox.height == CpuBox.height + MemBox.height + NetBox.height == 25 assert CpuBox.height + ProcBox.height == CpuBox.height + MemBox.height + NetBox.height == 25
def test_Graph(): def test_Graph():
test_graph = Graph(width=20, height=10, color=None, data=[x for x in range(20)], invert=False, max_value=0, offset=0, color_max_value=None) test_graph = Graph(
width=20,
height=10,
color=None,
data=list(range(20)),
invert=False,
max_value=0,
offset=0,
color_max_value=None,
)
assert len(str(test_graph)) > 1 assert len(str(test_graph)) > 1
assert str(test_graph).endswith("⣀⣤⣴⣾⣿⣿⣿⣿⣿") assert str(test_graph).endswith("⣀⣤⣴⣾⣿⣿⣿⣿⣿")
assert test_graph(5).endswith("") assert test_graph(5).endswith("")
def test_Meter(): def test_Meter():
test_meter = Meter(value=100, width=20, gradient_name="cpu", invert=False) test_meter = Meter(value=100, width=20, gradient_name="cpu", invert=False)
assert Fx.uncolor(str(test_meter)) == "■■■■■■■■■■■■■■■■■■■■" assert Fx.uncolor(str(test_meter)) == "■■■■■■■■■■■■■■■■■■■■"
def test_Banner(): def test_Banner():
assert len(Banner.draw(line=1, col=1, center=False, now=False)) == 2477 assert len(Banner.draw(line=1, col=1, center=False, now=False)) == 2477
def test_CpuCollector_collect(): def test_CpuCollector_collect():
bpytop.CONFIG.check_temp = False bpytop.CONFIG.check_temp = False
CpuCollector._collect() CpuCollector._collect()
@ -44,6 +63,7 @@ def test_CpuCollector_collect():
assert isinstance(CpuCollector.load_avg, list) assert isinstance(CpuCollector.load_avg, list)
assert isinstance(CpuCollector.uptime, str) assert isinstance(CpuCollector.uptime, str)
def test_CpuCollector_get_sensors(): def test_CpuCollector_get_sensors():
bpytop.CONFIG.check_temp = True bpytop.CONFIG.check_temp = True
bpytop.CONFIG.cpu_sensor = "Auto" bpytop.CONFIG.cpu_sensor = "Auto"
@ -53,6 +73,7 @@ def test_CpuCollector_get_sensors():
else: else:
assert CpuCollector.sensor_method == "" assert CpuCollector.sensor_method == ""
def test_CpuCollector_collect_temps(): def test_CpuCollector_collect_temps():
if not CpuCollector.got_sensors: if not CpuCollector.got_sensors:
pytest.skip("Not testing temperature collection if no sensors was detected!") pytest.skip("Not testing temperature collection if no sensors was detected!")
@ -64,6 +85,7 @@ def test_CpuCollector_collect_temps():
assert isinstance(CpuCollector.cpu_temp_high, int) assert isinstance(CpuCollector.cpu_temp_high, int)
assert isinstance(CpuCollector.cpu_temp_crit, int) assert isinstance(CpuCollector.cpu_temp_crit, int)
def test_MemCollector_collect(): def test_MemCollector_collect():
MemBox.width = 20 MemBox.width = 20
bpytop.CONFIG.show_swap = True bpytop.CONFIG.show_swap = True
@ -80,12 +102,14 @@ def test_MemCollector_collect():
else: else:
assert len(MemCollector.disks) > 0 assert len(MemCollector.disks) > 0
def test_NetCollector_get_nics(): def test_NetCollector_get_nics():
NetCollector._get_nics() NetCollector._get_nics()
if NetCollector.nic == "": if NetCollector.nic == "":
pytest.skip("No nic found, skipping tests!") pytest.skip("No nic found, skipping tests!")
assert NetCollector.nic in NetCollector.nics assert NetCollector.nic in NetCollector.nics
def test_NetCollector_collect(): def test_NetCollector_collect():
if NetCollector.nic == "": if NetCollector.nic == "":
pytest.skip("No nic found, skipping tests!") pytest.skip("No nic found, skipping tests!")
@ -94,6 +118,7 @@ def test_NetCollector_collect():
assert isinstance(NetCollector.strings[NetCollector.nic]["download"]["total"], str) assert isinstance(NetCollector.strings[NetCollector.nic]["download"]["total"], str)
assert isinstance(NetCollector.stats[NetCollector.nic]["upload"]["total"], int) assert isinstance(NetCollector.stats[NetCollector.nic]["upload"]["total"], int)
def test_ProcCollector_collect(): def test_ProcCollector_collect():
bpytop.CONFIG.proc_tree = False bpytop.CONFIG.proc_tree = False
bpytop.CONFIG.proc_mem_bytes = True bpytop.CONFIG.proc_mem_bytes = True
@ -103,7 +128,8 @@ def test_ProcCollector_collect():
bpytop.CONFIG.proc_tree = True bpytop.CONFIG.proc_tree = True
ProcCollector.processes = {} ProcCollector.processes = {}
ProcCollector._collect() ProcCollector._collect()
assert len(ProcCollector.processes) > 0 assert ProcCollector.processes
def test_CpuBox_draw(): def test_CpuBox_draw():
Box.calc_sizes() Box.calc_sizes()
@ -111,6 +137,7 @@ def test_CpuBox_draw():
CpuBox._draw_fg() CpuBox._draw_fg()
assert "cpu" in Draw.strings assert "cpu" in Draw.strings
def test_MemBox_draw(): def test_MemBox_draw():
bpytop.CONFIG.show_disks = True bpytop.CONFIG.show_disks = True
Box.calc_sizes() Box.calc_sizes()
@ -118,12 +145,14 @@ def test_MemBox_draw():
MemBox._draw_fg() MemBox._draw_fg()
assert "mem" in Draw.strings assert "mem" in Draw.strings
def test_NetBox_draw(): def test_NetBox_draw():
Box.calc_sizes() Box.calc_sizes()
assert len(NetBox._draw_bg()) > 1 assert len(NetBox._draw_bg()) > 1
NetBox._draw_fg() NetBox._draw_fg()
assert "net" in Draw.strings assert "net" in Draw.strings
def test_ProcBox_draw(): def test_ProcBox_draw():
Box.calc_sizes() Box.calc_sizes()
assert len(ProcBox._draw_bg()) > 1 assert len(ProcBox._draw_bg()) > 1

View File

@ -1,32 +1,37 @@
from more_itertools import divide from more_itertools import divide
import bpytop from bpytop import (CORES, THREADS, create_box, floating_humanizer,
from bpytop import (CORES, SYSTEM, THREADS, Fx, create_box, floating_humanizer, get_cpu_core_mapping, get_cpu_name, units_to_bytes)
get_cpu_core_mapping, get_cpu_name, units_to_bytes)
def test_get_cpu_name(): def test_get_cpu_name():
assert isinstance(get_cpu_name(), str) assert isinstance(get_cpu_name(), str)
def test_get_cpu_core_mapping(): def test_get_cpu_core_mapping():
cpu_core_mapping = get_cpu_core_mapping() cpu_core_mapping = get_cpu_core_mapping()
assert isinstance(cpu_core_mapping, list) assert isinstance(cpu_core_mapping, list)
# Assert cpu submappings are sequential # Assert cpu submappings are sequential
for submapping in divide(THREADS//CORES, cpu_core_mapping): for submapping in divide(THREADS // CORES, cpu_core_mapping):
submapping = list(submapping) submapping = list(submapping)
for a, b in zip(submapping[:-1], submapping[1:]): for a, b in zip(submapping[:-1], submapping[1:]):
assert b - a == 1 assert b - a == 1
def test_create_box(): def test_create_box():
assert len(create_box(x=1, y=1, width=10, height=10, title="", title2="", line_color=None, title_color=None, fill=True, box=None)) > 1 assert len(
create_box(x=1, y=1, width=10, height=10, title="", title2="", line_color=None, title_color=None, fill=True,
box=None)) > 1
def test_floating_humanizer(): def test_floating_humanizer():
assert floating_humanizer(100) == "100 Byte" assert floating_humanizer(100) == "100 Byte"
assert floating_humanizer(100<<10) == "100 KiB" assert floating_humanizer(100 << 10) == "100 KiB"
assert floating_humanizer(100<<20, bit=True) == "800 Mib" assert floating_humanizer(100 << 20, bit=True) == "800 Mib"
assert floating_humanizer(100<<20, start=1) == "100 GiB" assert floating_humanizer(100 << 20, start=1) == "100 GiB"
assert floating_humanizer(100<<40, short=True) == "100T" assert floating_humanizer(100 << 40, short=True) == "100T"
assert floating_humanizer(100<<50, per_second=True) == "100 PiB/s" assert floating_humanizer(100 << 50, per_second=True) == "100 PiB/s"
def test_units_to_bytes(): def test_units_to_bytes():
assert units_to_bytes("10kbits") == 1280 assert units_to_bytes("10kbits") == 1280

View File

@ -1,21 +1,21 @@
from bpytop import Term
import os
from unittest import mock from unittest import mock
from bpytop import Term
def test_empty(): def test_empty():
assert Term.title() == "\033]0;\a" assert Term.title() == "\033]0;\a"
def test_nonempty(): def test_nonempty():
assert Term.title("BpyTOP") == "\033]0;BpyTOP\a" assert Term.title("BpyTOP") == "\033]0;BpyTOP\a"
def test_empty_with_environ(): def test_empty_with_environ():
with mock.patch.dict("os.environ", {"TERMINAL_TITLE": "hello"}, clear=True): with mock.patch.dict("os.environ", {"TERMINAL_TITLE": "hello"}, clear=True):
assert Term.title() == "\033]0;hello\a" assert Term.title() == "\033]0;hello\a"
def test_nonempty_with_environ(): def test_nonempty_with_environ():
with mock.patch.dict("os.environ", {"TERMINAL_TITLE": "hello"}, clear=True): with mock.patch.dict("os.environ", {"TERMINAL_TITLE": "hello"}, clear=True):
assert Term.title("BpyTOP") == "\033]0;hello BpyTOP\a" assert Term.title("BpyTOP") == "\033]0;hello BpyTOP\a"