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

Ensure filename is passed to devtools from __init__ #383

Merged
merged 3 commits into from
Apr 12, 2022
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
5 changes: 4 additions & 1 deletion src/textual/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
from typing import Any

from rich.console import RenderableType
Expand All @@ -9,7 +10,9 @@ def log(*args: object, verbosity: int = 0, **kwargs) -> None:
from ._context import active_app

app = active_app.get()
app.log(*args, verbosity=verbosity, **kwargs)

caller = inspect.stack()[1]
app.log(*args, verbosity=verbosity, _textual_calling_frame=caller, **kwargs)


def panic(*args: RenderableType) -> None:
Expand Down
12 changes: 7 additions & 5 deletions src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,16 @@ def log(
self,
*objects: Any,
verbosity: int = 1,
caller: inspect.FrameInfo | None = None,
_textual_calling_frame: inspect.FrameInfo | None = None,
**kwargs,
) -> None:
"""Write to logs.

Args:
*objects (Any): Positional arguments are converted to string and written to logs.
verbosity (int, optional): Verbosity level 0-3. Defaults to 1.
_textual_calling_frame (inspect.FrameInfo | None): The frame info to include in
the log message sent to the devtools server.
"""
output = ""
try:
Expand All @@ -224,11 +226,11 @@ def log(
key_values = " ".join(f"{key}={value}" for key, value in kwargs.items())
output = " ".join((output, key_values))

if not caller:
caller = inspect.stack()[1]
if not _textual_calling_frame:
_textual_calling_frame = inspect.stack()[1]

calling_path = caller.filename
calling_lineno = caller.lineno
calling_path = _textual_calling_frame.filename
calling_lineno = _textual_calling_frame.lineno

if self.devtools.is_connected and verbosity <= self.log_verbosity:
if len(objects) > 1 or len(kwargs) >= 1 and output:
Expand Down
2 changes: 1 addition & 1 deletion src/textual/message_pump.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def is_running(self) -> bool:
return self._running

def log(self, *args, **kwargs) -> None:
return self.app.log(*args, **kwargs, caller=inspect.stack()[1])
return self.app.log(*args, **kwargs, _textual_calling_frame=inspect.stack()[1])

def set_parent(self, parent: MessagePump) -> None:
self._parent = parent
Expand Down