1515import click
1616
1717from idom import html
18- from idom ._console .utils import echo_error , echo_warning
1918
2019
2120CAMEL_CASE_SUB_PATTERN = re .compile (r"(?<!^)(?=[A-Z])" )
2221
2322
2423@click .command ()
25- @click .argument (
26- "directories" ,
27- nargs = - 1 ,
28- type = click .Path (exists = True , dir_okay = True , file_okay = False ),
29- )
24+ @click .argument ("paths" , nargs = - 1 , type = click .Path (exists = True ))
3025def update_html_usages (paths : list [str ]) -> None :
3126 """Rewrite files under the given paths using the new html element API.
3227
@@ -61,26 +56,19 @@ def update_html_usages(paths: list[str]) -> None:
6156
6257 at_least_one_file = False
6358 for p in map (Path , paths ):
64- if not p .exists ():
65- echo_warning (f"no directory { p } " )
66- continue
6759 for f in [p ] if p .is_file () else p .rglob ("*.py" ):
6860 at_least_one_file = True
6961 result = generate_rewrite (file = f , source = f .read_text ())
7062 if result is not None :
7163 f .write_text (result )
7264
73- if not at_least_one_file :
74- echo_error ("Found no Python files in the given directories." )
75- sys .exit (1 )
76-
7765
7866def generate_rewrite (file : Path , source : str ) -> str | None :
7967 tree = ast .parse (source )
8068
8169 changed : list [Sequence [ast .AST ]] = []
8270 for parents , node in walk_with_parent (tree ):
83- if not ( isinstance (node , ast .Call ) and node . args ):
71+ if not isinstance (node , ast .Call ):
8472 continue
8573
8674 func = node .func
@@ -91,28 +79,30 @@ def generate_rewrite(file: Path, source: str) -> str | None:
9179 else :
9280 continue
9381
94- if not (hasattr (html , name ) or name == "vdom" ):
95- continue
96-
9782 if name == "vdom" :
83+ if len (node .args ) < 2 :
84+ continue
9885 maybe_attr_dict_node = node .args [1 ]
9986 # remove attr dict from new args
10087 new_args = node .args [:1 ] + node .args [2 :]
101- else :
88+ elif hasattr (html , name ):
89+ if len (node .args ) == 0 :
90+ continue
10291 maybe_attr_dict_node = node .args [0 ]
10392 # remove attr dict from new args
10493 new_args = node .args [1 :]
94+ else :
95+ continue
10596
106- if node .args :
107- new_keyword_info = extract_keywords (maybe_attr_dict_node )
108- if new_keyword_info is not None :
109- if new_keyword_info .replace :
110- node .keywords = new_keyword_info .keywords
111- else :
112- node .keywords .extend (new_keyword_info .keywords )
97+ new_keyword_info = extract_keywords (maybe_attr_dict_node )
98+ if new_keyword_info is not None :
99+ if new_keyword_info .replace :
100+ node .keywords = new_keyword_info .keywords
101+ else :
102+ node .keywords .extend (new_keyword_info .keywords )
113103
114- node .args = new_args
115- changed .append ((node , * parents ))
104+ node .args = new_args
105+ changed .append ((node , * parents ))
116106
117107 if not changed :
118108 return None
0 commit comments