Skip to content

Simplify the logging of task outcomes. #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ all releases are available on `PyPI <https://pypi.org/project/pytask>`_ and
- :gh:`101` allows to visualize the project's DAG.
- :gh:`102` adds an example if a parametrization provides not the number of arguments
specified in the signature.
- :gh:`105` simplifies the logging of the tasks.


0.0.14 - 2021-03-23
Expand Down
11 changes: 5 additions & 6 deletions src/_pytask/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ def pytask_execute_task_process_report(session, report):
task = report.task
if report.success:
_update_states_in_database(session.dag, task.name)
report.symbol = "."
report.color = ColorCode.SUCCESS
else:
report.symbol = "F"
report.color = ColorCode.FAILED
for descending_task_name in descending_tasks(task.name, session.dag):
descending_task = session.dag.nodes[descending_task_name]["task"]
descending_task.markers.append(
Expand All @@ -150,12 +154,7 @@ def pytask_execute_task_process_report(session, report):
@hookimpl(trylast=True)
def pytask_execute_task_log_end(report):
"""Log task outcome."""
if report.success:
console.print(".", style=ColorCode.SUCCESS, end="")
else:
console.print("F", style=ColorCode.FAILED, end="")

return True
console.print(report.symbol, style=report.color, end="")


@hookimpl
Expand Down
4 changes: 2 additions & 2 deletions src/_pytask/hookspecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ def pytask_execute_task_teardown(session, task):
def pytask_execute_task_process_report(session, report):
"""Process the report of a task.

This hook allows to process each report generated by a task. It is either based on
an exception or a success.
This hook allows to process each report generated by a task which is either based on
an exception or a success. Set the color and the symbol for logging.

Some exceptions are intentionally raised like skips, but they should not be reported
as failures.
Expand Down
13 changes: 2 additions & 11 deletions src/_pytask/persist.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Implement the ability for tasks to persist."""
from _pytask.config import hookimpl
from _pytask.console import console
from _pytask.dag import node_and_neighbors
from _pytask.enums import ColorCode
from _pytask.exceptions import NodeNotFoundError
Expand Down Expand Up @@ -53,13 +52,5 @@ def pytask_execute_task_process_report(report):
"""
if report.exc_info and isinstance(report.exc_info[1], Persisted):
report.success = True


@hookimpl
def pytask_execute_task_log_end(report):
"""Log a persisting task with a green p."""
if report.success:
if report.exc_info:
if isinstance(report.exc_info[1], Persisted):
console.print("p", style=ColorCode.SUCCESS, end="")
return True
report.symbol = "p"
report.color = ColorCode.SUCCESS
2 changes: 2 additions & 0 deletions src/_pytask/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class ExecutionReport:
success = attr.ib(type=bool)
exc_info = attr.ib(default=None)
sections = attr.ib(factory=list)
symbol = attr.ib(default="?")
color = attr.ib(default=None)

@classmethod
def from_task_and_exception(cls, task, exc_info):
Expand Down
28 changes: 7 additions & 21 deletions src/_pytask/skipping.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""This module contains everything related to skipping tasks."""
from _pytask.config import hookimpl
from _pytask.console import console
from _pytask.dag import descending_tasks
from _pytask.enums import ColorCode
from _pytask.mark import get_specific_markers_from_task
Expand Down Expand Up @@ -74,9 +73,14 @@ def pytask_execute_task_process_report(session, report):
if report.exc_info:
if isinstance(report.exc_info[1], SkippedUnchanged):
report.success = True
report.symbol = "s"
report.color = ColorCode.SUCCESS

elif isinstance(report.exc_info[1], Skipped):
report.success = True
report.symbol = "s"
report.color = ColorCode.SKIPPED

for descending_task_name in descending_tasks(task.name, session.dag):
descending_task = session.dag.nodes[descending_task_name]["task"]
descending_task.markers.append(
Expand All @@ -90,28 +94,10 @@ def pytask_execute_task_process_report(session, report):
elif isinstance(report.exc_info[1], SkippedAncestorFailed):
report.success = False
report.exc_info = remove_traceback_from_exc_info(report.exc_info)
report.symbol = "s"
report.color = ColorCode.FAILED

if report.exc_info and isinstance(
report.exc_info[1], (Skipped, SkippedUnchanged, SkippedAncestorFailed)
):
return True


@hookimpl
def pytask_execute_task_log_end(report):
"""Log the status of a skipped task."""
if report.success:
if report.exc_info:
if isinstance(report.exc_info[1], Skipped):
console.print("s", style=ColorCode.SKIPPED, end="")
elif isinstance(report.exc_info[1], SkippedUnchanged):
console.print("s", style=ColorCode.SUCCESS, end="")
else:
if report.exc_info and isinstance(report.exc_info[1], SkippedAncestorFailed):
console.print("s", style=ColorCode.FAILED, end="")

if report.exc_info and isinstance(
report.exc_info[1], (Skipped, SkippedUnchanged, SkippedAncestorFailed)
):
# Return non-None value so that the task is not logged again.
return True
35 changes: 0 additions & 35 deletions tests/test_skipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from _pytask.outcomes import Skipped
from _pytask.outcomes import SkippedAncestorFailed
from _pytask.outcomes import SkippedUnchanged
from _pytask.report import ExecutionReport
from _pytask.skipping import pytask_execute_task_log_end
from _pytask.skipping import pytask_execute_task_setup
from pytask import main

Expand Down Expand Up @@ -217,36 +215,3 @@ class Task:

with expectation:
pytask_execute_task_setup(task)


@pytest.mark.unit
@pytest.mark.parametrize(
("exception", "character"),
[
(SkippedUnchanged, "s"),
(SkippedAncestorFailed, "s"),
(Skipped, "s"),
(None, ""),
],
)
def test_pytask_execute_task_log_end(capsys, exception, character):
if isinstance(exception, (Skipped, SkippedUnchanged)):
report = ExecutionReport.from_task_and_exception((), exception())
report.success = True
elif isinstance(exception, SkippedAncestorFailed):
report = ExecutionReport.from_task_and_exception((), SkippedAncestorFailed)
report.success = True
else:
dummy_task = DummyClass()
dummy_task._report_sections = None
report = ExecutionReport.from_task(dummy_task)

out = pytask_execute_task_log_end(report)

captured = capsys.readouterr()
if isinstance(exception, (Skipped, SkippedUnchanged)):
assert out
assert captured.out == character
else:
assert out is None
assert captured.out == ""