forked from sphinx-themes/sphinx-themes.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort-json.py
94 lines (72 loc) · 2.07 KB
/
sort-json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import json
import re
import sys
from pathlib import Path
# Only edit this if you're @pradyunsg or @shirou.
_FEATURED = [
"alabaster",
"sphinx-rtd-theme",
"furo",
"sphinx-book-theme",
"pydata-sphinx-theme",
"sphinx-press-theme",
"piccolo-theme",
"insegel",
"insipid-sphinx-theme",
"sphinx-material",
]
_canonicalize_regex = re.compile(r"[-_.]+")
def canonicalize_name(name):
return _canonicalize_regex.sub("-", name).lower()
def _get_index_or_default(li, value, *, default):
try:
return li.index(value)
except ValueError:
return default
def sort_key(theme):
"""Sort themes as "featured", "general", built-in."""
# Normalize names
theme["pypi"] = canonicalize_name(theme["pypi"])
if theme["pypi"] == "sphinx":
rank = _get_index_or_default(
_FEATURED,
theme["config"],
default=len(_FEATURED) + 1,
)
else:
rank = _get_index_or_default(
_FEATURED,
theme["pypi"],
default=len(_FEATURED),
)
return (rank, theme["display"])
def validate_display_names(data):
bad = set()
for theme in data["themes"]:
if theme["pypi"] == "sphinx":
continue
name = theme["display"].lower()
if "sphinx" in name or "theme" in name:
bad.add(f"display: {theme['display']}, pypi: {theme['pypi']}")
if bad:
bad_names = "\n".join(sorted(bad))
print(
f"FATAL: Got improper `display` values for packages:\n{bad_names}",
file=sys.stderr,
)
print(
"Don't include the words 'sphinx' and 'theme' in the display. "
'It is in a "Sphinx Themes Gallery" already.'
)
sys.exit(1)
def main():
path = Path("themes.json")
with path.open("r") as f:
data = json.load(f)
validate_display_names(data)
data["themes"].sort(key=sort_key)
with path.open("w") as f:
json.dump(data, f, sort_keys=True, indent=2)
f.write("\n")
if __name__ == "__main__":
main()