rich handler refactor

This commit is contained in:
Will McGugan 2020-10-31 14:02:16 +00:00
parent 434eccb17e
commit 3c9cca19a5
5 changed files with 24 additions and 14 deletions

View File

@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added tracebacks_show_locals parameter to RichHandler
- Applied dim=True to indent guide styles
- Added max_string to Pretty
- Factored out RichHandler.get_style_and_level to allow for overriding in subclasses
## [9.1.0] - 2020-10-23

View File

@ -2,7 +2,7 @@ import logging
from datetime import datetime
from logging import Handler, LogRecord
from pathlib import Path
from typing import ClassVar, List, Optional, Type
from typing import ClassVar, List, Optional, Tuple, Type
from . import get_console
from ._log_render import LogRender
@ -83,16 +83,27 @@ class RichHandler(Handler):
self.tracebacks_word_wrap = tracebacks_word_wrap
self.tracebacks_show_locals = tracebacks_show_locals
def get_style_and_level(self, record: LogRecord) -> Tuple[str, str]:
"""Get the level name from the record.
Args:
record (LogRecord): LogRecord instance.
Returns:
Tuple[str, str]: A tuple of the style and level name.
"""
level_name = record.levelname
style = f"logging.level.{level_name.lower()}"
return (style, level_name)
def emit(self, record: LogRecord) -> None:
"""Invoked by logging."""
path = Path(record.pathname).name
log_style = f"logging.level.{record.levelname.lower()}"
log_style, level_name = self.get_style_and_level(record)
message = self.format(record)
time_format = None if self.formatter is None else self.formatter.datefmt
log_time = datetime.fromtimestamp(record.created)
level = Text()
level.append(record.levelname, log_style)
level = Text.styled(level_name, log_style)
traceback = None
if (

View File

@ -27,7 +27,6 @@ from typing import (
)
from . import filesize, get_console
from .progress_bar import ProgressBar
from .console import (
Console,
ConsoleRenderable,
@ -40,6 +39,7 @@ from .control import Control
from .highlighter import Highlighter
from .jupyter import JupyterMixin
from .live_render import LiveRender
from .progress_bar import ProgressBar
from .style import StyleType
from .table import Table
from .text import Text
@ -846,8 +846,8 @@ class Progress(JupyterMixin, RenderHook):
"""Refresh (render) the progress information."""
if self.console.is_jupyter: # pragma: no cover
try:
from ipywidgets import Output
from IPython.display import display
from ipywidgets import Output
except ImportError:
import warnings
@ -974,13 +974,13 @@ class Progress(JupyterMixin, RenderHook):
if __name__ == "__main__": # pragma: no coverage
import time
import random
import time
from .panel import Panel
from .rule import Rule
from .syntax import Syntax
from .table import Table
from .rule import Rule
syntax = Syntax(
'''def loop_last(values: Iterable[T]) -> Iterable[Tuple[bool, T]]:

View File

@ -1,4 +1,4 @@
from typing import Optional, Union
from typing import Union
from .cells import cell_len, set_cell_size
from .console import Console, ConsoleOptions, RenderResult

View File

@ -1,13 +1,11 @@
from typing import Any, Tuple, TYPE_CHECKING
from collections.abc import Mapping
from typing import TYPE_CHECKING, Any, Tuple
from .highlighter import ReprHighlighter
from .panel import Panel
from .pretty import Pretty
from .text import Text, TextType
from .table import Table
from .text import Text, TextType
if TYPE_CHECKING:
from .console import ConsoleRenderable, RenderableType