Skip to content

Commit

Permalink
views: issue warning on use of .daemonize
Browse files Browse the repository at this point in the history
add deprecation warning
add intermediary Lona_2_0_DeprecationWarning so only
one warnings needs to be set for filtering.
update example code
  • Loading branch information
AvdN committed Jul 29, 2023
1 parent 7f8ba4c commit 0d1a6b1
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 15 deletions.
2 changes: 1 addition & 1 deletion doc/content/demos/daemonized-view/daemonized-view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions lona/compat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import lona.warnings

logger = logging.getLogger('lona')

Expand All @@ -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:
Expand All @@ -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}')

Expand All @@ -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',
Expand Down Expand Up @@ -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}')

Expand Down
8 changes: 8 additions & 0 deletions lona/html/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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)
6 changes: 3 additions & 3 deletions lona/html/nodes/forms/__init__.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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'
Expand All @@ -35,7 +35,7 @@ class DataList(Node):
TAG_NAME = 'datalist'


class Fieldset(Node):
class Fieldset(DeprecatedNode):
# TODO: remove in 2.0

TAG_NAME = 'fieldset'
Expand Down
6 changes: 3 additions & 3 deletions lona/html/nodes/forms/select.py
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 2 additions & 0 deletions lona/html/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions lona/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions lona/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']:
Expand Down
2 changes: 2 additions & 0 deletions lona/view_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
39 changes: 31 additions & 8 deletions lona/warnings.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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):
Expand All @@ -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,
)

0 comments on commit 0d1a6b1

Please sign in to comment.