From 0d1a6b1ac4c5bb513f021ad51a66f384e1123251 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/compat.py | 5 +++ 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/view_runtime.py | 2 + lona/warnings.py | 39 +++++++++++++++---- 10 files changed, 63 insertions(+), 15 deletions(-) diff --git a/doc/content/demos/daemonized-view/daemonized-view.py b/doc/content/demos/daemonized-view/daemonized-view.py index 61736019..88a966f3 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/compat.py b/lona/compat.py index 3353467a..ce7e9223 100644 --- a/lona/compat.py +++ b/lona/compat.py @@ -1,5 +1,6 @@ import logging import os +import lona.warnings logger = logging.getLogger('lona') @@ -14,6 +15,7 @@ def get_client_version(): # TODO: remove in 2.0 + lona.warnings.remove_2_0() client_version = int(os.environ.get(CLIENT_VERSION_ENV_VAR_NAME, '1')) if client_version not in CLIENT_VERSION_ENV_VAR_VALUES: @@ -31,6 +33,7 @@ def get_client_version(): def set_client_version(version): # TODO: remove in 2.0 + lona.warnings.remove_2_0() if version not in CLIENT_VERSION_ENV_VAR_VALUES: raise RuntimeError(f'invalid client version number: {version}') @@ -45,6 +48,7 @@ def set_client_version(version): def get_use_future_node_classes(): # TODO: remove in 2.0 + lona.warnings.remove_2_0() raw_use_future_node_classes = os.environ.get( USE_FUTURE_NODE_CLASSES_ENV_VAR_NAME, 'false', @@ -72,6 +76,7 @@ def get_use_future_node_classes(): def set_use_future_node_classes(enabled): # TODO: remove in 2.0 + lona.warnings.remove_2_0() if enabled not in USE_FUTURE_NODE_CLASSES_ENV_VAR_VALUES: raise RuntimeError(f'invalid value for {USE_FUTURE_NODE_CLASSES_ENV_VAR_NAME}: {enabled}') 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..3ecfb3bb 100644 --- a/lona/html/nodes/forms/__init__.py +++ b/lona/html/nodes/forms/__init__.py @@ -1,5 +1,5 @@ from lona.events.event_types import CLICK -from lona.html.node import Node +from lona.html.node import Node, DeprecatedNode 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/view_runtime.py b/lona/view_runtime.py index f877693e..ef65012d 100644 --- a/lona/view_runtime.py +++ b/lona/view_runtime.py @@ -123,6 +123,8 @@ def __init__( # TODO: remove in 2.0 # compatibility for older Lona application code + if hasattr(self.view, 'STOP_DAEMON_WHEN_VIEW_FINISHES'): + lona.warnings.remove_2_0('STOP_DAEMON_WHEN_VIEW_FINISHES') self.stop_daemon_when_view_finishes = getattr( self.view, 'STOP_DAEMON_WHEN_VIEW_FINISHES', diff --git a/lona/warnings.py b/lona/warnings.py index 510fc026..f122a54d 100644 --- a/lona/warnings.py +++ b/lona/warnings.py @@ -1,8 +1,9 @@ -import warnings as orginal_warnings +import inspect +import warnings as original_warnings 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, + )