Skip to content

Commit

Permalink
misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorshea committed Jan 26, 2023
1 parent 598bcf7 commit 79bd0ff
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion requirements/pkg-deps.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ requests >=2
colorlog >=6
asgiref >=3
lxml >=4
typer >=8, <9
click >=8, <9
12 changes: 6 additions & 6 deletions src/idom/_console/update_html_usages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 18 additions & 5 deletions src/idom/core/vdom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
from typing import Any, Mapping, cast
from warnings import warn

from fastjsonschema import compile as compile_json_schema

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test__console/test_update_html_usages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 79bd0ff

Please sign in to comment.