Skip to content

Commit 9f8b90c

Browse files
committed
warn on dir does not exist
1 parent 4a923f3 commit 9f8b90c

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

src/idom/_console/update_html_usages.py

+19-12
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@
1515
import click
1616

1717
from idom import html
18-
from idom._console.utils import error
18+
from idom._console.utils import echo_error, echo_warning
1919

2020

2121
CAMEL_CASE_SUB_PATTERN = re.compile(r"(?<!^)(?=[A-Z])")
2222

2323

2424
@click.command()
25-
@click.argument("directories", nargs=-1)
26-
def update_html_usages(directories: list[str]) -> None:
27-
"""Rewrite files in the given directories to use the new html element API.
25+
@click.argument(
26+
"directories",
27+
nargs=-1,
28+
type=click.Path(exists=True, dir_okay=True, file_okay=False),
29+
)
30+
def update_html_usages(paths: list[str]) -> None:
31+
"""Rewrite files under the given paths using the new html element API.
2832
2933
The old API required users to pass a dictionary of attributes to html element
3034
constructor functions. For example:
@@ -55,16 +59,19 @@ def update_html_usages(directories: list[str]) -> None:
5559
if sys.version_info < (3, 9): # pragma: no cover
5660
raise RuntimeError("This command requires Python>=3.9")
5761

58-
at_leat_one_file = False
59-
for d in directories:
60-
for file in Path(d).rglob("*.py"):
61-
at_leat_one_file = True
62-
result = generate_rewrite(file=file, source=file.read_text())
62+
at_least_one_file = False
63+
for p in map(Path, paths):
64+
if not p.exists():
65+
echo_warning(f"no directory {p}")
66+
continue
67+
for f in [p] if p.is_file() else p.rglob("*.py"):
68+
at_least_one_file = True
69+
result = generate_rewrite(file=f, source=f.read_text())
6370
if result is not None:
64-
file.write_text(result)
71+
f.write_text(result)
6572

66-
if not at_leat_one_file:
67-
error("Found no Python files in the given directories.")
73+
if not at_least_one_file:
74+
echo_error("Found no Python files in the given directories.")
6875
sys.exit(1)
6976

7077

src/idom/_console/utils.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import click
22

33

4-
def error(text: str) -> None:
4+
def echo_error(text: str) -> None:
55
click.echo(f"{click.style('Error', fg='red')}: {text}")
6+
7+
8+
def echo_warning(text: str) -> None:
9+
click.echo(f"{click.style('Warning', fg='yellow')}: {text}")

tests/test__console/test_update_html_usages.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,25 @@ def test_update_html_usages(tmp_path):
1717

1818
tempfile: Path = tmp_path / "temp.py"
1919
tempfile.write_text("html.div({'className': test})")
20-
21-
result = runner.invoke(update_html_usages, str(tmp_path))
22-
23-
if result.exception:
24-
raise result.exception
20+
result = runner.invoke(
21+
update_html_usages,
22+
args=[str(tmp_path)],
23+
catch_exceptions=False,
24+
)
2525

2626
assert result.exit_code == 0
2727
assert tempfile.read_text() == "html.div(class_name=test)"
2828

2929

30-
def test_update_html_usages_no_files(tmp_path):
30+
def test_update_html_usages_no_files():
3131
runner = CliRunner()
3232

33-
result = runner.invoke(update_html_usages, "directory-does-no-exist")
33+
result = runner.invoke(
34+
update_html_usages,
35+
args=["directory-does-no-exist"],
36+
catch_exceptions=False,
37+
)
38+
3439
assert result.exit_code == 1
3540
assert "Found no Python files" in result.stdout
3641

0 commit comments

Comments
 (0)