From b7412e503f49ccb9755e990d73993e290ee4ba1e Mon Sep 17 00:00:00 2001 From: Maciej Olko Date: Sun, 8 Dec 2024 23:29:24 +0100 Subject: [PATCH 1/9] Add links --- completion.py | 24 ++++++++++++++++++++++++ generate.py | 45 +++++++++++++++++++++++++++------------------ style.css | 5 +++-- visitors.py | 15 +++++++++++++++ 4 files changed, 69 insertions(+), 20 deletions(-) create mode 100644 completion.py create mode 100644 visitors.py diff --git a/completion.py b/completion.py new file mode 100644 index 000000000..4f9099514 --- /dev/null +++ b/completion.py @@ -0,0 +1,24 @@ +import pathlib +import shutil + +import git +from potodo import potodo + + +def get_completion_and_branch(tmpdir: str, language: str) -> tuple[float, str]: + clone_path = pathlib.Path(tmpdir, language) + for branch in ('3.13', '3.12', '3.11', '3.10', '3.9'): + try: + git.Repo.clone_from(f'https://github.com/python/python-docs-{language}.git', clone_path, depth=1, branch=branch) + except git.GitCommandError: + print(f'failed to clone {language} {branch}') + continue + try: + completion = potodo.scan_path(clone_path, no_cache=True, hide_reserved=False, api_url='').completion + except OSError: + print(f'failed to scan {language} {branch}') + shutil.rmtree(clone_path) + continue + else: + break + return completion, branch diff --git a/generate.py b/generate.py index 847c52cb1..306d79120 100644 --- a/generate.py +++ b/generate.py @@ -3,37 +3,36 @@ # "gitpython", # "potodo", # "jinja2", +# "requests", +# "humanize", # ] # /// +import csv +import io +import zipfile from datetime import datetime, timezone from pathlib import Path from shutil import rmtree from tempfile import TemporaryDirectory +from urllib.parse import urlencode + +import humanize +import requests from git import Repo, GitCommandError from potodo.potodo import scan_path from jinja2 import Template +import completion +import visitors + completion_progress = [] generation_time = datetime.now(timezone.utc) with TemporaryDirectory() as tmpdir: for language in ('es', 'fr', 'id', 'it', 'ja', 'ko', 'pl', 'pt-br', 'tr', 'uk', 'zh-cn', 'zh-tw'): - clone_path = Path(tmpdir, language) - for branch in ('3.13', '3.12', '3.11', '3.10', '3.9'): - try: - Repo.clone_from(f'https://github.com/python/python-docs-{language}.git', clone_path, depth=1, branch=branch) - except GitCommandError: - print(f'failed to clone {language} {branch}') - continue - try: - completion = scan_path(clone_path, no_cache=True, hide_reserved=False, api_url='').completion - except OSError: - print(f'failed to scan {language} {branch}') - rmtree(clone_path) - continue - else: - break - completion_progress.append((language, completion, branch)) + completion_number, branch = completion.get_completion_and_branch(tmpdir, language) + visitors_number = visitors.get_number_of_visitors(language) + completion_progress.append((language, completion_number, branch, visitors_number)) print(completion_progress[-1]) template = Template(""" @@ -46,16 +45,26 @@

Python Docs Translation Dashboard

- + + + + + + -{% for language, completion, branch in completion_progress | sort(attribute=1) | reverse %} +{% for language, completion, branch, visitors in completion_progress | sort(attribute=1) | reverse %} + diff --git a/visitors.py b/visitors.py index 464b37d8c..5c4315c76 100644 --- a/visitors.py +++ b/visitors.py @@ -6,9 +6,9 @@ import requests -def get_number_of_visitors(language: str) -> str: +def get_number_of_visitors(language: str) -> int: param = urllib.parse.urlencode({'filters': f'[["contains","event:page",["/{language}/"]]]'}) r = requests.get(f'https://plausible.io/docs.python.org/export?{param}', timeout=10) with zipfile.ZipFile(io.BytesIO(r.content), 'r') as z, z.open('visitors.csv') as csv_file: csv_reader = csv.DictReader(io.TextIOWrapper(csv_file)) - return f"{sum(int(row["visitors"]) for row in csv_reader):,}" + return sum(int(row["visitors"]) for row in csv_reader)
languagebranchcompletion
languagevisitorsbranchcompletion
{{ language }} + + {{ visitors }} + + {{ branch }}
{{ completion | round(2) }}%
diff --git a/style.css b/style.css index dd07efeb2..5feb55743 100644 --- a/style.css +++ b/style.css @@ -9,6 +9,7 @@ th, td { border: 1px solid #ddd; padding: 8px 12px; text-align: left; + white-space: nowrap; } th { background-color: #f4f4f4; @@ -24,7 +25,7 @@ th { min-width: 50px; box-sizing: border-box; } -td:nth-child(3) { +td:last-child { width: 100%; } @media screen and (max-width: 600px) { @@ -52,7 +53,7 @@ td:nth-child(3) { left: 10px; position: absolute; } - td:nth-child(3) { + td:last-child { width: inherit; } .progress-bar { diff --git a/visitors.py b/visitors.py new file mode 100644 index 000000000..e4bf49a5d --- /dev/null +++ b/visitors.py @@ -0,0 +1,15 @@ +import csv +import io +import urllib +import zipfile + +import humanize +import requests + + +def get_number_of_visitors(language: str) -> str: + param = urllib.parse.urlencode({'filters': f'[["contains","event:page",["/{language}/"]]]'}) + r = requests.get(f'https://plausible.io/docs.python.org/export?{param}', timeout=10) + with zipfile.ZipFile(io.BytesIO(r.content), 'r') as z, z.open('visitors.csv') as csv_file: + csv_reader = csv.DictReader(io.TextIOWrapper(csv_file)) + return humanize.intword(sum(int(row["visitors"]) for row in csv_reader)) From 36a5c40f991295d60190aea55d3194974b963e4a Mon Sep 17 00:00:00 2001 From: Maciej Olko Date: Mon, 9 Dec 2024 09:01:25 +0100 Subject: [PATCH 2/9] Remove unused imports Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- generate.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/generate.py b/generate.py index 306d79120..fc1892ba4 100644 --- a/generate.py +++ b/generate.py @@ -7,9 +7,6 @@ # "humanize", # ] # /// -import csv -import io -import zipfile from datetime import datetime, timezone from pathlib import Path from shutil import rmtree From 7bb065c641f04dc7d8ae08a653ae31969377907b Mon Sep 17 00:00:00 2001 From: Maciej Olko Date: Mon, 9 Dec 2024 09:01:32 +0100 Subject: [PATCH 3/9] Remove unused imports Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- generate.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/generate.py b/generate.py index fc1892ba4..bc3341f39 100644 --- a/generate.py +++ b/generate.py @@ -8,8 +8,6 @@ # ] # /// from datetime import datetime, timezone -from pathlib import Path -from shutil import rmtree from tempfile import TemporaryDirectory from urllib.parse import urlencode From 8b9bbb42c06312a774cf8e23f74a004109888506 Mon Sep 17 00:00:00 2001 From: Maciej Olko Date: Mon, 9 Dec 2024 09:01:41 +0100 Subject: [PATCH 4/9] Remove unused imports Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- generate.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/generate.py b/generate.py index bc3341f39..2e9e5a088 100644 --- a/generate.py +++ b/generate.py @@ -11,10 +11,7 @@ from tempfile import TemporaryDirectory from urllib.parse import urlencode -import humanize -import requests -from git import Repo, GitCommandError -from potodo.potodo import scan_path +from git import GitCommandError from jinja2 import Template import completion From 735abe7f867aa3fe3b1475633826c8e1754d4771 Mon Sep 17 00:00:00 2001 From: Maciej Olko Date: Mon, 9 Dec 2024 20:32:35 +0100 Subject: [PATCH 5/9] Display directly visitors number Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- visitors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/visitors.py b/visitors.py index e4bf49a5d..f1c956503 100644 --- a/visitors.py +++ b/visitors.py @@ -12,4 +12,4 @@ def get_number_of_visitors(language: str) -> str: r = requests.get(f'https://plausible.io/docs.python.org/export?{param}', timeout=10) with zipfile.ZipFile(io.BytesIO(r.content), 'r') as z, z.open('visitors.csv') as csv_file: csv_reader = csv.DictReader(io.TextIOWrapper(csv_file)) - return humanize.intword(sum(int(row["visitors"]) for row in csv_reader)) + return f"{sum(int(row["visitors"]) for row in csv_reader):,}" From 0d49286461c4c5f2817c7ce8021cc6375ef89d3f Mon Sep 17 00:00:00 2001 From: Maciej Olko Date: Mon, 9 Dec 2024 20:32:48 +0100 Subject: [PATCH 6/9] Display directly visitors number Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- visitors.py | 1 - 1 file changed, 1 deletion(-) diff --git a/visitors.py b/visitors.py index f1c956503..464b37d8c 100644 --- a/visitors.py +++ b/visitors.py @@ -3,7 +3,6 @@ import urllib import zipfile -import humanize import requests From b926da0e9ace5a620042a21960ace36fe07451d3 Mon Sep 17 00:00:00 2001 From: Maciej Olko Date: Mon, 9 Dec 2024 20:32:58 +0100 Subject: [PATCH 7/9] Display directly visitors number Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- generate.py | 1 - 1 file changed, 1 deletion(-) diff --git a/generate.py b/generate.py index 2e9e5a088..c099b1ba6 100644 --- a/generate.py +++ b/generate.py @@ -4,7 +4,6 @@ # "potodo", # "jinja2", # "requests", -# "humanize", # ] # /// from datetime import datetime, timezone From a42008ed3da43648367d2d54024024ad38e11d42 Mon Sep 17 00:00:00 2001 From: Maciej Olko Date: Mon, 9 Dec 2024 22:30:37 +0100 Subject: [PATCH 8/9] Align visitors number to right --- style.css | 3 +++ 1 file changed, 3 insertions(+) diff --git a/style.css b/style.css index 5feb55743..7270d08bb 100644 --- a/style.css +++ b/style.css @@ -25,6 +25,9 @@ th { min-width: 50px; box-sizing: border-box; } +td:nth-child(2) { + text-align: right; +} td:last-child { width: 100%; } From b8238fa11cc952712f23254fcccdce7b0f13a47c Mon Sep 17 00:00:00 2001 From: Maciej Olko Date: Mon, 9 Dec 2024 22:56:16 +0100 Subject: [PATCH 9/9] Removed unused imports, move integer formatting to the template --- generate.py | 4 +--- visitors.py | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/generate.py b/generate.py index c099b1ba6..7fef33cbe 100644 --- a/generate.py +++ b/generate.py @@ -8,9 +8,7 @@ # /// from datetime import datetime, timezone from tempfile import TemporaryDirectory -from urllib.parse import urlencode -from git import GitCommandError from jinja2 import Template import completion @@ -53,7 +51,7 @@
- {{ visitors }} + {{ '{:,}'.format(visitors) }} {{ branch }}