1010from htmlcmp .common import bcolors
1111
1212
13- def tidy_json (path ):
13+ def tidy_json (path : Path ) -> int :
14+ if not isinstance (path , Path ):
15+ raise TypeError ("path must be a Path object" )
16+ if not path .is_file ():
17+ raise FileNotFoundError (f"{ path } is not a file" )
18+
1419 try :
1520 with open (path , "r" ) as f :
1621 json .load (f )
@@ -19,7 +24,16 @@ def tidy_json(path):
1924 return 1
2025
2126
22- def tidy_html (path , html_tidy_config = None ):
27+ def tidy_html (path : Path , html_tidy_config : Path = None ) -> int :
28+ if not isinstance (path , Path ):
29+ raise TypeError ("path must be a Path object" )
30+ if not path .is_file ():
31+ raise FileNotFoundError (f"{ path } is not a file" )
32+ if html_tidy_config is not None and not isinstance (html_tidy_config , Path ):
33+ raise TypeError ("html_tidy_config must be a Path object or None" )
34+ if html_tidy_config is not None and not html_tidy_config .is_file ():
35+ raise FileNotFoundError (f"{ html_tidy_config } is not a file" )
36+
2337 cmd = ["tidy" ]
2438 if html_tidy_config :
2539 cmd .extend (["-config" , str (html_tidy_config .resolve ())])
@@ -34,22 +48,47 @@ def tidy_html(path, html_tidy_config=None):
3448 return 0
3549
3650
37- def tidy_file (path , html_tidy_config = None ):
51+ def tidy_file (path : Path , html_tidy_config : Path = None ) -> int :
52+ if not isinstance (path , Path ):
53+ raise TypeError ("path must be a Path object" )
54+ if not path .is_file ():
55+ raise FileNotFoundError (f"{ path } is not a file" )
56+
3857 if path .suffix == ".json" :
3958 return tidy_json (path )
4059 elif path .suffix == ".html" :
4160 return tidy_html (path , html_tidy_config = html_tidy_config )
4261
4362
44- def tidyable_file (path ):
63+ def tidyable_file (path : Path ) -> bool :
64+ if not isinstance (path , Path ):
65+ raise TypeError ("path must be a Path object" )
66+ if not path .is_file ():
67+ raise FileNotFoundError (f"{ path } is not a file" )
68+
4569 if path .suffix == ".json" :
4670 return True
4771 if path .suffix == ".html" :
4872 return True
4973 return False
5074
5175
52- def tidy_dir (path , level = 0 , prefix = "" , html_tidy_config = None ):
76+ def tidy_dir (
77+ path : Path , level : int = 0 , prefix : str = "" , html_tidy_config : Path = None
78+ ) -> dict [str , list [Path ]]:
79+ if not isinstance (path , Path ):
80+ raise TypeError ("path must be a Path object" )
81+ if not path .is_dir ():
82+ raise NotADirectoryError (f"{ path } is not a directory" )
83+ if not isinstance (level , int ) or level < 0 :
84+ raise ValueError ("level must be a non-negative integer" )
85+ if not isinstance (prefix , str ):
86+ raise TypeError ("prefix must be a string" )
87+ if html_tidy_config is not None and not isinstance (html_tidy_config , Path ):
88+ raise TypeError ("html_tidy_config must be a Path object or None" )
89+ if html_tidy_config is not None and not html_tidy_config .is_file ():
90+ raise FileNotFoundError (f"{ html_tidy_config } is not a file" )
91+
5392 prefix_file = prefix + "├── "
5493 if level == 0 :
5594 print (f"tidy dir { path } " )
0 commit comments