Skip to content

Add pre-commit #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update config
  • Loading branch information
StanFromIreland committed Jun 10, 2025
commit 0fee92ad20ef17fb0661936aeba6b45e02fb296b
82 changes: 41 additions & 41 deletions manage_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,29 @@
from polib import pofile
from transifex.api import transifex_api

LANGUAGE = "pl"
LANGUAGE = 'pl'


def fetch():
"""
Fetch translations from Transifex, remove source lines.
"""
if (code := call("tx --version", shell=True)) != 0:
sys.stderr.write("The Transifex client app is required.\n")
if (code := call('tx --version', shell=True)) != 0:
sys.stderr.write('The Transifex client app is required.\n')
exit(code)
lang = LANGUAGE
_call(f"tx pull -l {lang} --minimum-perc=1 --force --skip")
for file in Path().rglob("*.po"):
_call(f"msgcat --no-location -o {file} {file}")
_call(f'tx pull -l {lang} --minimum-perc=1 --force --skip')
for file in Path().rglob('*.po'):
_call(f'msgcat --no-location -o {file} {file}')


def _call(command: str):
if (return_code := call(command, shell=True)) != 0:
exit(return_code)


PROJECT_SLUG = "python-newest"
VERSION = "3.14"
PROJECT_SLUG = 'python-newest'
VERSION = '3.14'


def recreate_tx_config():
Expand All @@ -61,13 +61,13 @@ def recreate_tx_config():
with chdir(directory):
_clone_cpython_repo(VERSION)
_build_gettext()
with chdir(Path(directory) / "cpython/Doc/build"):
with chdir(Path(directory) / 'cpython/Doc/build'):
_create_txconfig()
_update_txconfig_resources()
with open(".tx/config", "r") as file:
with open('.tx/config', 'r') as file:
contents = file.read()
contents = contents.replace("./<lang>/LC_MESSAGES/", "")
with open(".tx/config", "w") as file:
contents = contents.replace('./<lang>/LC_MESSAGES/', '')
with open('.tx/config', 'w') as file:
file.write(contents)
warn_about_files_to_delete()

Expand All @@ -76,35 +76,35 @@ def warn_about_files_to_delete():
files = list(_get_files_to_delete())
if not files:
return
warn(f"Found {len(files)} file(s) to delete: {', '.join(files)}.")
warn(f'Found {len(files)} file(s) to delete: {", ".join(files)}.')


def _get_files_to_delete():
with open(".tx/config") as config_file:
with open('.tx/config') as config_file:
config = config_file.read()
for file in Path().rglob("*.po"):
for file in Path().rglob('*.po'):
if os.fsdecode(file) not in config:
yield os.fsdecode(file)


def _clone_cpython_repo(version: str):
_call(
f"git clone -b {version} --single-branch https://github.com/python/cpython.git --depth 1"
f'git clone -b {version} --single-branch https://github.com/python/cpython.git --depth 1'
)


def _build_gettext():
_call("make -C cpython/Doc/ gettext")
_call('make -C cpython/Doc/ gettext')


def _create_txconfig():
_call("sphinx-intl create-txconfig")
_call('sphinx-intl create-txconfig')


def _update_txconfig_resources():
_call(
f"sphinx-intl update-txconfig-resources --transifex-organization-name python-doc "
f"--transifex-project-name={PROJECT_SLUG} --locale-dir . --pot-dir gettext"
f'sphinx-intl update-txconfig-resources --transifex-organization-name python-doc '
f'--transifex-project-name={PROJECT_SLUG} --locale-dir . --pot-dir gettext'
)


Expand All @@ -119,34 +119,34 @@ class ResourceLanguageStatistics:
@classmethod
def from_api_entry(cls, data: transifex_api.ResourceLanguageStats) -> Self:
return cls(
name=data.id.removeprefix(f"o:python-doc:p:{PROJECT_SLUG}:r:").removesuffix(
f":l:{LANGUAGE}"
name=data.id.removeprefix(f'o:python-doc:p:{PROJECT_SLUG}:r:').removesuffix(
f':l:{LANGUAGE}'
),
total_words=data.attributes["total_words"],
translated_words=data.attributes["translated_words"],
total_strings=data.attributes["total_strings"],
translated_strings=data.attributes["translated_strings"],
total_words=data.attributes['total_words'],
translated_words=data.attributes['translated_words'],
total_strings=data.attributes['total_strings'],
translated_strings=data.attributes['translated_strings'],
)


def _get_tx_token() -> str:
if os.path.exists(".tx/api-key"):
with open(".tx/api-key") as f:
if os.path.exists('.tx/api-key'):
with open('.tx/api-key') as f:
transifex_api_key = f.read()
else:
transifex_api_key = os.getenv("TX_TOKEN", "")
transifex_api_key = os.getenv('TX_TOKEN', '')
return transifex_api_key


def _get_resources() -> list[transifex_api.Resource]:
transifex_api.setup(auth=_get_tx_token())
return transifex_api.Resource.filter(project=f"o:python-doc:p:{PROJECT_SLUG}").all()
return transifex_api.Resource.filter(project=f'o:python-doc:p:{PROJECT_SLUG}').all()


def get_resource_language_stats() -> list[ResourceLanguageStatistics]:
transifex_api.setup(auth=_get_tx_token())
resources = transifex_api.ResourceLanguageStats.filter(
project=f"o:python-doc:p:{PROJECT_SLUG}", language=f"l:{LANGUAGE}"
project=f'o:python-doc:p:{PROJECT_SLUG}', language=f'l:{LANGUAGE}'
).all()
return [ResourceLanguageStatistics.from_api_entry(entry) for entry in resources]

Expand All @@ -165,43 +165,43 @@ def get_number_of_translators():


def _fetch_translators() -> Generator[str, None, None]:
for file in Path().rglob("*.po"):
for file in Path().rglob('*.po'):
header = pofile(file).header.splitlines()
for translator_record in header[header.index("Translators:") + 1 :]:
translator, _year = translator_record.split(", ")
for translator_record in header[header.index('Translators:') + 1 :]:
translator, _year = translator_record.split(', ')
yield translator


def _remove_bot(translators: set[str]) -> None:
translators.remove("Transifex Bot <>")
translators.remove('Transifex Bot <>')


def _eliminate_aliases(translators: set[str]) -> set[str]:
unique = set()
for name in translators:
for match in unique:
if (
ratio := SequenceMatcher(lambda x: x in "<>@", name, match).ratio()
ratio := SequenceMatcher(lambda x: x in '<>@', name, match).ratio()
) > 0.64:
info(f"{name} and {match} are similar ({ratio:.3f}). Deduplicating.")
info(f'{name} and {match} are similar ({ratio:.3f}). Deduplicating.')
break
else:
unique.add(name)
return unique


def language_switcher(entry: ResourceLanguageStatistics) -> bool:
language_switcher_resources_prefixes = ("bugs", "tutorial", "library--functions")
language_switcher_resources_prefixes = ('bugs', 'tutorial', 'library--functions')
return any(
entry.name.startswith(prefix) for prefix in language_switcher_resources_prefixes
)


if __name__ == "__main__":
RUNNABLE_SCRIPTS = ("fetch", "recreate_tx_config", "warn_about_files_to_delete")
if __name__ == '__main__':
RUNNABLE_SCRIPTS = ('fetch', 'recreate_tx_config', 'warn_about_files_to_delete')

parser = ArgumentParser()
parser.add_argument("cmd", choices=RUNNABLE_SCRIPTS)
parser.add_argument('cmd', choices=RUNNABLE_SCRIPTS)
options = parser.parse_args()

eval(options.cmd)()
2 changes: 2 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[format]
quote-style = "single"
35 changes: 0 additions & 35 deletions update_switcher_chart.py

This file was deleted.