1010from htmlcmp .common import bcolors
1111
1212
13- def tidy_json (path : Path ) -> int :
13+ def tidy_json (path : Path , verbose : bool = False ) -> int :
1414 if not isinstance (path , Path ):
1515 raise TypeError ("path must be a Path object" )
1616 if not path .is_file ():
@@ -21,10 +21,11 @@ def tidy_json(path: Path) -> int:
2121 json .load (f )
2222 return 0
2323 except ValueError :
24+ print (f"{ bcolors .FAIL } Error: { path } is not a valid JSON file{ bcolors .ENDC } " )
2425 return 1
2526
2627
27- def tidy_html (path : Path , html_tidy_config : Path = None ) -> int :
28+ def tidy_html (path : Path , html_tidy_config : Path = None , verbose : bool = False ) -> int :
2829 if not isinstance (path , Path ):
2930 raise TypeError ("path must be a Path object" )
3031 if not path .is_file ():
@@ -41,23 +42,32 @@ def tidy_html(path: Path, html_tidy_config: Path = None) -> int:
4142 result = subprocess .run (
4243 cmd , stdout = subprocess .PIPE , stderr = subprocess .STDOUT , text = True
4344 )
45+ if result .stdout :
46+ if verbose and result .returncode == 0 :
47+ print (result .stdout )
48+ elif verbose and result .returncode == 1 :
49+ print (f"{ bcolors .WARNING } Warning: { path } has warnings{ bcolors .ENDC } " )
50+ print (f"{ bcolors .WARNING } { result .stdout } { bcolors .ENDC } " )
51+ elif verbose or result .returncode > 1 :
52+ print (f"{ bcolors .FAIL } Error: { path } has errors{ bcolors .ENDC } " )
53+ print (f"{ bcolors .FAIL } { result .stdout } { bcolors .ENDC } " )
4454 if result .returncode == 1 :
4555 return 1
4656 if result .returncode > 1 :
4757 return 2
4858 return 0
4959
5060
51- def tidy_file (path : Path , html_tidy_config : Path = None ) -> int :
61+ def tidy_file (path : Path , html_tidy_config : Path = None , verbose : bool = False ) -> int :
5262 if not isinstance (path , Path ):
5363 raise TypeError ("path must be a Path object" )
5464 if not path .is_file ():
5565 raise FileNotFoundError (f"{ path } is not a file" )
5666
5767 if path .suffix == ".json" :
58- return tidy_json (path )
68+ return tidy_json (path , verbose = verbose )
5969 elif path .suffix == ".html" :
60- return tidy_html (path , html_tidy_config = html_tidy_config )
70+ return tidy_html (path , html_tidy_config = html_tidy_config , verbose = verbose )
6171
6272
6373def tidyable_file (path : Path ) -> bool :
@@ -74,7 +84,11 @@ def tidyable_file(path: Path) -> bool:
7484
7585
7686def tidy_dir (
77- path : Path , level : int = 0 , prefix : str = "" , html_tidy_config : Path = None
87+ path : Path ,
88+ level : int = 0 ,
89+ prefix : str = "" ,
90+ html_tidy_config : Path = None ,
91+ verbose : bool = False ,
7892) -> dict [str , list [Path ]]:
7993 if not isinstance (path , Path ):
8094 raise TypeError ("path must be a Path object" )
@@ -104,7 +118,7 @@ def tidy_dir(
104118
105119 for filename in [path .name for path in files ]:
106120 filepath = path / filename
107- tidy = tidy_file (filepath , html_tidy_config = html_tidy_config )
121+ tidy = tidy_file (filepath , html_tidy_config = html_tidy_config , verbose = verbose )
108122 if tidy == 0 :
109123 print (f"{ prefix_file } { bcolors .OKGREEN } { filename } ✓{ bcolors .ENDC } " )
110124 elif tidy == 1 :
@@ -121,6 +135,7 @@ def tidy_dir(
121135 level = level + 1 ,
122136 prefix = prefix + "│ " ,
123137 html_tidy_config = html_tidy_config ,
138+ verbose = verbose ,
124139 )
125140 result ["warning" ].extend (subresult ["warning" ])
126141 result ["error" ].extend (subresult ["error" ])
@@ -134,9 +149,16 @@ def main():
134149 parser .add_argument (
135150 "--html-tidy-config" , type = Path , help = "Path to tidy config file"
136151 )
152+ parser .add_argument (
153+ "--verbose" ,
154+ action = "store_true" ,
155+ help = "Print verbose output (warnings and errors)" ,
156+ )
137157 args = parser .parse_args ()
138158
139- result = tidy_dir (args .path , html_tidy_config = args .html_tidy_config )
159+ result = tidy_dir (
160+ args .path , html_tidy_config = args .html_tidy_config , verbose = args .verbose
161+ )
140162 if result ["error" ]:
141163 return 1
142164
0 commit comments