|
27 | 27 | from warnings import warn
|
28 | 28 |
|
29 | 29 | from polib import pofile
|
30 |
| -from transifex.api import transifex_api |
31 |
| - |
32 |
| -LANGUAGE = 'pl' |
33 |
| -PROJECT_SLUG = 'python-newest' |
34 |
| -VERSION = '3.14' |
35 |
| - |
36 |
| - |
37 |
| -def fetch(): |
38 |
| - """ |
39 |
| - Fetch translations from Transifex, remove source lines. |
40 |
| - """ |
41 |
| - if (code := call("tx --version", shell=True)) != 0: |
42 |
| - sys.stderr.write("The Transifex client app is required.\n") |
43 |
| - exit(code) |
44 |
| - lang = LANGUAGE |
45 |
| - _call(f'tx pull -l {lang} --minimum-perc=1 --force --skip') |
46 |
| - for file in Path().rglob('*.po'): |
47 |
| - _call(f'msgcat --no-location -o {file} {file}') |
48 |
| - |
49 |
| - |
50 |
| -def _call(command: str): |
51 |
| - if (return_code := call(command, shell=True)) != 0: |
52 |
| - exit(return_code) |
53 |
| - |
54 |
| - |
55 |
| -def recreate_tx_config(): |
56 |
| - """ |
57 |
| - Regenerate Transifex client config for all resources. |
58 |
| - """ |
59 |
| - with TemporaryDirectory() as directory: |
60 |
| - with chdir(directory): |
61 |
| - _clone_cpython_repo(VERSION) |
62 |
| - _build_gettext() |
63 |
| - with chdir(Path(directory) / 'cpython/Doc/build'): |
64 |
| - _create_txconfig() |
65 |
| - _update_txconfig_resources() |
66 |
| - with open('.tx/config', 'r') as file: |
67 |
| - contents = file.read() |
68 |
| - contents = contents.replace('./<lang>/LC_MESSAGES/', '') |
69 |
| - with open('.tx/config', 'w') as file: |
70 |
| - file.write(contents) |
71 |
| - warn_about_files_to_delete() |
72 |
| - |
73 |
| -def warn_about_files_to_delete(): |
74 |
| - files = list(_get_files_to_delete()) |
75 |
| - if not files: |
76 |
| - return |
77 |
| - warn(f'Found {len(files)} file(s) to delete: {", ".join(files)}.') |
78 |
| - |
79 |
| -def _get_files_to_delete(): |
80 |
| - with open('.tx/config') as config_file: |
81 |
| - config = config_file.read() |
82 |
| - for file in Path().rglob('*.po'): |
83 |
| - if os.fsdecode(file) not in config: |
84 |
| - yield os.fsdecode(file) |
85 |
| - |
86 |
| - |
87 |
| -def _clone_cpython_repo(version: str): |
88 |
| - _call(f'git clone -b {version} --single-branch https://github.com/python/cpython.git --depth 1') |
89 |
| - |
90 |
| - |
91 |
| -def _build_gettext(): |
92 |
| - _call("make -C cpython/Doc/ gettext") |
93 |
| - |
94 |
| - |
95 |
| -def _create_txconfig(): |
96 |
| - _call('sphinx-intl create-txconfig') |
97 |
| - |
98 |
| - |
99 |
| -def _update_txconfig_resources(): |
100 |
| - _call( |
101 |
| - f'sphinx-intl update-txconfig-resources --transifex-organization-name python-doc ' |
102 |
| - f'--transifex-project-name={PROJECT_SLUG} --locale-dir . --pot-dir gettext' |
103 |
| - ) |
104 |
| - |
105 |
| - |
106 |
| -@dataclass |
107 |
| -class ResourceLanguageStatistics: |
108 |
| - name: str |
109 |
| - total_words: int |
110 |
| - translated_words: int |
111 |
| - total_strings: int |
112 |
| - translated_strings: int |
113 |
| - |
114 |
| - @classmethod |
115 |
| - def from_api_entry(cls, data: transifex_api.ResourceLanguageStats) -> Self: |
116 |
| - return cls( |
117 |
| - name=data.id.removeprefix(f'o:python-doc:p:{PROJECT_SLUG}:r:').removesuffix(f':l:{LANGUAGE}'), |
118 |
| - total_words=data.attributes['total_words'], |
119 |
| - translated_words=data.attributes['translated_words'], |
120 |
| - total_strings=data.attributes['total_strings'], |
121 |
| - translated_strings=data.attributes['translated_strings'], |
122 |
| - ) |
123 |
| - |
124 |
| - |
125 |
| -def _get_tx_token() -> str: |
126 |
| - if os.path.exists('.tx/api-key'): |
127 |
| - with open('.tx/api-key') as f: |
128 |
| - transifex_api_key = f.read() |
129 |
| - else: |
130 |
| - transifex_api_key = os.getenv('TX_TOKEN', '') |
131 |
| - return transifex_api_key |
132 |
| - |
133 |
| - |
134 |
| -def _get_resources() -> list[transifex_api.Resource]: |
135 |
| - transifex_api.setup(auth=_get_tx_token()) |
136 |
| - return transifex_api.Resource.filter(project=f'o:python-doc:p:{PROJECT_SLUG}').all() |
137 |
| - |
138 |
| - |
139 |
| -def get_resource_language_stats() -> list[ResourceLanguageStatistics]: |
140 |
| - transifex_api.setup(auth=_get_tx_token()) |
141 |
| - resources = transifex_api.ResourceLanguageStats.filter( |
142 |
| - project=f'o:python-doc:p:{PROJECT_SLUG}', language=f'l:{LANGUAGE}' |
143 |
| - ).all() |
144 |
| - return [ResourceLanguageStatistics.from_api_entry(entry) for entry in resources] |
145 |
| - |
146 |
| - |
147 |
| -def progress_from_resources(resources: Iterable[ResourceLanguageStatistics]) -> float: |
148 |
| - pairs = ((e.translated_words, e.total_words) for e in resources) |
149 |
| - translated_total, total_total = (sum(counts) for counts in zip(*pairs)) |
150 |
| - return translated_total / total_total * 100 |
151 |
| - |
152 |
| - |
153 |
| -def get_number_of_translators(): |
154 |
| - translators = set(_fetch_translators()) |
155 |
| - _remove_bot(translators) |
156 |
| - translators = _eliminate_aliases(translators) |
157 |
| - return len(translators) |
158 |
| - |
159 |
| - |
160 |
| -def _fetch_translators() -> Generator[str, None, None]: |
161 |
| - for file in Path().rglob('*.po'): |
162 |
| - header = pofile(file).header.splitlines() |
163 |
| - for translator_record in header[header.index('Translators:') + 1:]: |
164 |
| - translator, _year = translator_record.split(', ') |
165 |
| - yield translator |
166 |
| - |
167 |
| - |
168 |
| -def _remove_bot(translators: set[str]) -> None: |
169 |
| - translators.remove("Transifex Bot <>") |
170 |
| - |
171 |
| - |
172 |
| -def _eliminate_aliases(translators: set[str]) -> set[str]: |
173 |
| - unique = set() |
174 |
| - for name in translators: |
175 |
| - for match in unique: |
176 |
| - if (ratio := SequenceMatcher(lambda x: x in '<>@', name, match).ratio()) > 0.64: |
177 |
| - info(f"{name} and {match} are similar ({ratio:.3f}). Deduplicating.") |
178 |
| - break |
179 |
| - else: |
180 |
| - unique.add(name) |
181 |
| - return unique |
182 |
| - |
183 |
| - |
184 |
| -def language_switcher(entry: ResourceLanguageStatistics) -> bool: |
185 |
| - language_switcher_resources_prefixes = ('bugs', 'tutorial', 'library--functions') |
186 |
| - return any(entry.name.startswith(prefix) for prefix in language_switcher_resources_prefixes) |
| 30 | +# from transifex.api import transifex_api |
| 31 | +# |
| 32 | +# LANGUAGE = 'pl' |
| 33 | +# PROJECT_SLUG = 'python-newest' |
| 34 | +# VERSION = '3.14' |
| 35 | +# |
| 36 | +# |
| 37 | +# def fetch(): |
| 38 | +# """ |
| 39 | +# Fetch translations from Transifex, remove source lines. |
| 40 | +# """ |
| 41 | +# if (code := call("tx --version", shell=True)) != 0: |
| 42 | +# sys.stderr.write("The Transifex client app is required.\n") |
| 43 | +# exit(code) |
| 44 | +# lang = LANGUAGE |
| 45 | +# _call(f'tx pull -l {lang} --minimum-perc=1 --force --skip') |
| 46 | +# for file in Path().rglob('*.po'): |
| 47 | +# _call(f'msgcat --no-location -o {file} {file}') |
| 48 | +# |
| 49 | +# |
| 50 | +# def _call(command: str): |
| 51 | +# if (return_code := call(command, shell=True)) != 0: |
| 52 | +# exit(return_code) |
| 53 | +# |
| 54 | +# |
| 55 | +# def recreate_tx_config(): |
| 56 | +# """ |
| 57 | +# Regenerate Transifex client config for all resources. |
| 58 | +# """ |
| 59 | +# with TemporaryDirectory() as directory: |
| 60 | +# with chdir(directory): |
| 61 | +# _clone_cpython_repo(VERSION) |
| 62 | +# _build_gettext() |
| 63 | +# with chdir(Path(directory) / 'cpython/Doc/build'): |
| 64 | +# _create_txconfig() |
| 65 | +# _update_txconfig_resources() |
| 66 | +# with open('.tx/config', 'r') as file: |
| 67 | +# contents = file.read() |
| 68 | +# contents = contents.replace('./<lang>/LC_MESSAGES/', '') |
| 69 | +# with open('.tx/config', 'w') as file: |
| 70 | +# file.write(contents) |
| 71 | +# warn_about_files_to_delete() |
| 72 | +# |
| 73 | +# def warn_about_files_to_delete(): |
| 74 | +# files = list(_get_files_to_delete()) |
| 75 | +# if not files: |
| 76 | +# return |
| 77 | +# warn(f'Found {len(files)} file(s) to delete: {", ".join(files)}.') |
| 78 | +# |
| 79 | +# def _get_files_to_delete(): |
| 80 | +# with open('.tx/config') as config_file: |
| 81 | +# config = config_file.read() |
| 82 | +# for file in Path().rglob('*.po'): |
| 83 | +# if os.fsdecode(file) not in config: |
| 84 | +# yield os.fsdecode(file) |
| 85 | +# |
| 86 | +# |
| 87 | +# def _clone_cpython_repo(version: str): |
| 88 | +# _call(f'git clone -b {version} --single-branch https://github.com/python/cpython.git --depth 1') |
| 89 | +# |
| 90 | +# |
| 91 | +# def _build_gettext(): |
| 92 | +# _call("make -C cpython/Doc/ gettext") |
| 93 | +# |
| 94 | +# |
| 95 | +# def _create_txconfig(): |
| 96 | +# _call('sphinx-intl create-txconfig') |
| 97 | +# |
| 98 | +# |
| 99 | +# def _update_txconfig_resources(): |
| 100 | +# _call( |
| 101 | +# f'sphinx-intl update-txconfig-resources --transifex-organization-name python-doc ' |
| 102 | +# f'--transifex-project-name={PROJECT_SLUG} --locale-dir . --pot-dir gettext' |
| 103 | +# ) |
| 104 | +# |
| 105 | +# |
| 106 | +# @dataclass |
| 107 | +# class ResourceLanguageStatistics: |
| 108 | +# name: str |
| 109 | +# total_words: int |
| 110 | +# translated_words: int |
| 111 | +# total_strings: int |
| 112 | +# translated_strings: int |
| 113 | +# |
| 114 | +# @classmethod |
| 115 | +# def from_api_entry(cls, data: transifex_api.ResourceLanguageStats) -> Self: |
| 116 | +# return cls( |
| 117 | +# name=data.id.removeprefix(f'o:python-doc:p:{PROJECT_SLUG}:r:').removesuffix(f':l:{LANGUAGE}'), |
| 118 | +# total_words=data.attributes['total_words'], |
| 119 | +# translated_words=data.attributes['translated_words'], |
| 120 | +# total_strings=data.attributes['total_strings'], |
| 121 | +# translated_strings=data.attributes['translated_strings'], |
| 122 | +# ) |
| 123 | +# |
| 124 | +# |
| 125 | +# def _get_tx_token() -> str: |
| 126 | +# if os.path.exists('.tx/api-key'): |
| 127 | +# with open('.tx/api-key') as f: |
| 128 | +# transifex_api_key = f.read() |
| 129 | +# else: |
| 130 | +# transifex_api_key = os.getenv('TX_TOKEN', '') |
| 131 | +# return transifex_api_key |
| 132 | +# |
| 133 | +# |
| 134 | +# def _get_resources() -> list[transifex_api.Resource]: |
| 135 | +# transifex_api.setup(auth=_get_tx_token()) |
| 136 | +# return transifex_api.Resource.filter(project=f'o:python-doc:p:{PROJECT_SLUG}').all() |
| 137 | +# |
| 138 | +# |
| 139 | +# def get_resource_language_stats() -> list[ResourceLanguageStatistics]: |
| 140 | +# transifex_api.setup(auth=_get_tx_token()) |
| 141 | +# resources = transifex_api.ResourceLanguageStats.filter( |
| 142 | +# project=f'o:python-doc:p:{PROJECT_SLUG}', language=f'l:{LANGUAGE}' |
| 143 | +# ).all() |
| 144 | +# return [ResourceLanguageStatistics.from_api_entry(entry) for entry in resources] |
| 145 | +# |
| 146 | +# |
| 147 | +# def progress_from_resources(resources: Iterable[ResourceLanguageStatistics]) -> float: |
| 148 | +# pairs = ((e.translated_words, e.total_words) for e in resources) |
| 149 | +# translated_total, total_total = (sum(counts) for counts in zip(*pairs)) |
| 150 | +# return translated_total / total_total * 100 |
| 151 | +# |
| 152 | +# |
| 153 | +# def get_number_of_translators(): |
| 154 | +# translators = set(_fetch_translators()) |
| 155 | +# _remove_bot(translators) |
| 156 | +# translators = _eliminate_aliases(translators) |
| 157 | +# return len(translators) |
| 158 | +# |
| 159 | +# |
| 160 | +# def _fetch_translators() -> Generator[str, None, None]: |
| 161 | +# for file in Path().rglob('*.po'): |
| 162 | +# header = pofile(file).header.splitlines() |
| 163 | +# for translator_record in header[header.index('Translators:') + 1:]: |
| 164 | +# translator, _year = translator_record.split(', ') |
| 165 | +# yield translator |
| 166 | +# |
| 167 | +# |
| 168 | +# def _remove_bot(translators: set[str]) -> None: |
| 169 | +# translators.remove("Transifex Bot <>") |
| 170 | +# |
| 171 | +# |
| 172 | +# def _eliminate_aliases(translators: set[str]) -> set[str]: |
| 173 | +# unique = set() |
| 174 | +# for name in translators: |
| 175 | +# for match in unique: |
| 176 | +# if (ratio := SequenceMatcher(lambda x: x in '<>@', name, match).ratio()) > 0.64: |
| 177 | +# info(f"{name} and {match} are similar ({ratio:.3f}). Deduplicating.") |
| 178 | +# break |
| 179 | +# else: |
| 180 | +# unique.add(name) |
| 181 | +# return unique |
| 182 | +# |
| 183 | +# |
| 184 | +# def language_switcher(entry: ResourceLanguageStatistics) -> bool: |
| 185 | +# language_switcher_resources_prefixes = ('bugs', 'tutorial', 'library--functions') |
| 186 | +# return any(entry.name.startswith(prefix) for prefix in language_switcher_resources_prefixes) |
187 | 187 |
|
188 | 188 |
|
189 | 189 | def generate_commit_msg():
|
@@ -215,7 +215,7 @@ def generate_commit_msg():
|
215 | 215 | translators.add(f'Co-Authored-By: {translator}')
|
216 | 216 | break
|
217 | 217 |
|
218 |
| - print('Update translation from Transifex\n' + "\n".join(translators)) |
| 218 | + print('Update translation from Transifex\n\n' + "\n".join(translators)) |
219 | 219 |
|
220 | 220 | if __name__ == "__main__":
|
221 | 221 | RUNNABLE_SCRIPTS = ('fetch', 'recreate_tx_config', 'warn_about_files_to_delete', 'generate_commit_msg')
|
|
0 commit comments