From 0ff6921c61000887f7a40076572eeb77fb6c3760 Mon Sep 17 00:00:00 2001 From: Anthon van der Neut Date: Sat, 29 Jul 2023 13:16:30 +0200 Subject: [PATCH] views: issue warning on use of .daemonize add deprecation warning add intermediary Lona_2_0_DeprecationWarning so only one warnings needs to be set for filtering. update example code --- .../demos/daemonized-view/daemonized-view.py | 2 +- lona/html/node.py | 8 ++++ lona/html/nodes/forms/__init__.py | 6 +-- lona/html/nodes/forms/select.py | 6 +-- lona/html/parsing.py | 2 + lona/templating.py | 2 + lona/view.py | 6 +++ lona/warnings.py | 39 +++++++++++++++---- 8 files changed, 56 insertions(+), 15 deletions(-) diff --git a/doc/content/demos/daemonized-view/daemonized-view.py b/doc/content/demos/daemonized-view/daemonized-view.py index 6a9014c7..1fe58c0f 100644 --- a/doc/content/demos/daemonized-view/daemonized-view.py +++ b/doc/content/demos/daemonized-view/daemonized-view.py @@ -46,7 +46,7 @@ def handle_request(self, request): ) # tell Lona to not kill the view when the page gets refreshed - self.daemonize() + self.is_daemon = True while True: self.show(html) diff --git a/lona/html/node.py b/lona/html/node.py index f95489ef..ec3a1744 100644 --- a/lona/html/node.py +++ b/lona/html/node.py @@ -13,6 +13,7 @@ from lona.protocol import NODE_TYPE from lona.html.widget import Widget from lona.state import State +import lona.warnings def parse_style_string(style_string: str) -> dict[str, str]: @@ -457,3 +458,10 @@ def show(self): with self.lock: if 'display' in self.style and self.style['display'] == 'none': del self.style['display'] + + +class DeprecatedNode(Node): + + def __init__(self, *args, **kwargs): + lona.warnings.remove_2_0(_class=True) + super().__init__(*args, **kwargs) diff --git a/lona/html/nodes/forms/__init__.py b/lona/html/nodes/forms/__init__.py index 303e4bd7..4ceb06fa 100644 --- a/lona/html/nodes/forms/__init__.py +++ b/lona/html/nodes/forms/__init__.py @@ -1,5 +1,5 @@ +from lona.html.node import DeprecatedNode, Node from lona.events.event_types import CLICK -from lona.html.node import Node class Button(Node): @@ -25,7 +25,7 @@ def disabled(self, new_value): del self.attributes['disabled'] -class Datalist(Node): +class Datalist(DeprecatedNode): # TODO: remove in 2.0 TAG_NAME = 'datalist' @@ -35,7 +35,7 @@ class DataList(Node): TAG_NAME = 'datalist' -class Fieldset(Node): +class Fieldset(DeprecatedNode): # TODO: remove in 2.0 TAG_NAME = 'fieldset' diff --git a/lona/html/nodes/forms/select.py b/lona/html/nodes/forms/select.py index 97d2ccd5..821d9fa2 100644 --- a/lona/html/nodes/forms/select.py +++ b/lona/html/nodes/forms/select.py @@ -1,14 +1,14 @@ from lona.events.event_types import CHANGE -from lona.html.node import Node +from lona.html.node import DeprecatedNode -class Option(Node): +class Option(DeprecatedNode): # TODO: remove in 2.0 TAG_NAME = 'option' -class Select(Node): +class Select(DeprecatedNode): # TODO: remove in 2.0 TAG_NAME = 'select' diff --git a/lona/html/parsing.py b/lona/html/parsing.py index 1f13e4ed..bedc9dee 100644 --- a/lona/html/parsing.py +++ b/lona/html/parsing.py @@ -14,6 +14,7 @@ from lona.html.nodes.text_content import Div from lona.html.text_node import TextNode from lona.html.node import Node +import lona.warnings logger = logging.getLogger('lona') @@ -270,6 +271,7 @@ def HTML( # html string elif '<' in node or '>' in node: + lona.warnings.remove_2_0('HTML parsing') parsed_nodes = cast( list, parse_html( diff --git a/lona/templating.py b/lona/templating.py index 7f1ae428..9a5aa137 100644 --- a/lona/templating.py +++ b/lona/templating.py @@ -7,6 +7,7 @@ from lona.compat import get_client_version from lona.protocol import get_enum_values +import lona.warnings logger = logging.getLogger('lona.templating') @@ -40,6 +41,7 @@ def state(self): def client_version(self): # TODO: remove in Lona 2.0 + lona.warning.remove_2_0() return get_client_version() def load_stylesheets(self): diff --git a/lona/view.py b/lona/view.py index 93a706e7..81a95e95 100644 --- a/lona/view.py +++ b/lona/view.py @@ -15,6 +15,7 @@ from lona.connection import Connection from lona.channels import Channel from lona.request import Request +import lona.warnings # avoid import cycles if TYPE_CHECKING: # pragma: no cover @@ -349,6 +350,11 @@ def sleep(self, delay: float, result: T | None = None) -> T | None: def daemonize(self) -> None: # TODO: remove in 2.0 + lona.warnings.warn( # NOQA: G010 + '.daemonize() is deprecated, use .is_daemon = True\n (see: https://lona-web.org/1.x/api-reference/views.html#lonaview-daemonize)', + lona.warnings.DaemonizeDeprecationWarning, + stacklevel=2, + ) self.is_daemon = True def ping(self) -> Literal['pong']: diff --git a/lona/warnings.py b/lona/warnings.py index 510fc026..733c2313 100644 --- a/lona/warnings.py +++ b/lona/warnings.py @@ -1,8 +1,9 @@ -import warnings as orginal_warnings +import warnings as original_warnings +import inspect class ExtendedWarn: - warn = orginal_warnings.warn + warn = original_warnings.warn def __call__( self, message, category=None, stacklevel=1, source=None, callee=None, @@ -22,9 +23,9 @@ def __call__( warn = ExtendedWarn() -orginal_warnings.warn = warn # type: ignore +original_warnings.warn = warn # type: ignore -_original_formatwarning = orginal_warnings.formatwarning +_original_formatwarning = original_warnings.formatwarning def _formatwarning_with_callee(message, category, filename, lineno, line): @@ -43,13 +44,35 @@ def _formatwarning_with_callee(message, category, filename, lineno, line): return _original_formatwarning(message, category, filename, lineno, line) -orginal_warnings.formatwarning = _formatwarning_with_callee # type: ignore +original_warnings.formatwarning = _formatwarning_with_callee # type: ignore -class DictResponseDeprecationWarning(PendingDeprecationWarning): +class Lona_2_0_DeprecationWarning(PendingDeprecationWarning): pass -orginal_warnings.simplefilter( - 'once', category=DictResponseDeprecationWarning, +original_warnings.simplefilter( + 'once', category=Lona_2_0_DeprecationWarning, ) + + +class DictResponseDeprecationWarning(Lona_2_0_DeprecationWarning): + pass + + +class DaemonizeDeprecationWarning(Lona_2_0_DeprecationWarning): + pass + + +def remove_2_0(msg=None, _class=False): + if msg is None: + if _class: + msg = 'class ' + msg += inspect.stack()[1].frame.f_locals['self'].__class__.__name__ + else: + msg = inspect.stack()[1].function + '()' + original_warnings.warn( # NOQA: G010 + msg + ' will be removed in 2.0', # NOQA: G003 + Lona_2_0_DeprecationWarning, + stacklevel=2, + )