diff --git a/counts.py b/counts.py new file mode 100644 index 000000000..b1cdeb49a --- /dev/null +++ b/counts.py @@ -0,0 +1,30 @@ +from pathlib import Path + +from polib import pofile + + +def _count(pot) -> tuple[int, int]: + pot = pofile(pot) + word_count = 0 + for entry in pot: + word_count += len(entry.msgid.split()) + return len(pot), word_count + + +def get_counts(dir: Path) -> tuple[int, int]: + total_string_count = 0 + total_word_count = 0 + for root, dirs, files in dir.walk(): + for file in files: + if file.endswith('.pot'): + pot = root.joinpath(file) + strings, words = _count(pot.read_text()) + total_string_count += strings + total_word_count += words + return total_string_count, total_word_count + + +if __name__ == '__main__': + print( + get_counts(Path(__file__).parent.joinpath('clones/cpython/Doc/build/gettext')) + ) diff --git a/generate.py b/generate.py index f773848b3..5df32e98a 100644 --- a/generate.py +++ b/generate.py @@ -15,8 +15,8 @@ import build_status import contribute from completion import branches_from_devguide, get_completion, TranslatorsData +from counts import get_counts from repositories import Language, get_languages_and_repos -from word_count import get_word_count generation_time = datetime.now(timezone.utc) @@ -99,7 +99,7 @@ class LanguageProjectData: completion_progress=(completion_progress := list(get_completion_progress())), generation_time=generation_time, duration=(datetime.now(timezone.utc) - generation_time).seconds, - word_count=get_word_count(Path('clones', 'cpython', 'Doc', 'build', 'gettext')), + counts=get_counts(Path('clones', 'cpython', 'Doc', 'build', 'gettext')), ) Path('index.html').write_text(output) diff --git a/template.html.jinja b/template.html.jinja index 676f83117..18575b9f0 100644 --- a/template.html.jinja +++ b/template.html.jinja @@ -55,7 +55,7 @@ main | meta

* the number in parentheses shows change in the last 30 days, included in the total completion

Currently being translated into {{ completion_progress|length }} languages. -The documentation has a total word count of {{ '{:,}'.format(word_count) }}. +The documentation has a total string count of {{ '{:,}'.format(counts[0]) }} ({{ '{:,}'.format(counts[1]) }} words). For more information about translations, see the Python Developer’s Guide.


diff --git a/word_count.py b/word_count.py deleted file mode 100644 index b7c9f1972..000000000 --- a/word_count.py +++ /dev/null @@ -1,20 +0,0 @@ -import os -from polib import pofile - - -def _count_words(pot) -> int: - pot = pofile(pot) - word_count = 0 - for entry in pot: - word_count += len(entry.msgid.split()) - return word_count - - -def get_word_count(dir) -> int: - total_word_count = 0 - for root, dirs, files in os.walk(dir): - for file in files: - if file.endswith('.pot'): - pot = os.path.join(root, file) - total_word_count += _count_words(pot) - return total_word_count