11#!/usr/bin/env python3
22# -*- coding: utf-8 -*-
33
4- import os
54import sys
65import argparse
76import json
87import subprocess
98import shlex
9+ from pathlib import Path
1010
1111from htmlcmp .common import bcolors
1212
@@ -20,11 +20,11 @@ def tidy_json(path):
2020 return 1
2121
2222
23- def tidy_html (path ):
24- config_path = os . path . join (
25- os . path . dirname ( os . path . realpath ( __file__ )), ".html-tidy"
26- )
27- cmd = shlex .split (f'tidy -config " { config_path } " -q "{ path } "' )
23+ def tidy_html (path , tidy_config = None ):
24+ if tidy_config :
25+ cmd = shlex . split ( f'tidy -config " { tidy_config . resove () } " -q " { path } "' )
26+ else :
27+ cmd = shlex .split (f'tidy -q "{ path } "' )
2828 result = subprocess .run (cmd , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
2929 if result .returncode == 1 :
3030 return 1
@@ -33,22 +33,22 @@ def tidy_html(path):
3333 return 0
3434
3535
36- def tidy_file (path ):
37- if path .endswith ( ".json" ) :
36+ def tidy_file (path , tidy_config = None ):
37+ if path .suffix == ".json" :
3838 return tidy_json (path )
39- elif path .endswith ( ".html" ) :
40- return tidy_html (path )
39+ elif path .suffix == ".html" :
40+ return tidy_html (path , tidy_config = tidy_config )
4141
4242
4343def tidyable_file (path ):
44- if path .endswith ( ".json" ) :
44+ if path .suffix == ".json" :
4545 return True
46- if path .endswith ( ".html" ) :
46+ if path .suffix == ".html" :
4747 return True
4848 return False
4949
5050
51- def tidy_dir (path , level = 0 , prefix = "" ):
51+ def tidy_dir (path , level = 0 , prefix = "" , tidy_config = None ):
5252 prefix_file = prefix + "├── "
5353 if level == 0 :
5454 print (f"tidy dir { path } " )
@@ -58,15 +58,15 @@ def tidy_dir(path, level=0, prefix=""):
5858 "error" : [],
5959 }
6060
61- items = [os . path . join ( path , name ) for name in os . listdir ( path )]
61+ items = [path / name for name in path . iterdir ( )]
6262 files = sorted (
63- [path for path in items if os . path .isfile ( path ) and tidyable_file (path )]
63+ [path for path in items if path .is_file ( ) and tidyable_file (path )]
6464 )
65- dirs = sorted ([path for path in items if os . path .isdir ( path )])
65+ dirs = sorted ([path for path in items if path .is_dir ( )])
6666
67- for filename in [os . path .basename ( path ) for path in files ]:
68- filepath = os . path . join ( path , filename )
69- tidy = tidy_file (filepath )
67+ for filename in [path .name for path in files ]:
68+ filepath = path / filename
69+ tidy = tidy_file (filepath , tidy_config = tidy_config )
7070 if tidy == 0 :
7171 print (f"{ prefix_file } { bcolors .OKGREEN } { filename } ✓{ bcolors .ENDC } " )
7272 elif tidy == 1 :
@@ -76,10 +76,13 @@ def tidy_dir(path, level=0, prefix=""):
7676 print (f"{ prefix_file } { bcolors .FAIL } { filename } ✘{ bcolors .ENDC } " )
7777 result ["error" ].append (filepath )
7878
79- for dirname in [os . path .basename ( path ) for path in dirs ]:
79+ for dirname in [path .name for path in dirs ]:
8080 print (prefix + "├── " + dirname )
8181 subresult = tidy_dir (
82- os .path .join (path , dirname ), level = level + 1 , prefix = prefix + "│ "
82+ path / dirname ,
83+ level = level + 1 ,
84+ prefix = prefix + "│ " ,
85+ tidy_config = tidy_config ,
8386 )
8487 result ["warning" ].extend (subresult ["warning" ])
8588 result ["error" ].extend (subresult ["error" ])
@@ -89,10 +92,11 @@ def tidy_dir(path, level=0, prefix=""):
8992
9093def main ():
9194 parser = argparse .ArgumentParser ()
92- parser .add_argument ("a" )
95+ parser .add_argument ("path" , type = Path , help = "Path to directory to tidy" )
96+ parser .add_argument ("--tidy-config" , type = Path , help = "Path to tidy config file" )
9397 args = parser .parse_args ()
9498
95- result = tidy_dir (args .a )
99+ result = tidy_dir (args .path , tidy_config = args . tidy_config )
96100 if result ["error" ]:
97101 return 1
98102
0 commit comments