Debian Codegraph @

s_tui/builtin_stress_menu.py

raw symbols SCIP
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2017-2025 Alex Manuskin, Gil Tsuker
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18
19 """Configuration menu for the built-in Python CPU stresser."""
20
21 from __future__ import annotations
22
23 import logging
24 import re
25 from collections.abc import Callable
26
27 import psutil
28 import urwid
29
30 from s_tui.builtin_stresser import (
31 STRATEGIES,
32 STRATEGY_LABELS,
33 get_default_strategy,
34 strategy_available,
35 )
36
37
38 class BuiltinStressMenu:
39 MAX_TITLE_LEN = 50
40
41 def __init__(self, return_fn: Callable[[], None]) -> None:
42 self.return_fn = return_fn
43
44 self.num_workers = "1"
45 try:
46 self.num_workers = str(psutil.cpu_count())
47 logging.info("builtin stress default workers %s", self.num_workers)
48 except OSError as err:
49 logging.debug(err)
50
51 self.strategy = get_default_strategy()
52 self._pending_strategy = self.strategy
53
54 self.num_workers_ctrl = urwid.Edit("CPU worker count: ", self.num_workers)
55
56 # Strategy radio buttons
57 self._strategy_group: list[urwid.RadioButton] = []
58 self._strategy_buttons: dict[str, urwid.RadioButton] = {}
59 strategy_widgets: list[urwid.Widget] = []
60 for key in STRATEGIES:
61 label = STRATEGY_LABELS[key]
62 if not strategy_available(key):
63 label += " (requires numpy)"
64 rb = urwid.RadioButton(
65 self._strategy_group,
66 label,
67 state=(key == self.strategy),
68 on_state_change=self._on_strategy_change,
69 user_data=key,
70 )
71 self._strategy_buttons[key] = rb
72 strategy_widgets.append(rb)
73
74 default_button = urwid.Button("Default", on_press=self.on_default)
75 default_button._label.align = "center"
76
77 save_button = urwid.Button("Save", on_press=self.on_save)
78 save_button._label.align = "center"
79
80 cancel_button = urwid.Button("Cancel", on_press=self.on_cancel)
81 cancel_button._label.align = "center"
82
83 if_buttons = urwid.Columns([default_button, save_button, cancel_button])
84
85 title = urwid.Text(("bold text", " s-tui stress options \n"), "center")
86
87 self.titles = [
88 title,
89 self.num_workers_ctrl,
90 urwid.Divider("-"),
91 urwid.Text(("bold text", "Strategy:")),
92 *strategy_widgets,
93 urwid.Divider("-"),
94 if_buttons,
95 ]
96
97 self.main_window = urwid.LineBox(urwid.ListBox(self.titles))
98
99 def _on_strategy_change(
100 self, button: urwid.RadioButton, state: bool, key: str
101 ) -> None:
102 if state:
103 self._pending_strategy = key
104
105 def get_size(self) -> tuple[int, int]:
106 return len(self.titles) + 5, self.MAX_TITLE_LEN
107
108 def get_num_workers(self) -> int:
109 """Return the configured number of workers (minimum 1)."""
110 try:
111 return max(1, int(self.num_workers))
112 except ValueError:
113 return 1
114
115 def get_strategy(self) -> str:
116 """Return the selected strategy key."""
117 return self.strategy
118
119 def _restore_ui(self) -> None:
120 """Reset UI controls to match committed state."""
121 self.num_workers_ctrl.set_edit_text(self.num_workers)
122 self._strategy_buttons[self.strategy].set_state(True)
123 self._pending_strategy = self.strategy
124
125 def on_default(self, _) -> None:
126 self.num_workers = str(psutil.cpu_count() or 1)
127 self.strategy = get_default_strategy()
128 self._restore_ui()
129 self.return_fn()
130
131 def on_save(self, _) -> None:
132 raw = self.num_workers_ctrl.get_edit_text()
133 if re.match(r"\A[0-9]+\Z", raw) and int(raw) > 0:
134 self.num_workers = raw
135 else:
136 self.num_workers = str(psutil.cpu_count() or 1)
137 self.strategy = self._pending_strategy
138 self._restore_ui()
139 self.return_fn()
140
141 def on_cancel(self, _) -> None:
142 self._restore_ui()
143 self.return_fn()