Skip to content

Commit 650334d

Browse files
authored
Simplify the logging of task outcomes. (#105)
1 parent 9ebd567 commit 650334d

File tree

7 files changed

+19
-75
lines changed

7 files changed

+19
-75
lines changed

docs/changes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ all releases are available on `PyPI <https://pypi.org/project/pytask>`_ and
2727
- :gh:`101` allows to visualize the project's DAG.
2828
- :gh:`102` adds an example if a parametrization provides not the number of arguments
2929
specified in the signature.
30+
- :gh:`105` simplifies the logging of the tasks.
3031

3132

3233
0.0.14 - 2021-03-23

src/_pytask/execute.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,11 @@ def pytask_execute_task_process_report(session, report):
129129
task = report.task
130130
if report.success:
131131
_update_states_in_database(session.dag, task.name)
132+
report.symbol = "."
133+
report.color = ColorCode.SUCCESS
132134
else:
135+
report.symbol = "F"
136+
report.color = ColorCode.FAILED
133137
for descending_task_name in descending_tasks(task.name, session.dag):
134138
descending_task = session.dag.nodes[descending_task_name]["task"]
135139
descending_task.markers.append(
@@ -150,12 +154,7 @@ def pytask_execute_task_process_report(session, report):
150154
@hookimpl(trylast=True)
151155
def pytask_execute_task_log_end(report):
152156
"""Log task outcome."""
153-
if report.success:
154-
console.print(".", style=ColorCode.SUCCESS, end="")
155-
else:
156-
console.print("F", style=ColorCode.FAILED, end="")
157-
158-
return True
157+
console.print(report.symbol, style=report.color, end="")
159158

160159

161160
@hookimpl

src/_pytask/hookspecs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ def pytask_execute_task_teardown(session, task):
330330
def pytask_execute_task_process_report(session, report):
331331
"""Process the report of a task.
332332
333-
This hook allows to process each report generated by a task. It is either based on
334-
an exception or a success.
333+
This hook allows to process each report generated by a task which is either based on
334+
an exception or a success. Set the color and the symbol for logging.
335335
336336
Some exceptions are intentionally raised like skips, but they should not be reported
337337
as failures.

src/_pytask/persist.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Implement the ability for tasks to persist."""
22
from _pytask.config import hookimpl
3-
from _pytask.console import console
43
from _pytask.dag import node_and_neighbors
54
from _pytask.enums import ColorCode
65
from _pytask.exceptions import NodeNotFoundError
@@ -53,13 +52,5 @@ def pytask_execute_task_process_report(report):
5352
"""
5453
if report.exc_info and isinstance(report.exc_info[1], Persisted):
5554
report.success = True
56-
57-
58-
@hookimpl
59-
def pytask_execute_task_log_end(report):
60-
"""Log a persisting task with a green p."""
61-
if report.success:
62-
if report.exc_info:
63-
if isinstance(report.exc_info[1], Persisted):
64-
console.print("p", style=ColorCode.SUCCESS, end="")
65-
return True
55+
report.symbol = "p"
56+
report.color = ColorCode.SUCCESS

src/_pytask/report.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class ExecutionReport:
4444
success = attr.ib(type=bool)
4545
exc_info = attr.ib(default=None)
4646
sections = attr.ib(factory=list)
47+
symbol = attr.ib(default="?")
48+
color = attr.ib(default=None)
4749

4850
@classmethod
4951
def from_task_and_exception(cls, task, exc_info):

src/_pytask/skipping.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""This module contains everything related to skipping tasks."""
22
from _pytask.config import hookimpl
3-
from _pytask.console import console
43
from _pytask.dag import descending_tasks
54
from _pytask.enums import ColorCode
65
from _pytask.mark import get_specific_markers_from_task
@@ -74,9 +73,14 @@ def pytask_execute_task_process_report(session, report):
7473
if report.exc_info:
7574
if isinstance(report.exc_info[1], SkippedUnchanged):
7675
report.success = True
76+
report.symbol = "s"
77+
report.color = ColorCode.SUCCESS
7778

7879
elif isinstance(report.exc_info[1], Skipped):
7980
report.success = True
81+
report.symbol = "s"
82+
report.color = ColorCode.SKIPPED
83+
8084
for descending_task_name in descending_tasks(task.name, session.dag):
8185
descending_task = session.dag.nodes[descending_task_name]["task"]
8286
descending_task.markers.append(
@@ -90,28 +94,10 @@ def pytask_execute_task_process_report(session, report):
9094
elif isinstance(report.exc_info[1], SkippedAncestorFailed):
9195
report.success = False
9296
report.exc_info = remove_traceback_from_exc_info(report.exc_info)
97+
report.symbol = "s"
98+
report.color = ColorCode.FAILED
9399

94100
if report.exc_info and isinstance(
95101
report.exc_info[1], (Skipped, SkippedUnchanged, SkippedAncestorFailed)
96102
):
97103
return True
98-
99-
100-
@hookimpl
101-
def pytask_execute_task_log_end(report):
102-
"""Log the status of a skipped task."""
103-
if report.success:
104-
if report.exc_info:
105-
if isinstance(report.exc_info[1], Skipped):
106-
console.print("s", style=ColorCode.SKIPPED, end="")
107-
elif isinstance(report.exc_info[1], SkippedUnchanged):
108-
console.print("s", style=ColorCode.SUCCESS, end="")
109-
else:
110-
if report.exc_info and isinstance(report.exc_info[1], SkippedAncestorFailed):
111-
console.print("s", style=ColorCode.FAILED, end="")
112-
113-
if report.exc_info and isinstance(
114-
report.exc_info[1], (Skipped, SkippedUnchanged, SkippedAncestorFailed)
115-
):
116-
# Return non-None value so that the task is not logged again.
117-
return True

tests/test_skipping.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
from _pytask.outcomes import Skipped
77
from _pytask.outcomes import SkippedAncestorFailed
88
from _pytask.outcomes import SkippedUnchanged
9-
from _pytask.report import ExecutionReport
10-
from _pytask.skipping import pytask_execute_task_log_end
119
from _pytask.skipping import pytask_execute_task_setup
1210
from pytask import main
1311

@@ -217,36 +215,3 @@ class Task:
217215

218216
with expectation:
219217
pytask_execute_task_setup(task)
220-
221-
222-
@pytest.mark.unit
223-
@pytest.mark.parametrize(
224-
("exception", "character"),
225-
[
226-
(SkippedUnchanged, "s"),
227-
(SkippedAncestorFailed, "s"),
228-
(Skipped, "s"),
229-
(None, ""),
230-
],
231-
)
232-
def test_pytask_execute_task_log_end(capsys, exception, character):
233-
if isinstance(exception, (Skipped, SkippedUnchanged)):
234-
report = ExecutionReport.from_task_and_exception((), exception())
235-
report.success = True
236-
elif isinstance(exception, SkippedAncestorFailed):
237-
report = ExecutionReport.from_task_and_exception((), SkippedAncestorFailed)
238-
report.success = True
239-
else:
240-
dummy_task = DummyClass()
241-
dummy_task._report_sections = None
242-
report = ExecutionReport.from_task(dummy_task)
243-
244-
out = pytask_execute_task_log_end(report)
245-
246-
captured = capsys.readouterr()
247-
if isinstance(exception, (Skipped, SkippedUnchanged)):
248-
assert out
249-
assert captured.out == character
250-
else:
251-
assert out is None
252-
assert captured.out == ""

0 commit comments

Comments
 (0)