From 1978d371eec8a8d0faf6e7b687a9f634da4b085d Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 14:11:03 +0100 Subject: [PATCH] refactor: index path tails through one helper --- scripts/collection/indexes.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/scripts/collection/indexes.py b/scripts/collection/indexes.py index d6ac08e74..640efa9bf 100644 --- a/scripts/collection/indexes.py +++ b/scripts/collection/indexes.py @@ -27,6 +27,18 @@ def _path_suffixes(rel_path: str) -> list[str]: return ["/".join(parts[-depth:]) for depth in range(len(parts), 1, -1)] +def _index_suffixes( + by_path_suffix: dict[str, list[str]], path: str, name: str, sha1: str +) -> None: + """File a content under every tail of one of its paths but its bare name.""" + for suffix in _path_suffixes(path): + if suffix == name: + continue + held = by_path_suffix.setdefault(suffix, []) + if sha1 not in held: + held.append(sha1) + + def build_indexes(files: dict, aliases: dict) -> dict: """Build secondary indexes for fast lookup.""" by_md5: dict[str, str] = {} @@ -47,13 +59,7 @@ def build_indexes(files: dict, aliases: dict) -> dict: by_sha256[entry["sha256"]] = sha1 # Path suffix index for regional variant resolution - for suffix in _path_suffixes(entry["path"]): - if suffix == name: - continue - if suffix not in by_path_suffix: - by_path_suffix[suffix] = [] - if sha1 not in by_path_suffix[suffix]: - by_path_suffix[suffix].append(sha1) + _index_suffixes(by_path_suffix, entry["path"], name, sha1) # Add alias names to by_name index (aliases have different filenames for same SHA1) for sha1, alias_list in aliases.items(): @@ -64,13 +70,7 @@ def build_indexes(files: dict, aliases: dict) -> dict: if sha1 not in by_name[name]: by_name[name].append(sha1) # Also index alias paths in by_path_suffix - for suffix in _path_suffixes(alias["path"]): - if suffix == name: - continue - if suffix not in by_path_suffix: - by_path_suffix[suffix] = [] - if sha1 not in by_path_suffix[suffix]: - by_path_suffix[suffix].append(sha1) + _index_suffixes(by_path_suffix, alias["path"], name, sha1) return { "by_md5": by_md5,