|
9 | 9 | # ]
|
10 | 10 | # ///
|
11 | 11 | import subprocess
|
| 12 | +from collections.abc import Iterator |
12 | 13 | from datetime import datetime, timezone
|
13 | 14 | from pathlib import Path
|
14 | 15 | from tempfile import TemporaryDirectory
|
| 16 | +from typing import cast |
15 | 17 |
|
16 | 18 | from git import Repo
|
17 | 19 | from jinja2 import Template
|
|
21 | 23 | import visitors
|
22 | 24 | from completion import branches_from_devguide, get_completion
|
23 | 25 |
|
24 |
| -completion_progress = [] |
25 | 26 | generation_time = datetime.now(timezone.utc)
|
26 | 27 |
|
27 |
| -with TemporaryDirectory() as clones_dir: |
28 |
| - Repo.clone_from( |
29 |
| - 'https://github.com/python/devguide.git', |
30 |
| - devguide_dir := Path(clones_dir, 'devguide'), |
31 |
| - depth=1, |
32 |
| - ) |
33 |
| - latest_branch = branches_from_devguide(devguide_dir)[0] |
34 |
| - Repo.clone_from( |
35 |
| - 'https://github.com/python/cpython.git', |
36 |
| - Path(clones_dir, 'cpython'), |
37 |
| - depth=1, |
38 |
| - branch=latest_branch, |
39 |
| - ) |
40 |
| - subprocess.run(['make', '-C', Path(clones_dir, 'cpython/Doc'), 'venv'], check=True) |
41 |
| - subprocess.run( |
42 |
| - ['make', '-C', Path(clones_dir, 'cpython/Doc'), 'gettext'], check=True |
43 |
| - ) |
44 |
| - languages_built = dict(build_status.get_languages()) |
45 |
| - for language, repo in repositories.get_languages_and_repos(devguide_dir): |
46 |
| - if repo: |
47 |
| - completion_number, translators_number = get_completion(clones_dir, repo) |
48 |
| - visitors_number = visitors.get_number_of_visitors(language) |
49 |
| - else: |
50 |
| - completion_number, translators_number, visitors_number = 0.0, 0, 0 |
51 |
| - completion_progress.append( |
52 |
| - ( |
53 |
| - language, |
54 |
| - repo, |
55 |
| - completion_number, |
56 |
| - translators_number, |
57 |
| - visitors_number, |
58 |
| - language in languages_built, # built on docs.python.org |
59 |
| - languages_built.get(language), # in switcher |
60 |
| - ) |
| 28 | + |
| 29 | +def get_completion_progress() -> ( |
| 30 | + Iterator[tuple[str, str, float, int, int, bool, bool | None]] |
| 31 | +): |
| 32 | + with TemporaryDirectory() as clones_dir: |
| 33 | + Repo.clone_from( |
| 34 | + 'https://github.com/python/devguide.git', |
| 35 | + devguide_dir := Path(clones_dir, 'devguide'), |
| 36 | + depth=1, |
| 37 | + ) |
| 38 | + latest_branch = branches_from_devguide(devguide_dir)[0] |
| 39 | + Repo.clone_from( |
| 40 | + 'https://github.com/python/cpython.git', |
| 41 | + Path(clones_dir, 'cpython'), |
| 42 | + depth=1, |
| 43 | + branch=latest_branch, |
61 | 44 | )
|
62 |
| - print(completion_progress[-1]) |
| 45 | + subprocess.run( |
| 46 | + ['make', '-C', Path(clones_dir, 'cpython/Doc'), 'venv'], check=True |
| 47 | + ) |
| 48 | + subprocess.run( |
| 49 | + ['make', '-C', Path(clones_dir, 'cpython/Doc'), 'gettext'], check=True |
| 50 | + ) |
| 51 | + languages_built = dict(build_status.get_languages()) |
| 52 | + for lang, repo in repositories.get_languages_and_repos(devguide_dir): |
| 53 | + built = lang in languages_built |
| 54 | + in_switcher = languages_built.get(lang) |
| 55 | + if not repo: |
| 56 | + yield lang, cast(str, repo), 0.0, 0, 0, built, in_switcher |
| 57 | + continue |
| 58 | + completion, translators = get_completion(clones_dir, repo) |
| 59 | + visitors_num = visitors.get_number_of_visitors(lang) if built else 0 |
| 60 | + yield lang, repo, completion, translators, visitors_num, built, in_switcher |
63 | 61 |
|
64 |
| -template = Template( |
65 |
| - """ |
66 |
| -<html lang="en"> |
67 |
| -<head> |
68 |
| - <title>Python Docs Translation Dashboard</title> |
69 |
| - <link rel="stylesheet" href="style.css"> |
70 |
| -</head> |
71 |
| -<body> |
72 |
| -<h1>Python Docs Translation Dashboard</h1> |
73 |
| -<table> |
74 |
| -<thead> |
75 |
| -<tr> |
76 |
| - <th>language</th> |
77 |
| - <th>build</th> |
78 |
| - <th><a href="https://plausible.io/data-policy#how-we-count-unique-users-without-cookies">visitors<a/></th> |
79 |
| - <th>translators</th> |
80 |
| - <th>completion</th> |
81 |
| -</tr> |
82 |
| -</thead> |
83 |
| -<tbody> |
84 |
| -{% for language, repo, completion, translators, visitors, build, in_switcher in completion_progress | sort(attribute='2,3') | reverse %} |
85 |
| -<tr> |
86 |
| - {% if repo %} |
87 |
| - <td data-label="language"> |
88 |
| - <a href="https://github.com/{{ repo }}" target="_blank"> |
89 |
| - {{ language }} |
90 |
| - </a> |
91 |
| - </td> |
92 |
| - {% else %} |
93 |
| - <td data-label="language">{{ language }}</td> |
94 |
| - {% endif %} |
95 |
| - <td data-label="build"> |
96 |
| - {% if build %} |
97 |
| - <a href="https://docs.python.org/{{ language }}/" target="_blank">✓{% if in_switcher %} in switcher{% endif %}</a> |
98 |
| - {% else %} |
99 |
| - ✗ |
100 |
| - {% endif %} |
101 |
| - </td> |
102 |
| - <td data-label="visitors"> |
103 |
| - <a href="https://plausible.io/docs.python.org?filters=((contains,page,(/{{ language }}/)))" target="_blank"> |
104 |
| - {{ '{:,}'.format(visitors) }} |
105 |
| - </a> |
106 |
| - </td> |
107 |
| - <td data-label="translators">{{ '{:,}'.format(translators) }}</td> |
108 |
| - <td data-label="completion"> |
109 |
| - <div class="progress-bar" style="width: {{ completion | round(2) }}%;">{{ completion | round(2) }}%</div> |
110 |
| - </td> |
111 |
| -</tr> |
112 |
| -{% endfor %} |
113 |
| -</tbody> |
114 |
| -</table> |
115 |
| -<p>Last updated at {{ generation_time.strftime('%A, %d %B %Y, %X %Z') }}.</p> |
116 |
| -</body> |
117 |
| -</html> |
118 |
| -""" |
119 |
| -) |
120 | 62 |
|
121 |
| -output = template.render( |
122 |
| - completion_progress=completion_progress, generation_time=generation_time |
123 |
| -) |
| 63 | +if __name__ == '__main__': |
| 64 | + template = Template(Path('template.html.jinja').read_text()) |
| 65 | + |
| 66 | + output = template.render( |
| 67 | + completion_progress=get_completion_progress(), generation_time=generation_time |
| 68 | + ) |
124 | 69 |
|
125 |
| -with open('index.html', 'w') as file: |
126 |
| - file.write(output) |
| 70 | + Path('index.html').write_text(output) |
0 commit comments