[ Web Proxy ]
URL:
Viewing:
https://github-redirect.dependabot.com/python/python-docs-pl/blob/3.13/manage_translation.py
[Back]
[Original]
python-docs-pl/manage_translation.py at 3.13 python/python-docs-pl GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
[]
[]
GitHub
Repository hosting, code review, and collaboration
[]
[]
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
[]
[]
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
[]
[]
GitHub Universe
The event for developers, by developers talks, workshops, and whats next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
python
python-docs-pl
Repository navigation
Code
Issues
4
(4)
Pull requests
2
(2)
Actions
Security and quality
Insights
More
items
Files
Expand file tree
3.13
Breadcrumbs
python-docs-pl
/
manage_translation.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
executable file
252 lines (199 loc) 7.85 KB
3.13
Breadcrumbs
python-docs-pl
/
manage_translation.py
Copy path
Top
File metadata and controls
Code
Blame
executable file
252 lines (199 loc) 7.85 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env python
#
# This python file contains utility scripts to manage Python docs Polish translation.
# It has to be run inside the python-docs-pl git root directory.
#
# Inspired by django-docs-translations script by claudep.
#
# The following commands are available:
#
# * fetch: fetch translations from transifex.com and strip source lines from the
# files.
# * recreate_tx_config: recreate configuration for all resources.
# * warn_about_files_to_delete: lists files that are not available upstream
# * generate_commit_msg: generates commit message with co-authors
from
argparse
import
ArgumentParser
import
os
from
contextlib
import
chdir
from
dataclasses
import
dataclass
from
difflib
import
SequenceMatcher
from
logging
import
info
from
pathlib
import
Path
from
subprocess
import
call
,
run
,
CalledProcessError
import
sys
from
tempfile
import
TemporaryDirectory
from
typing
import
Self
,
Generator
,
Iterable
from
warnings
import
warn
from
polib
import
pofile
,
POFile
from
transifex
.
api
import
transifex_api
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
"
)
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
}
'
)
def
_call
(
command
:
str
):
if
(
return_code
:=
call
(
command
,
shell
=
True
))
!=
0
:
exit
(
return_code
)
PROJECT_SLUG
=
'python-313'
VERSION
=
'3.13'
def
recreate_tx_config
():
"""
Regenerate Transifex client config for all resources.
"""
with
TemporaryDirectory
()
as
directory
:
with
chdir
(
directory
):
_clone_cpython_repo
(
VERSION
)
_build_gettext
()
with
chdir
(
Path
(
directory
)
/
'cpython/Doc/build'
):
_create_txconfig
()
_update_txconfig_resources
()
with
open
(
'.tx/config'
,
'r'
)
as
file
:
contents
=
file
.
read
()
contents
=
contents
.
replace
(
'./<lang>/LC_MESSAGES/'
,
''
)
with
open
(
'.tx/config'
,
'w'
)
as
file
:
file
.
write
(
contents
)
warn_about_files_to_delete
()
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
)
}
.'
)
def
_get_files_to_delete
():
with
open
(
'.tx/config'
)
as
config_file
:
config
=
config_file
.
read
()
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'
)
def
_build_gettext
():
_call
(
"make -C cpython/Doc/ gettext"
)
def
_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'
)
@
dataclass
class
ResourceLanguageStatistics
:
name
:
str
total_words
:
int
translated_words
:
int
total_strings
:
int
translated_strings
:
int
@
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
}
'
),
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
:
transifex_api_key
=
f
.
read
()
else
:
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
()
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
}
'
).
all
()
return
[
ResourceLanguageStatistics
.
from_api_entry
(
entry
)
for
entry
in
resources
]
def
progress_from_resources
(
resources
:
Iterable
[
ResourceLanguageStatistics
])
->
float
:
pairs
=
((
e
.
translated_words
,
e
.
total_words
)
for
e
in
resources
)
translated_total
,
total_total
=
(
sum
(
counts
)
for
counts
in
zip
(
*
pairs
))
return
translated_total
/
total_total
*
100
def
get_number_of_translators
():
translators
=
set
(
_fetch_translators
())
_remove_bot
(
translators
)
translators
=
_eliminate_aliases
(
translators
)
return
len
(
translators
)
def
_fetch_translators
()
->
Generator
[
str
,
None
,
None
]:
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
(
', '
)
yield
translator
def
_remove_bot
(
translators
:
set
[
str
])
->
None
:
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
())
>
0.64
:
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'
)
return
any
(
entry
.
name
.
startswith
(
prefix
)
for
prefix
in
language_switcher_resources_prefixes
)
def
generate_commit_msg
():
"""Generate a commit message
Parses staged files and generates a commit message with Last-Translator's as
co-authors.
"""
translators
:
set
[
str
]
=
set
()
result
=
run
(
[
'git'
,
'diff'
,
'--cached'
,
'--name-only'
,
'--diff-filter=ACM'
],
capture_output
=
True
,
text
=
True
,
check
=
True
,
)
staged
=
[
filename
for
filename
in
result
.
stdout
.
splitlines
()
if
filename
.
endswith
(
'.po'
)
]
for
file
in
staged
:
staged_file
=
run
(
[
'git'
,
'show'
,
f':
{
file
}
'
],
capture_output
=
True
,
text
=
True
,
check
=
True
).
stdout
try
:
old_file
=
run
(
[
'git'
,
'show'
,
f'HEAD:
{
file
}
'
],
capture_output
=
True
,
text
=
True
,
check
=
True
,
).
stdout
except
CalledProcessError
:
old_file
=
''
new_po
=
pofile
(
staged_file
)
old_po
=
pofile
(
old_file
)
if
old_file
else
POFile
()
old_entries
=
{
entry
.
msgid
:
entry
.
msgstr
for
entry
in
old_po
}
for
entry
in
new_po
:
if
entry
.
msgstr
and
(
entry
.
msgid
not
in
old_entries
or
old_entries
[
entry
.
msgid
]
!=
entry
.
msgstr
):
translator
=
new_po
.
metadata
.
get
(
'Last-Translator'
)
translator
=
translator
.
split
(
','
)[
0
].
strip
()
if
translator
:
translators
.
add
(
f'Co-Authored-By:
{
translator
}
'
)
break
print
(
'Update translation from Transifex
\n
\n
'
+
'
\n
'
.
join
(
translators
))
if
__name__
==
"__main__"
:
RUNNABLE_SCRIPTS
=
(
'fetch'
,
'recreate_tx_config'
,
'warn_about_files_to_delete'
,
'generate_commit_msg'
,
)
parser
=
ArgumentParser
()
parser
.
add_argument
(
'cmd'
,
choices
=
RUNNABLE_SCRIPTS
)
options
=
parser
.
parse_args
()
eval
(
options
.
cmd
)()
Footer
© 2026 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Community
Docs
Contact
Manage cookies
Do not share my personal information
You cant perform that action at this time.
Web Proxy Viewer |
New URL
|
Original Page