Skip to content

Commit 80b7fba

Browse files
committed
use pathlib
1 parent bc51878 commit 80b7fba

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

src/htmlcmp/html_render_diff.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
import os
54
import sys
65
import argparse
76
import io
87
import time
8+
from pathlib import Path
99

1010
from PIL import Image, ImageChops
1111
from selenium import webdriver
1212
from selenium.webdriver.common.by import By
1313
from selenium.webdriver.support import expected_conditions
1414
from selenium.webdriver.support.ui import WebDriverWait
15-
import pathlib
1615

1716

18-
def to_url(something):
19-
if os.path.isfile(something):
20-
return pathlib.Path(os.path.abspath(something)).as_uri()
21-
return something
17+
def to_url(path):
18+
if path.is_file():
19+
return path.as_uri()
20+
return path
2221

2322

2423
def screenshot(browser, url):
@@ -77,8 +76,8 @@ def html_render_diff(a, b, browser):
7776

7877
def main():
7978
parser = argparse.ArgumentParser()
80-
parser.add_argument("a")
81-
parser.add_argument("b")
79+
parser.add_argument("a", type=Path)
80+
parser.add_argument("b", type=Path)
8281
parser.add_argument(
8382
"--driver", choices=["chrome", "firefox", "phantomjs"], default="firefox"
8483
)

src/htmlcmp/tidy_output.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
import os
54
import sys
65
import argparse
76
import json
87
import subprocess
98
import shlex
9+
from pathlib import Path
1010

1111
from 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

4343
def 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

9093
def 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

Comments
 (0)