@@ -16,34 +16,37 @@ def cli(*, version: bool) -> None:
16
16
17
17
@cli .command ()
18
18
@click .argument ("sources" , type = click .Path (exists = True ), nargs = - 1 )
19
- @click .option ("-r" , "--recursive" , type = bool , default = True , help = "Recurse into directories." )
19
+ @click .option ("-r" , "--recursive" , type = bool , is_flag = True , default = False , help = "Recurse into directories." )
20
20
def compile (sources : list [str ], recursive : bool ) -> None :
21
- """Compile a PX file to a PY file."""
21
+ """Compile .px files to regular .py files."""
22
+ count = 0
22
23
for source in sources :
23
24
path = Path (source )
24
- if path .is_dir ():
25
- transpile_dir (path , recursive = recursive )
26
- elif path .suffix == ".px" :
27
- transpile_file (path )
28
- else :
29
- click .echo (f"Skipping { source } (not a PX file)" )
30
- click .echo ("Compilation complete." )
25
+ count += transpile_dir (path , recursive = recursive )
26
+ msg = f"Compiled { count } file" + ("s" if count != 1 else "" ) + "."
27
+ click .secho (msg , fg = "green" , bold = True )
31
28
32
29
33
- def transpile_dir (path : Path , * , recursive : bool = False ) -> None :
34
- assert path .is_dir ()
30
+ def transpile_dir (path : Path , * , recursive : bool = False ) -> int :
31
+ if path .is_file ():
32
+ return transpile_file (path )
33
+ count = 0
35
34
for file in path .iterdir ():
36
35
if file .is_dir () and recursive :
37
- transpile_dir (file )
38
- elif file .suffix == ".px" :
39
- transpile_file (file )
36
+ count += transpile_dir (file )
37
+ elif file .is_file () and file .suffix == ".px" :
38
+ count += transpile_file (file )
39
+ return count
40
40
41
41
42
- def transpile_file (path : Path ) -> None :
43
- """Transpile a PX file to a PY file."""
44
- assert path .suffix == ".px"
42
+ def transpile_file (path : Path ) -> int :
43
+ if path .suffix != ".px" :
44
+ click .secho (f"Skipping { path } (not a .px file)" , fg = "yellow" )
45
+ return 0
46
+ click .echo (f"Compiling { path } ..." )
45
47
transpiled = transpile (path .read_text ())
46
48
path .with_suffix (".py" ).write_text (transpiled )
49
+ return 1
47
50
48
51
49
52
if __name__ == "__main__" :
0 commit comments