Skip to content
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

VSCode Logging #506

Open
wants to merge 48 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
d42d176
Added Logging for Vscode
mj023 Nov 29, 2023
ef10aab
Fixed mistake in json
mj023 Nov 29, 2023
ed38f14
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 29, 2023
a513fa5
Moved execution logging to live.py
mj023 Dec 6, 2023
b613a75
Merge branch 'vscode-integration' of https://github.com/mj023/pytask …
mj023 Dec 6, 2023
5f7b80a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 6, 2023
7846dee
added requests as dependency
mj023 Dec 6, 2023
d051db6
Merge branch 'vscode-integration' of https://github.com/mj023/pytask …
mj023 Dec 6, 2023
ceed57f
Add Func description
mj023 Dec 6, 2023
28a43e9
add requests to mypy
mj023 Dec 6, 2023
bb59416
Check for no exec info
mj023 Dec 6, 2023
bb1af38
suppress more exceptions
mj023 Dec 6, 2023
4ef51b0
Add new vscode module
mj023 Dec 13, 2023
945e933
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 13, 2023
24623fd
Suppress exceptions
mj023 Jan 17, 2024
8d5ca61
Remove old code
mj023 Jan 17, 2024
7db35cf
Remove Changes
mj023 Jan 17, 2024
1415d38
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 17, 2024
d0087a6
Merge branch 'pytask-dev:main' into vscode-integration
mj023 Jan 17, 2024
4ad1d36
Add vscode module
mj023 Jan 17, 2024
ec09e60
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 17, 2024
1722f1c
Fix URL error
mj023 Jan 17, 2024
3c188b5
Merge branch 'vscode-integration' of https://github.com/mj023/pytask …
mj023 Jan 17, 2024
224331f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 17, 2024
c856264
Check URL
mj023 Jan 17, 2024
c7de595
Merge branch 'vscode-integration' of https://github.com/mj023/pytask …
mj023 Jan 17, 2024
7d7bf21
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 17, 2024
e5d38f9
Disable qa
mj023 Jan 24, 2024
e6d1156
Merge branch 'vscode-integration' of https://github.com/mj023/pytask …
mj023 Jan 24, 2024
b9af139
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 24, 2024
b0aba34
Add noqa
mj023 Jan 24, 2024
208249b
Merge branch 'vscode-integration' of https://github.com/mj023/pytask …
mj023 Jan 24, 2024
5a28ed6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 24, 2024
2956580
change noqa
mj023 Jan 24, 2024
3166c8d
Merge branch 'vscode-integration' of https://github.com/mj023/pytask …
mj023 Jan 24, 2024
573d39a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 24, 2024
c1418c3
Merge branch 'main' into vscode-integration
mj023 Jan 24, 2024
f8945a7
Merge branch 'pytask-dev:main' into vscode-integration
mj023 Feb 7, 2024
2a856b2
Add tests for vscode module
mj023 Feb 7, 2024
ba12e7b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 7, 2024
a6c2dc9
Merge branch 'main' into vscode-integration
mj023 Feb 13, 2024
4d335d4
Send Logging to different urls
mj023 Mar 6, 2024
f2de23f
Merge branch 'vscode-integration' of https://github.com/mj023/pytask …
mj023 Mar 6, 2024
a3db9ae
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 6, 2024
4c68723
Merge branch 'pytask-dev:main' into vscode-integration
mj023 Apr 17, 2024
2e28de7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 17, 2024
1cd6281
Merge branch 'main' into vscode-integration
mj023 Jun 20, 2024
cde73fd
Make Port changeable
mj023 Jun 20, 2024
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 src/_pytask/pluginmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def pytask_add_hooks(pm: PluginManager) -> None:
"_pytask.skipping",
"_pytask.task",
"_pytask.warnings",
"_pytask.vscode",
)
register_hook_impls_from_modules(pm, builtin_hook_impl_modules)

Expand Down
96 changes: 96 additions & 0 deletions src/_pytask/vscode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""Contains Code for VSCode Logging."""

from __future__ import annotations

import contextlib
import json
import os
from threading import Thread
from typing import TYPE_CHECKING
from typing import Any
from urllib import request

from _pytask.config import hookimpl
from _pytask.console import console
from _pytask.console import render_to_string
from _pytask.nodes import PTaskWithPath
from _pytask.outcomes import CollectionOutcome
from _pytask.outcomes import TaskOutcome
from _pytask.traceback import Traceback

if TYPE_CHECKING:
from _pytask.node_protocols import PTask
from _pytask.reports import CollectionReport
from _pytask.reports import ExecutionReport
from _pytask.session import Session


def send_logging_vscode(url: str, data: dict[str, Any], timeout: float) -> None:
"""Send logging information to VSCode."""
with contextlib.suppress(Exception):
response = json.dumps(data).encode("utf-8")
req = request.Request(url, data=response) # noqa: S310
req.add_header("Content-Type", "application/json; charset=utf-8")
request.urlopen(req, timeout=timeout) # noqa: S310


@hookimpl(tryfirst=True)
def pytask_collect_log(
session: Session, reports: list[CollectionReport], tasks: list[PTask]
) -> None:
if (
os.environ.get("PYTASK_VSCODE") is not None
and session.config["command"] == "collect"
):
try:
port = int(os.environ.get("PYTASK_VSCODE"))
except ValueError:
port = 6000

Check warning on line 48 in src/_pytask/vscode.py

View check run for this annotation

Codecov / codecov/patch

src/_pytask/vscode.py#L47-L48

Added lines #L47 - L48 were not covered by tests
exitcode = "OK"
for report in reports:
if report.outcome == CollectionOutcome.FAIL:
exitcode = "COLLECTION_FAILED"
result = [
{"name": task.name, "path": str(task.path)}
if isinstance(task, PTaskWithPath)
else {"name": task.name, "path": ""}
for task in tasks
]
url = f"http://localhost:{port}/pytask/collect"
thread = Thread(
target=send_logging_vscode,
args=(
url,
{"exitcode": exitcode, "tasks": result},
0.00001,
),
)
thread.start()


@hookimpl(tryfirst=True)
def pytask_execute_task_log_end(session: Session, report: ExecutionReport) -> None: # noqa: ARG001
if os.environ.get("PYTASK_VSCODE") is not None:
try:
port = int(os.environ.get("PYTASK_VSCODE"))
except ValueError:
port = 6000

Check warning on line 77 in src/_pytask/vscode.py

View check run for this annotation

Codecov / codecov/patch

src/_pytask/vscode.py#L76-L77

Added lines #L76 - L77 were not covered by tests
if report.outcome == TaskOutcome.FAIL and report.exc_info is not None:
result = {
"type": "task",
"name": report.task.name,
"outcome": str(report.outcome),
"exc_info": render_to_string(Traceback(report.exc_info), console),
}
else:
result = {
"type": "task",
"name": report.task.name,
"outcome": str(report.outcome),
}
url = f"http://localhost:{port}/pytask/run"
thread = Thread(
target=send_logging_vscode,
args=(url, result, 0.00001),
)
thread.start()
48 changes: 48 additions & 0 deletions tests/test_vscode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from __future__ import annotations

import os
import textwrap

import pytest
from pytask import ExitCode
from pytask import cli


@pytest.mark.end_to_end()
def test_vscode_collect_failed(runner, tmp_path):
source = """
raise Exception
"""
os.environ["PYTASK_VSCODE"] = "6000"
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))

result = runner.invoke(cli, ["collect", tmp_path.as_posix()])
assert result.exit_code == ExitCode.COLLECTION_FAILED


@pytest.mark.end_to_end()
def test_vscode_collect(runner, tmp_path):
source = """
def task_raises():
return
"""
os.environ["PYTASK_VSCODE"] = "6000"
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))

result = runner.invoke(cli, ["collect", tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK


@pytest.mark.end_to_end()
def test_vscode_build(runner, tmp_path):
source = """
def task_example():
return
def task_raises():
raise Exception
"""
os.environ["PYTASK_VSCODE"] = "6000"
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))

result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.FAILED
Loading