mirror of
https://github.com/vinta/awesome-python.git
synced 2026-05-09 22:53:49 +08:00
refactor(build): flatten extract_entries and annotate result dict
Collapse the if-seen/else-new branches so the category/group/subcategory merge logic runs once per entry unconditionally, appending to empty lists on first sight instead of duplicating the append logic in the else branch. Annotate seen and entries as dict[str, Any] so ty can resolve the mixed value types (str, list, None) in each entry dict. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
+24
-28
@@ -6,6 +6,7 @@ import re
|
|||||||
import shutil
|
import shutil
|
||||||
from datetime import UTC, datetime
|
from datetime import UTC, datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from jinja2 import Environment, FileSystemLoader
|
from jinja2 import Environment, FileSystemLoader
|
||||||
from readme_parser import ParsedGroup, ParsedSection, parse_readme, parse_sponsors
|
from readme_parser import ParsedGroup, ParsedSection, parse_readme, parse_sponsors
|
||||||
@@ -75,45 +76,40 @@ def extract_entries(
|
|||||||
Entries appearing in multiple categories are merged into a single entry
|
Entries appearing in multiple categories are merged into a single entry
|
||||||
with lists of categories and groups.
|
with lists of categories and groups.
|
||||||
"""
|
"""
|
||||||
cat_to_group: dict[str, str] = {}
|
cat_to_group = {cat["name"]: group["name"] for group in groups for cat in group["categories"]}
|
||||||
for group in groups:
|
|
||||||
for cat in group["categories"]:
|
|
||||||
cat_to_group[cat["name"]] = group["name"]
|
|
||||||
|
|
||||||
seen: dict[tuple[str, str], dict] = {} # (url, name) -> entry
|
seen: dict[tuple[str, str], dict[str, Any]] = {} # (url, name) -> entry
|
||||||
entries: list[dict] = []
|
entries: list[dict[str, Any]] = []
|
||||||
for cat in categories:
|
for cat in categories:
|
||||||
group_name = cat_to_group.get(cat["name"], "Other")
|
group_name = cat_to_group.get(cat["name"], "Other")
|
||||||
for entry in cat["entries"]:
|
for entry in cat["entries"]:
|
||||||
url = entry["url"]
|
key = (entry["url"], entry["name"])
|
||||||
key = (url, entry["name"])
|
existing: dict[str, Any] | None = seen.get(key)
|
||||||
if key in seen:
|
if existing is None:
|
||||||
existing = seen[key]
|
existing = {
|
||||||
if cat["name"] not in existing["categories"]:
|
|
||||||
existing["categories"].append(cat["name"])
|
|
||||||
if group_name not in existing["groups"]:
|
|
||||||
existing["groups"].append(group_name)
|
|
||||||
subcat = entry["subcategory"]
|
|
||||||
if subcat:
|
|
||||||
scoped = f"{cat['name']} > {subcat}"
|
|
||||||
if not any(s["value"] == scoped for s in existing["subcategories"]):
|
|
||||||
existing["subcategories"].append({"name": subcat, "value": scoped})
|
|
||||||
else:
|
|
||||||
merged = {
|
|
||||||
"name": entry["name"],
|
"name": entry["name"],
|
||||||
"url": url,
|
"url": entry["url"],
|
||||||
"description": entry["description"],
|
"description": entry["description"],
|
||||||
"categories": [cat["name"]],
|
"categories": [],
|
||||||
"groups": [group_name],
|
"groups": [],
|
||||||
"subcategories": [{"name": entry["subcategory"], "value": f"{cat['name']} > {entry['subcategory']}"}] if entry["subcategory"] else [],
|
"subcategories": [],
|
||||||
"stars": None,
|
"stars": None,
|
||||||
"owner": None,
|
"owner": None,
|
||||||
"last_commit_at": None,
|
"last_commit_at": None,
|
||||||
"source_type": detect_source_type(url),
|
"source_type": detect_source_type(entry["url"]),
|
||||||
"also_see": entry["also_see"],
|
"also_see": entry["also_see"],
|
||||||
}
|
}
|
||||||
seen[key] = merged
|
seen[key] = existing
|
||||||
entries.append(merged)
|
entries.append(existing)
|
||||||
|
if cat["name"] not in existing["categories"]:
|
||||||
|
existing["categories"].append(cat["name"])
|
||||||
|
if group_name not in existing["groups"]:
|
||||||
|
existing["groups"].append(group_name)
|
||||||
|
subcat = entry["subcategory"]
|
||||||
|
if subcat:
|
||||||
|
scoped = f"{cat['name']} > {subcat}"
|
||||||
|
if not any(s["value"] == scoped for s in existing["subcategories"]):
|
||||||
|
existing["subcategories"].append({"name": subcat, "value": scoped})
|
||||||
return entries
|
return entries
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user