diff --git a/compare50/__main__.py b/compare50/__main__.py index 15bbc46..bc6212f 100644 --- a/compare50/__main__.py +++ b/compare50/__main__.py @@ -14,6 +14,7 @@ import tempfile import requests import packaging.version +import names import attr import lib50 @@ -299,10 +300,10 @@ def expand_patterns(patterns): def check_version(package_name=__package__, timeout=5): - """Check for newer version of the package on PyPI""" + """Check for newer version of the package on PyPI""" if not __version__: return - + try: current = packaging.version.parse(__version__) latest = max(requests.get(f"https://pypi.org/pypi/{package_name}/json", timeout=timeout).json()["releases"], key=packaging.version.parse) @@ -468,9 +469,23 @@ def main(): object.__setattr__(sub, "preprocessor", preprocessor) pass_to_results[pass_] = _api.compare(scores, ignored_files, pass_) + all_subs = list(subs) + list(archive_subs) + list(ignored_subs) + + # Generate a unique first name for each submission + fictional_names = [] + used_names = set() + while len(fictional_names) < len(all_subs): + name = names.get_first_name() + if name not in used_names: + fictional_names.append(name) + used_names.add(name) + + # Map each Submission to a fictional name + sub_to_machine_name = {sub: name for sub, name in zip(all_subs, fictional_names)} + # Render results with _api.progress_bar("Rendering", disable=args.debug): - index = _renderer.render(pass_to_results, dest=args.output) + index = _renderer.render(pass_to_results, dest=args.output, sub_to_machine_name=sub_to_machine_name) termcolor.cprint( f"Done! Visit file://{index.absolute()} in a web browser to see the results.", "green") diff --git a/compare50/_renderer/_renderer.py b/compare50/_renderer/_renderer.py index 5542a94..bf760b4 100644 --- a/compare50/_renderer/_renderer.py +++ b/compare50/_renderer/_renderer.py @@ -68,6 +68,7 @@ def percentage(self): @attr.s(slots=True) class HTMLSubmission: + path = attr.ib(converter=str) name = attr.ib(converter=str) files = attr.ib() num_chars_matched = attr.ib() @@ -81,7 +82,7 @@ def percentage(self): return 0 -def render(pass_to_results, dest): +def render(pass_to_results, dest, sub_to_machine_name): bar = _api.get_progress_bar() dest = pathlib.Path(dest) @@ -102,7 +103,7 @@ def render(pass_to_results, dest): with _api.Executor() as executor: # Load static files max_id = len(results_per_sub_pair) - for id, html in executor.map(_RenderTask(dest, max_id, match_js, match_css), enumerate(results_per_sub_pair, 1)): + for id, html in executor.map(_RenderTask(dest, max_id, match_js, match_css, sub_to_machine_name), enumerate(results_per_sub_pair, 1)): with open(dest / f"match_{id}.html", "w") as f: f.write(html) bar.update() @@ -125,13 +126,13 @@ def render(pass_to_results, dest): subs = set() graph_info = {"nodes": [], "links": [], "data": {}} for i, result in enumerate(ranking_results): - graph_info["links"].append({"index":i, "source": str(result.sub_a.path), "target": str(result.sub_b.path), "value": 10 * result.score.score/max_score}) + graph_info["links"].append({"index":i, "source": sub_to_machine_name[result.sub_a], "target": sub_to_machine_name[result.sub_b], "value": 10 * result.score.score/max_score}) subs.add(result.sub_a) subs.add(result.sub_b) for sub in subs: - graph_info["nodes"].append({"id": str(sub.path)}) - graph_info["data"][str(sub.path)] = {"is_archive": sub.is_archive} + graph_info["nodes"].append({"id": sub_to_machine_name[sub]}) + graph_info["data"][sub_to_machine_name[sub]] = {"is_archive": sub.is_archive, "path": str(sub.path)} index_css = common_css + [read_file(STATIC / "index.css")] index_js = [read_file(STATIC / f) for f in ("d3.v4.min.js", "d3-scale-chromatic.v1.min.js", "d3-simple-slider.js", "index.js")] @@ -160,12 +161,13 @@ def read_file(fname): class _RenderTask: - def __init__(self, dest, max_id, js, css): + def __init__(self, dest, max_id, js, css, sub_to_machine_name): dest.mkdir(exist_ok=True) self.dest = dest self.max_id = max_id self.js = js self.css = css + self.sub_to_machine_name = sub_to_machine_name def __call__(self, arg): id, results = arg @@ -173,7 +175,7 @@ def __call__(self, arg): match_htmls = [] for result in results: - renderer = _Renderer(result.name) + renderer = _Renderer(result.name, self.sub_to_machine_name) score = result.score groups = result.groups ignored_spans = result.ignored_spans @@ -236,8 +238,9 @@ def _prepare_dest(dest): class _Renderer: - def __init__(self, name): + def __init__(self, name, sub_to_machine_name): self.name = name + self.sub_to_machine_name = sub_to_machine_name self._frag_id_counter = -1 self._span_id_store = IdStore() self._group_id_store = IdStore() @@ -285,7 +288,7 @@ def html_submission(self, submission, file_to_spans, ignored_spans): html_files = self.html_files(submission, file_to_spans, ignored_spans) num_chars_matched = sum(f.num_chars_matched for f in html_files) num_chars = sum(f.num_chars for f in html_files) - return HTMLSubmission(submission.path, html_files, num_chars_matched, num_chars) + return HTMLSubmission(submission.path, self.sub_to_machine_name[submission], html_files, num_chars_matched, num_chars) def data(self, result, html_fragments, ignored_spans): fragment_to_spans = {} diff --git a/compare50/_renderer/static/index.js b/compare50/_renderer/static/index.js index edcf78a..a27f437 100644 --- a/compare50/_renderer/static/index.js +++ b/compare50/_renderer/static/index.js @@ -361,7 +361,11 @@ function update_index() { new_trs.append("td") .attr("class", "sub_name") .datum(d => d[field]) - .html(d => GRAPH.data[d.id].is_archive ? `${ARCHIVE_IMG} ${d.id}` : d.id); + .html(d => { + const fic_name = GRAPH.data[d.id].is_archive ? `${ARCHIVE_IMG} ${d.id}` : d.id; + const path = GRAPH.data[d.id].path || ''; + return `${fic_name}`; + }); } new_trs.append("td") diff --git a/compare50/_renderer/templates/match.html b/compare50/_renderer/templates/match.html index 4700021..d9c523f 100644 --- a/compare50/_renderer/templates/match.html +++ b/compare50/_renderer/templates/match.html @@ -4,7 +4,7 @@