Skip to content

Commit 65d0006

Browse files
committed
without browser
1 parent 1e9376d commit 65d0006

File tree

2 files changed

+46
-25
lines changed

2 files changed

+46
-25
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,15 @@ docker run -ti \
3535
```bash
3636
docker build --tag odr_core_test test/docker
3737
```
38+
39+
## Run locally
40+
41+
```bash
42+
PYTHONPATH=$(pwd)/src:$PYTHONPATH python ./src/htmlcmp/compare_output_server.py \
43+
/path/to/REFERENCE \
44+
/path/to/MONITORED \
45+
--compare \
46+
--driver firefox \
47+
--port 8000 \
48+
-vv
49+
```

src/htmlcmp/compare_output_server.py

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Config:
3131
thread_local = threading.local()
3232

3333

34-
def result_symbol(result: str):
34+
def result_symbol(result: str) -> str | None:
3535
if not isinstance(result, str):
3636
raise TypeError("Result must be of type str")
3737

@@ -41,10 +41,10 @@ def result_symbol(result: str):
4141
return "✔"
4242
if result == "different":
4343
return "❌"
44-
return "⛔"
44+
return None
4545

4646

47-
def result_css(result: str):
47+
def result_css(result: str) -> str | None:
4848
if not isinstance(result, str):
4949
raise TypeError("Result must be of type str")
5050

@@ -53,8 +53,8 @@ def result_css(result: str):
5353
if result == "same":
5454
return "color:green;"
5555
if result == "different":
56-
return "color:orange;"
57-
return "color:red;"
56+
return "color:red;"
57+
return None
5858

5959

6060
class Observer:
@@ -86,7 +86,7 @@ def dispatch(self, event):
8686
self._observer.schedule(Handler(Config.path_a), Config.path_a, recursive=True)
8787
self._observer.schedule(Handler(Config.path_b), Config.path_b, recursive=True)
8888

89-
def start(self):
89+
def start(self) -> None:
9090
logger.info("Starting watchdog observer")
9191
self._observer.start()
9292

@@ -118,22 +118,23 @@ def init_compare(a: Path, b: Path):
118118
init_compare(Config.path_a, Config.path_b)
119119
logger.info("Initial comparison submitted")
120120

121-
def stop(self):
121+
def stop(self) -> None:
122122
logger.info("Stopping watchdog observer")
123123
self._observer.stop()
124124

125-
def join(self):
125+
def join(self) -> None:
126126
logger.info("Joining watchdog observer")
127127
self._observer.join()
128128

129129

130130
class Comparator:
131131
def __init__(self, max_workers: int):
132132
def initializer():
133-
browser = getattr(Config.thread_local, "browser", None)
134-
if browser is None:
135-
browser = get_browser(driver=Config.driver)
136-
Config.thread_local.browser = browser
133+
if Config.driver is not None:
134+
browser = getattr(Config.thread_local, "browser", None)
135+
if browser is None:
136+
browser = get_browser(driver=Config.driver)
137+
Config.thread_local.browser = browser
137138

138139
logger.info(f"Creating comparator with {max_workers} workers")
139140

@@ -143,12 +144,16 @@ def initializer():
143144
self._result = {}
144145
self._future = {}
145146

146-
def submit(self, path: Path):
147+
def submit(self, path: Path) -> None:
147148
logger.debug(f"Submitting comparison for path: {path}")
148149

149150
if not isinstance(path, Path):
150151
raise TypeError("Path must be of type Path")
151152

153+
if path.suffix.lower() in [".html", ".htm"] and Config.driver is None:
154+
logger.debug(f"Skipping submission of HTML file without browser: {path}")
155+
return
156+
152157
if path in self._future:
153158
try:
154159
self._future[path].cancel()
@@ -160,7 +165,7 @@ def submit(self, path: Path):
160165
self._result[path] = "pending"
161166
self._future[path] = self._executor.submit(self.compare, path)
162167

163-
def compare(self, path: Path):
168+
def compare(self, path: Path) -> None:
164169
logger.debug(f"Comparing files for path: {path}")
165170

166171
if not isinstance(path, Path):
@@ -177,7 +182,7 @@ def compare(self, path: Path):
177182
self._result[path] = "same" if result else "different"
178183
self._future.pop(path)
179184

180-
def result(self, path: Path):
185+
def result(self, path: Path) -> str | None:
181186
logger.debug(f"Getting comparison result for path: {path}")
182187

183188
if not isinstance(path, Path):
@@ -220,9 +225,13 @@ def result(self, path: Path):
220225

221226
return functools.reduce(
222227
lambda a, b: (
223-
"pending"
224-
if "pending" in (a, b)
225-
else ("different" if "different" in (a, b) else "same")
228+
None
229+
if None in (a, b)
230+
else (
231+
"pending"
232+
if "pending" in (a, b)
233+
else "different" if "different" in (a, b) else "same"
234+
)
226235
),
227236
[self.result(path / name) for name in common]
228237
+ [
@@ -235,8 +244,8 @@ def result(self, path: Path):
235244
"same",
236245
)
237246

238-
logger.warning(f"No comparison result for path: {path}")
239-
return "unknown"
247+
logger.debug(f"No comparison result for path: {path}")
248+
return None
240249

241250

242251
app = Flask("compare")
@@ -248,7 +257,7 @@ def root():
248257

249258
current_entry_id = 0
250259

251-
def next_entry_id():
260+
def next_entry_id() -> int:
252261
nonlocal current_entry_id
253262
entry_id = current_entry_id
254263
current_entry_id += 1
@@ -594,9 +603,7 @@ def main():
594603
parser = argparse.ArgumentParser()
595604
parser.add_argument("a", type=Path, help="Path to the first directory")
596605
parser.add_argument("b", type=Path, help="Path to the second directory")
597-
parser.add_argument(
598-
"--driver", choices=["chrome", "firefox", "phantomjs"], default="firefox"
599-
)
606+
parser.add_argument("--driver", choices=["chrome", "firefox", "phantomjs"])
600607
parser.add_argument("--max-workers", type=int, default=1)
601608
parser.add_argument("--compare", action="store_true")
602609
parser.add_argument("--port", type=int, default=5000)
@@ -614,7 +621,9 @@ def main():
614621
Config.path_a = args.a
615622
Config.path_b = args.b
616623
Config.driver = args.driver
617-
Config.browser = get_browser(driver=args.driver)
624+
Config.browser = (
625+
get_browser(driver=args.driver) if args.driver is not None else None
626+
)
618627

619628
if args.compare:
620629
Config.comparator = Comparator(max_workers=args.max_workers)

0 commit comments

Comments
 (0)