Skip to content

Commit 3f32f24

Browse files
committed
Refactored private public attributes for LiveExecution.
1 parent 7d0ad18 commit 3f32f24

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

src/_pytask/live.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ def pytask_post_parse(config: dict[str, Any]) -> None:
8181

8282
if config["verbose"] >= 1:
8383
live_execution = LiveExecution(
84-
live_manager,
85-
config["n_entries_in_table"],
86-
config["verbose"],
87-
config["editor_url_scheme"],
84+
live_manager=live_manager,
85+
n_entries_in_table=config["n_entries_in_table"],
86+
verbose=config["verbose"],
87+
editor_url_scheme=config["editor_url_scheme"],
8888
)
8989
config["pm"].register(live_execution, "live_execution")
9090

@@ -95,7 +95,7 @@ def pytask_post_parse(config: dict[str, Any]) -> None:
9595
@hookimpl(tryfirst=True)
9696
def pytask_execute_build(session: Session) -> None:
9797
live_execution = session.config["pm"].get_plugin("live_execution")
98-
live_execution._n_tasks = len(session.tasks)
98+
live_execution.n_tasks = len(session.tasks)
9999

100100

101101
@attr.s(eq=False)
@@ -147,25 +147,25 @@ def is_started(self) -> None:
147147
return self._live.is_started
148148

149149

150-
@attr.s(eq=False)
150+
@attr.s(eq=False, kw_only=True)
151151
class LiveExecution:
152152
"""A class for managing the table displaying task progress during the execution."""
153153

154-
_live_manager = attr.ib(type=LiveManager)
155-
_n_entries_in_table = attr.ib(type=int)
156-
_verbose = attr.ib(type=int)
157-
_editor_url_scheme = attr.ib(type=str)
158-
_running_tasks = attr.ib(factory=dict, type=Dict[str, Task])
154+
live_manager = attr.ib(type=LiveManager)
155+
n_entries_in_table = attr.ib(type=int)
156+
verbose = attr.ib(type=int)
157+
editor_url_scheme = attr.ib(type=str)
158+
n_tasks = attr.ib(default="x", type=Union[int, str])
159159
_reports = attr.ib(factory=list, type=List[Dict[str, Any]])
160-
_n_tasks = attr.ib(default="x", type=Union[int, str])
160+
_running_tasks = attr.ib(factory=dict, type=Dict[str, Task])
161161

162162
@hookimpl(hookwrapper=True)
163163
def pytask_execute_build(self) -> Generator[None, None, None]:
164164
"""Wrap the execution with the live manager and yield a complete table at the
165165
end."""
166-
self._live_manager.start()
166+
self.live_manager.start()
167167
yield
168-
self._live_manager.stop(transient=True)
168+
self.live_manager.stop(transient=True)
169169
table = self._generate_table(
170170
reduce_table=False, sort_table=True, add_caption=False
171171
)
@@ -195,9 +195,9 @@ def _generate_table(
195195
if more entries are requested, the list is filled up with completed tasks.
196196
197197
"""
198-
n_reports_to_display = self._n_entries_in_table - len(self._running_tasks)
198+
n_reports_to_display = self.n_entries_in_table - len(self._running_tasks)
199199

200-
if self._verbose < 2:
200+
if self.verbose < 2:
201201
reports = [
202202
report
203203
for report in self._reports
@@ -227,7 +227,7 @@ def _generate_table(
227227
if add_caption:
228228
caption_kwargs = {
229229
"caption": Text(
230-
f"Completed: {len(self._reports)}/{self._n_tasks}",
230+
f"Completed: {len(self._reports)}/{self.n_tasks}",
231231
style=Style(dim=True, italic=False),
232232
),
233233
"caption_justify": "right",
@@ -243,15 +243,15 @@ def _generate_table(
243243
table.add_row(
244244
format_task_id(
245245
report["task"],
246-
editor_url_scheme=self._editor_url_scheme,
246+
editor_url_scheme=self.editor_url_scheme,
247247
short_name=True,
248248
),
249249
Text(report["outcome"].symbol, style=report["outcome"].style),
250250
)
251251
for task in self._running_tasks.values():
252252
table.add_row(
253253
format_task_id(
254-
task, editor_url_scheme=self._editor_url_scheme, short_name=True
254+
task, editor_url_scheme=self.editor_url_scheme, short_name=True
255255
),
256256
"running",
257257
)
@@ -272,7 +272,7 @@ def _update_table(
272272
table = self._generate_table(
273273
reduce_table=reduce_table, sort_table=sort_table, add_caption=add_caption
274274
)
275-
self._live_manager.update(table)
275+
self.live_manager.update(table)
276276

277277
def update_running_tasks(self, new_running_task: Task) -> None:
278278
"""Add a new running task."""

tests/test_live.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ def test_live_execution_sequentially(capsys, tmp_path):
6565
task.short_name = "task_module.py::task_example"
6666

6767
live_manager = LiveManager()
68-
live = LiveExecution(live_manager, 20, 1, "no_link")
68+
live = LiveExecution(
69+
live_manager=live_manager,
70+
n_entries_in_table=20,
71+
verbose=1,
72+
editor_url_scheme="no_link",
73+
)
6974

7075
live_manager.start()
7176
live.update_running_tasks(task)
@@ -118,7 +123,12 @@ def test_live_execution_displays_skips_and_persists(capsys, tmp_path, verbose, o
118123
task.short_name = "task_module.py::task_example"
119124

120125
live_manager = LiveManager()
121-
live = LiveExecution(live_manager, 20, verbose, "no_link")
126+
live = LiveExecution(
127+
live_manager=live_manager,
128+
n_entries_in_table=20,
129+
verbose=verbose,
130+
editor_url_scheme="no_link",
131+
)
122132

123133
live_manager.start()
124134
live.update_running_tasks(task)
@@ -162,8 +172,13 @@ def test_live_execution_displays_subset_of_table(capsys, tmp_path, n_entries_in_
162172
running_task.short_name = "task_module.py::task_running"
163173

164174
live_manager = LiveManager()
165-
live = LiveExecution(live_manager, n_entries_in_table, 1, "no_link")
166-
live._n_tasks = 2
175+
live = LiveExecution(
176+
live_manager=live_manager,
177+
n_entries_in_table=n_entries_in_table,
178+
verbose=1,
179+
editor_url_scheme="no_link",
180+
n_tasks=2,
181+
)
167182

168183
live_manager.start()
169184
live.update_running_tasks(running_task)
@@ -210,7 +225,12 @@ def test_live_execution_skips_do_not_crowd_out_displayed_tasks(capsys, tmp_path)
210225
task.short_name = "task_module.py::task_example"
211226

212227
live_manager = LiveManager()
213-
live = LiveExecution(live_manager, 20, 1, "no_link")
228+
live = LiveExecution(
229+
live_manager=live_manager,
230+
n_entries_in_table=20,
231+
verbose=1,
232+
editor_url_scheme="no_link",
233+
)
214234

215235
live_manager.start()
216236
live.update_running_tasks(task)

0 commit comments

Comments
 (0)