forked from jtpio/ipylab
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alan Fleming
committed
Oct 29, 2024
1 parent
118f448
commit 0f0edab
Showing
11 changed files
with
226 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Copyright (c) ipylab contributors. | ||
# Distributed under the terms of the Modified BSD License. | ||
|
||
from __future__ import annotations | ||
|
||
import logging | ||
from typing import TYPE_CHECKING, Any, Literal | ||
|
||
from ipylab._compat.enum import StrEnum | ||
from ipylab._compat.typing import TypedDict | ||
|
||
if TYPE_CHECKING: | ||
from ipywidgets import Widget | ||
|
||
from ipylab import App | ||
|
||
__all__ = ["LogLevel", "LogTypes", "LogPayloadType", "LogPayloadText", "LogPayloadHtml", "LogPayloadOutput"] | ||
|
||
class LogLevel(StrEnum): | ||
"The log levels available in Jupyterlab" | ||
debug = "debug" | ||
info = "info" | ||
warning = "warning" | ||
error = "error" | ||
critical = "critical" | ||
|
||
@classmethod | ||
def to_numeric(cls, value: LogLevel | int): | ||
return log_name_mappings[LogLevel(value)] | ||
|
||
@classmethod | ||
def to_level(cls, val: LogLevel | int) -> LogLevel: | ||
if isinstance(val, int): | ||
if val >= log_name_mappings[LogLevel.critical]: | ||
return LogLevel.critical | ||
if val >= log_name_mappings[LogLevel.error]: | ||
return LogLevel.error | ||
if val >= log_name_mappings[LogLevel.warning]: | ||
return LogLevel.warning | ||
if val >= log_name_mappings[LogLevel.info]: | ||
return LogLevel.info | ||
return LogLevel.debug | ||
return LogLevel(val) | ||
|
||
|
||
log_name_mappings = { | ||
LogLevel.debug: 10, | ||
LogLevel.info: 20, | ||
LogLevel.warning: 30, | ||
LogLevel.error: 40, | ||
LogLevel.critical: 50, | ||
} | ||
|
||
class LogTypes(StrEnum): | ||
text = "text" | ||
html = "html" | ||
output = "output" | ||
|
||
@classmethod | ||
def parse(cls, log): | ||
level = LogLevel.to_level(log["level"]) | ||
match LogTypes(log["type"]): | ||
case LogTypes.text: | ||
return LogPayloadText(type=LogTypes.text, level=level, data=log["data"]) | ||
case LogTypes.html: | ||
return LogPayloadHtml(type=LogTypes.html, level=level, data=log["data"]) | ||
case LogTypes.output: | ||
raise NotImplementedError | ||
return LogPayloadOutput(type=LogTypes.output, level=LogLevel(log["level"]), data=log["data"]) | ||
|
||
|
||
class LogPayloadBase(TypedDict): | ||
type: LogTypes | ||
level: LogLevel | int | ||
data: Any | ||
|
||
|
||
class LogPayloadText(LogPayloadBase): | ||
type: Literal[LogTypes.text] | ||
data: str | ||
|
||
|
||
class LogPayloadHtml(LogPayloadText): | ||
type: Literal[LogTypes.html] | ||
|
||
|
||
class LogPayloadOutput(LogPayloadBase): | ||
type: Literal[LogTypes.output] | ||
data: Widget | ||
|
||
|
||
LogPayloadType = LogPayloadBase | LogPayloadText | LogPayloadHtml | LogPayloadOutput | ||
|
||
|
||
class IpylabLogHandler(logging.Handler): | ||
def __init__(self, app: App) -> None: | ||
self.app = app | ||
self.app.observe(self._observe_app_log_level, "logger_level") | ||
super().__init__(LogLevel.to_numeric(self.app.logger_level)) | ||
|
||
def _observe_app_log_level(self, change: dict): | ||
self.setLevel(LogLevel.to_numeric(change["new"])) | ||
|
||
def emit(self, record): | ||
log = LogPayloadText(type=LogTypes.text, level=LogLevel.to_level(record.levelno), data=self.format(record)) | ||
self.app.send_log_message(log) | ||
|
||
|
||
class IpylabFormatter(logging.Formatter): | ||
pass |
Oops, something went wrong.