-
Notifications
You must be signed in to change notification settings - Fork 5
refactor: replace SubProject inheritance with has-a Fetcher composition #1170
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
base: main
Are you sure you want to change the base?
Changes from all commits
c04b977
d9bc15e
6e04339
5d77df6
b2cf9ae
bece1d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,47 +7,33 @@ | |
| import sys | ||
| import types | ||
| from contextlib import nullcontext | ||
| from logging import LogRecord | ||
| from typing import TYPE_CHECKING, Any, cast | ||
| from typing import Any, cast | ||
|
|
||
| from rich.console import Console, ConsoleRenderable | ||
| from rich._log_render import LogRender # type: ignore[import-untyped] | ||
| from rich.console import Console | ||
| from rich.highlighter import NullHighlighter | ||
| from rich.logging import RichHandler | ||
| from rich.markup import escape as markup_escape | ||
| from rich.status import Status | ||
| from rich.table import Table | ||
|
|
||
| from dfetch import __version__ | ||
|
|
||
| if TYPE_CHECKING: | ||
| from rich.traceback import Traceback | ||
|
|
||
| def _make_non_expanding_log_render(**kwargs: Any) -> Any: | ||
| """Return a LogRender callable that disables table expansion. | ||
|
|
||
| class _NoExpandRichHandler(RichHandler): | ||
| """RichHandler that disables table expansion to prevent blank lines in asciicasts. | ||
|
|
||
| Rich's LogRender uses expand=True on its Table.grid, which pads every | ||
| log message with trailing spaces to fill the full terminal width. When | ||
| asciinema records the output the padded line fills the terminal exactly, | ||
| causing the subsequent newline to produce a blank line in the cast | ||
| player. Overriding render to set expand=False removes the trailing | ||
| spaces and avoids the spurious blank lines. | ||
| Used when recording with asciinema to prevent Rich's ``expand=True`` from | ||
| padding log lines to the full terminal width, which produces spurious blank | ||
| lines in the cast player. | ||
| """ | ||
| renderer = LogRender(**kwargs) | ||
|
|
||
| def render( | ||
| self, | ||
| *, | ||
| record: LogRecord, | ||
| traceback: Traceback | None, | ||
| message_renderable: ConsoleRenderable, | ||
| ) -> ConsoleRenderable: | ||
| """Render log entry without expanding the table to the full terminal width.""" | ||
| renderable = super().render( | ||
| record=record, traceback=traceback, message_renderable=message_renderable | ||
| ) | ||
| if isinstance(renderable, Table): | ||
| renderable.expand = False | ||
| return renderable | ||
| def _render(*args: Any, **kw: Any) -> Any: | ||
| table = renderer(*args, **kw) | ||
| table.expand = False | ||
| return table | ||
|
|
||
| return _render | ||
|
|
||
|
|
||
| def make_console(no_color: bool = False) -> Console: | ||
|
|
@@ -63,8 +49,7 @@ def configure_root_logger(console: Console | None = None) -> None: | |
| """Configure the root logger with RichHandler using the provided Console.""" | ||
| console = console or make_console() | ||
|
|
||
| handler_class = _NoExpandRichHandler if os.getenv("ASCIINEMA_REC") else RichHandler | ||
| handler = handler_class( | ||
| handler = RichHandler( | ||
| console=console, | ||
| show_time=False, | ||
| show_path=False, | ||
|
|
@@ -74,6 +59,18 @@ def configure_root_logger(console: Console | None = None) -> None: | |
| highlighter=NullHighlighter(), | ||
| ) | ||
|
|
||
| if os.getenv("ASCIINEMA_REC"): | ||
| # Rich's LogRender uses expand=True on its Table.grid, which pads every | ||
| # log message with trailing spaces to fill the full terminal width. When | ||
| # asciinema records the output the padded line fills the terminal exactly, | ||
| # causing the subsequent newline to produce a blank line in the cast | ||
| # player. Wrapping _log_render so it returns a non-expanding table | ||
| # removes the trailing spaces and avoids the spurious blank lines. | ||
| no_expand = _make_non_expanding_log_render( | ||
| show_time=False, show_level=False, show_path=False | ||
| ) | ||
| handler._log_render = no_expand # pylint: disable=protected-access | ||
|
|
||
|
Comment on lines
+69
to
+73
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🌐 Web query:
💡 Result: In Rich 15.0.0, there is no public extension point or configuration parameter for the RichHandler class designed to disable log table expansion or the associated column-based wrapping [1][2][3][4]. The RichHandler is explicitly architected to render logs using a table-based layout to maintain aligned, readable output in terminal environments [3][5][4]. Consequently, the internal rendering logic—including the use of a table that enforces fixed-width columns—is considered a core part of its design rather than an optional behavior [3][5]. For users who require control over this rendering (e.g., to disable hard wrapping or column expansion), the library maintainers recommend one of the following approaches: 1. Subclassing and overriding: You can create a custom logging handler by inheriting from logging.Handler or by subclassing RichHandler and overriding the emit method to implement your own rendering logic [3][5]. 2. Custom LogRender: While RichHandler uses an internal LogRender class, it is not intended to be exposed as a public API [3][5]. However, as documented in community discussions, some users have achieved specific layout requirements by defining a custom renderer class and assigning it to the _log_render attribute, though this relies on internal implementation details that may change [3][5]. If you are using the handler for output that is not intended for human consumption in a terminal (e.g., piping logs to a file or another process), it is generally recommended to use a standard logging.StreamHandler or a custom handler that does not perform terminal-specific layout formatting [6][5]. Citations:
🏁 Script executed: # First, let's check if dfetch/log.py exists and get its basic structure
fd -t f "log.py" dfetch/Repository: dfetch-org/dfetch Length of output: 75 🏁 Script executed: # Read the relevant sections of dfetch/log.py
cat -n dfetch/log.py | head -80Repository: dfetch-org/dfetch Length of output: 3123 🏁 Script executed: # Also check if there's an import statement or usage of _AsciinemaRichHandler later in the file
rg "_AsciinemaRichHandler|_log_render|ASCIINEMA_REC" dfetch/log.py -nRepository: dfetch-org/dfetch Length of output: 447 🏁 Script executed: # Check if assigning to self._log_render in __init__ would trigger protected-access warnings
# Also verify no other inline suppressions exist in the file
rg "#\s*(noqa|type:\s*ignore|pylint:\s*disable|pyright:\s*ignore)" dfetch/log.py -nRepository: dfetch-org/dfetch Length of output: 296 🏁 Script executed: # Verify the exact line count of the file to ensure line references are accurate
wc -l dfetch/log.pyRepository: dfetch-org/dfetch Length of output: 79 Remove inline suppression by moving handler customization to a dedicated subclass. Line 72 contains an inline Create a dedicated Suggested refactor+class _AsciinemaRichHandler(RichHandler):
+ """RichHandler variant with non-expanding log table rendering."""
+
+ def __init__(self, **kwargs: Any) -> None:
+ super().__init__(**kwargs)
+ self._log_render = _make_non_expanding_log_render(
+ show_time=False, show_level=False, show_path=False
+ )
+
def configure_root_logger(console: Console | None = None) -> None:
"""Configure the root logger with RichHandler using the provided Console."""
console = console or make_console()
+ handler_cls: type[RichHandler] = (
+ _AsciinemaRichHandler if os.getenv("ASCIINEMA_REC") else RichHandler
+ )
- handler = RichHandler(
+ handler = handler_cls(
console=console,
show_time=False,
show_path=False,
show_level=False,
markup=True,
rich_tracebacks=True,
highlighter=NullHighlighter(),
)
-
- if os.getenv("ASCIINEMA_REC"):
- # Rich's LogRender uses expand=True on its Table.grid, which pads every
- # log message with trailing spaces to fill the full terminal width. When
- # asciinema records the output the padded line fills the terminal exactly,
- # causing the subsequent newline to produce a blank line in the cast
- # player. Wrapping _log_render so it returns a non-expanding table
- # removes the trailing spaces and avoids the spurious blank lines.
- no_expand = _make_non_expanding_log_render(
- show_time=False, show_level=False, show_path=False
- )
- handler._log_render = no_expand # pylint: disable=protected-accessRich 15.0.0 provides no public API to disable log table expansion; the library maintainers recommend subclassing 🤖 Prompt for AI AgentsSource: Coding guidelines |
||
| logging.basicConfig( | ||
| level=logging.INFO, | ||
| format="%(message)s", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.