From 79bd0ff1a5dccfd961d90907bd1e1520c8c7491d Mon Sep 17 00:00:00 2001 From: rmorshea Date: Wed, 25 Jan 2023 19:09:09 -0800 Subject: [PATCH] misc fixes --- requirements/pkg-deps.txt | 2 +- src/idom/_console/update_html_usages.py | 12 +++++----- src/idom/core/vdom.py | 23 +++++++++++++++---- .../test__console/test_update_html_usages.py | 2 +- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/requirements/pkg-deps.txt b/requirements/pkg-deps.txt index 398057266..061839473 100644 --- a/requirements/pkg-deps.txt +++ b/requirements/pkg-deps.txt @@ -7,4 +7,4 @@ requests >=2 colorlog >=6 asgiref >=3 lxml >=4 -typer >=8, <9 +click >=8, <9 diff --git a/src/idom/_console/update_html_usages.py b/src/idom/_console/update_html_usages.py index 34402068d..4d66837f7 100644 --- a/src/idom/_console/update_html_usages.py +++ b/src/idom/_console/update_html_usages.py @@ -3,7 +3,7 @@ import ast import re from collections.abc import Sequence -from glob import iglob +from glob import glob from keyword import kwlist from pathlib import Path from textwrap import indent @@ -20,9 +20,9 @@ @click.command() -@click.argument("patterns", nargs=-1) -def update_html_usages(patterns: list[str]) -> None: - """Rewrite files matching the given glob patterns to the new html element API. +@click.argument("directories", nargs=-1) +def update_html_usages(directories: list[str]) -> None: + """Rewrite files in the given directories to use the new html element API. The old API required users to pass a dictionary of attributes to html element constructor functions. For example: @@ -50,8 +50,8 @@ def update_html_usages(patterns: list[str]) -> None: just above its changes. As such it requires manual intervention to put those comments back in their original location. """ - for pat in patterns: - for file in map(Path, iglob(pat)): + for d in directories: + for file in Path(d).rglob("*.py"): result = generate_rewrite(file=file, source=file.read_text()) if result is not None: file.write_text(result) diff --git a/src/idom/core/vdom.py b/src/idom/core/vdom.py index eb10da890..40340367b 100644 --- a/src/idom/core/vdom.py +++ b/src/idom/core/vdom.py @@ -2,6 +2,7 @@ import logging from typing import Any, Mapping, cast +from warnings import warn from fastjsonschema import compile as compile_json_schema @@ -153,20 +154,32 @@ def vdom( """ model: VdomDict = {"tagName": tag} - children: list[VdomChild] = [] + flattened_children: list[VdomChild] = [] for child in children: + if isinstance(child, dict) and "tagName" not in child: + warn( + ( + "Element constructor signatures have changed! A CLI tool for " + "automatically updating code to the latest API has been provided " + "with this release of IDOM (e.g. 'idom update-html-usages'). For " + "start a discussion if you need help transitioning to this new " + "interface: https://github.com/idom-team/idom/discussions/new?category=question" + ), + DeprecationWarning, + ) + attributes.update(child) if _is_single_child(child): - children.append(child) + flattened_children.append(child) else: - children.extend(child) + flattened_children.extend(child) attributes, event_handlers = separate_attributes_and_event_handlers(attributes) if attributes: model["attributes"] = attributes - if children: - model["children"] = children + if flattened_children: + model["children"] = flattened_children if event_handlers: model["eventHandlers"] = event_handlers diff --git a/tests/test__console/test_update_html_usages.py b/tests/test__console/test_update_html_usages.py index 0ee6686c0..58c79c958 100644 --- a/tests/test__console/test_update_html_usages.py +++ b/tests/test__console/test_update_html_usages.py @@ -13,7 +13,7 @@ def test_update_html_usages(tmp_path): tempfile: Path = tmp_path / "temp.py" tempfile.write_text("html.div({'className': test})") - result = runner.invoke(update_html_usages, str(tempfile)) + result = runner.invoke(update_html_usages, str(tmp_path)) if result.exception: raise result.exception