Skip to content

Commit 7978fed

Browse files
committed
Indicate languages in the docs.python.org switcher
1 parent ea33d13 commit 7978fed

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

generate.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# /// script
2+
# requires-python = ">=3.11"
23
# dependencies = [
34
# "gitpython",
45
# "potodo",
@@ -16,6 +17,7 @@
1617
from jinja2 import Template
1718

1819
import repositories
20+
import switcher
1921
import visitors
2022
from completion import branches_from_devguide, get_completion
2123

@@ -30,13 +32,23 @@
3032
)
3133
subprocess.run(['make', '-C', Path(clones_dir, 'cpython/Doc'), 'venv'], check=True)
3234
subprocess.run(['make', '-C', Path(clones_dir, 'cpython/Doc'), 'gettext'], check=True)
35+
switcher_languages = switcher.get_languages()
3336
for language, repo in repositories.get_languages_and_repos(devguide_dir):
3437
if repo:
3538
completion_number, translators_number = get_completion(clones_dir, repo)
3639
visitors_number = visitors.get_number_of_visitors(language)
3740
else:
3841
completion_number, visitors_number = 0.0, 0
39-
completion_progress.append((language, repo, completion_number, translators_number, visitors_number))
42+
completion_progress.append(
43+
(
44+
language,
45+
repo,
46+
completion_number,
47+
translators_number,
48+
visitors_number,
49+
switcher_languages[language],
50+
)
51+
)
4052
print(completion_progress[-1])
4153

4254
template = Template(
@@ -58,12 +70,12 @@
5870
</tr>
5971
</thead>
6072
<tbody>
61-
{% for language, repo, completion, translators, visitors in completion_progress | sort(attribute=2) | reverse %}
73+
{% for language, repo, completion, translators, visitors, in_switcher in completion_progress | sort(attribute=2) | reverse %}
6274
<tr>
6375
{% if repo %}
6476
<td data-label="language">
6577
<a href="https://github.com/{{ repo }}" target="_blank">
66-
{{ language }}
78+
{{ language -}}
6779
</a>
6880
</td>
6981
<td data-label="visitors">
@@ -84,6 +96,7 @@
8496
{% endfor %}
8597
</tbody>
8698
</table>
99+
<p>* Language is in the <a href="https://docs.python.org">docs.python.org</a> switcher.</p>
87100
<p>Last updated at {{ generation_time.strftime('%A, %d %B %Y, %X %Z') }}.</p>
88101
</body>
89102
</html>

switcher.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Fetch languages in the https://docs.python.org language switcher.
3+
4+
Return a defaultdict mapping language codes to a Boolean indicating
5+
whether it is in the language switcher.
6+
"""
7+
8+
import tomllib
9+
from collections import defaultdict
10+
11+
import requests
12+
13+
14+
def get_languages() -> defaultdict[str, bool]:
15+
# Languages missing from config.toml are not in production
16+
in_prod = defaultdict(lambda: False)
17+
data = requests.get(
18+
"https://raw.githubusercontent.com/"
19+
"python/docsbuild-scripts/refs/heads/main/config.toml",
20+
timeout=10,
21+
).text
22+
languages = tomllib.loads(data)["languages"]
23+
for code, language in languages.items():
24+
# Languages in config.toml default to being in production
25+
in_prod[code] = language.get("in_prod", True)
26+
return in_prod
27+
28+
29+
def main() -> None:
30+
languages = get_languages()
31+
print(languages)
32+
print("en:", languages["en"])
33+
print("pl:", languages["pl"])
34+
print("ar:", languages["ar"])
35+
36+
37+
if __name__ == "__main__":
38+
main()

0 commit comments

Comments
 (0)