15
15
import click
16
16
17
17
from idom import html
18
- from idom ._console .utils import echo_error , echo_warning
19
18
20
19
21
20
CAMEL_CASE_SUB_PATTERN = re .compile (r"(?<!^)(?=[A-Z])" )
22
21
23
22
24
23
@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 ))
30
25
def update_html_usages (paths : list [str ]) -> None :
31
26
"""Rewrite files under the given paths using the new html element API.
32
27
@@ -61,26 +56,19 @@ def update_html_usages(paths: list[str]) -> None:
61
56
62
57
at_least_one_file = False
63
58
for p in map (Path , paths ):
64
- if not p .exists ():
65
- echo_warning (f"no directory { p } " )
66
- continue
67
59
for f in [p ] if p .is_file () else p .rglob ("*.py" ):
68
60
at_least_one_file = True
69
61
result = generate_rewrite (file = f , source = f .read_text ())
70
62
if result is not None :
71
63
f .write_text (result )
72
64
73
- if not at_least_one_file :
74
- echo_error ("Found no Python files in the given directories." )
75
- sys .exit (1 )
76
-
77
65
78
66
def generate_rewrite (file : Path , source : str ) -> str | None :
79
67
tree = ast .parse (source )
80
68
81
69
changed : list [Sequence [ast .AST ]] = []
82
70
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 ):
84
72
continue
85
73
86
74
func = node .func
@@ -91,28 +79,30 @@ def generate_rewrite(file: Path, source: str) -> str | None:
91
79
else :
92
80
continue
93
81
94
- if not (hasattr (html , name ) or name == "vdom" ):
95
- continue
96
-
97
82
if name == "vdom" :
83
+ if len (node .args ) < 2 :
84
+ continue
98
85
maybe_attr_dict_node = node .args [1 ]
99
86
# remove attr dict from new args
100
87
new_args = node .args [:1 ] + node .args [2 :]
101
- else :
88
+ elif hasattr (html , name ):
89
+ if len (node .args ) == 0 :
90
+ continue
102
91
maybe_attr_dict_node = node .args [0 ]
103
92
# remove attr dict from new args
104
93
new_args = node .args [1 :]
94
+ else :
95
+ continue
105
96
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 )
113
103
114
- node .args = new_args
115
- changed .append ((node , * parents ))
104
+ node .args = new_args
105
+ changed .append ((node , * parents ))
116
106
117
107
if not changed :
118
108
return None
0 commit comments