-
-
Notifications
You must be signed in to change notification settings - Fork 81
Update to Python 3.12 #301
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
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a67273b
Update to Python 3.11
BlueGlassBlock 8602d5e
Update python-311.yml
BlueGlassBlock 3554fc2
Fix transifex cli position
BlueGlassBlock c259ee1
Fix config generation script
BlueGlassBlock 6d03983
Add 3.11 to workflows
BlueGlassBlock c3d4b1a
chore: update 3.11 links and badges
BlueGlassBlock e01effb
chore: Add 3.12 to workflow
BlueGlassBlock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,82 @@ | ||
#!/usr/bin/env python3 | ||
"""Please note that this script requires a Transifex API token to run.""" | ||
import glob | ||
import json | ||
import os | ||
import subprocess | ||
from functools import partial | ||
from pathlib import Path | ||
import re | ||
import sys | ||
import urllib.request | ||
|
||
run = partial(subprocess.run, check=True) | ||
|
||
def list_resources(token, project): | ||
auth_handler = urllib.request.HTTPBasicAuthHandler() | ||
auth_handler.add_password( | ||
realm="api", uri="https://api.transifex.com/", user="api", passwd=token | ||
|
||
def init_project(): | ||
run(["tx", "init"]) | ||
|
||
|
||
def add_files(version: str): | ||
run( | ||
[ | ||
"tx", | ||
"add", | ||
"remote", | ||
"--file-filter", | ||
"trans/<lang>/<resource_slug>.<ext>", | ||
f"https://www.transifex.com/python-doc/{version}/dashboard/", | ||
] | ||
) | ||
opener = urllib.request.build_opener(auth_handler) | ||
urllib.request.install_opener(opener) | ||
next_ = ( | ||
"https://api.transifex.com/organizations/python-doc/projects/" | ||
+ project | ||
+ "/resources/" | ||
|
||
|
||
FILTER_PATTERN = re.compile( | ||
r"^(?P<prefix>file_filter( *)=( *))(?P<resource>.+)$", re.MULTILINE | ||
) | ||
|
||
|
||
def name_replacer(match: re.Match[str]): | ||
prefix, resource = match.group("prefix", "resource") | ||
override_prefix = prefix.replace("file_filter", "trans.zh_CN") | ||
pattern = ( | ||
resource.replace("trans/<lang>/", "") | ||
.replace("glossary_", "glossary") | ||
.replace("--", "/") | ||
.replace("_", "?") | ||
) | ||
resources = [] | ||
while True: | ||
resp = urllib.request.urlopen(next_) | ||
result = json.loads(resp.read().decode("utf-8")) | ||
resources.extend([i["slug"] for i in result]) | ||
link = re.findall('<([^<]*)>; rel="next"', resp.getheader("Link") or "") | ||
if not link: | ||
break | ||
next_ = link[0] | ||
return resources | ||
|
||
|
||
def render_config(doc_dir, project, resources): | ||
os.chdir(doc_dir) | ||
tpl = """ | ||
|
||
[{project}.{resource}] | ||
trans.zh_CN = {filename} | ||
source_lang = en | ||
type = PO""" | ||
conf = """[main] | ||
host = https://www.transifex.com""" | ||
for resource in sorted(resources): | ||
if resource == "glossary_": | ||
filename = "glossary.po" | ||
elif resource == "sphinx": | ||
filename = "sphinx.po" | ||
else: | ||
pattern = resource.replace("--", "/").replace("_", "?") | ||
matches = glob.glob(pattern + ".rst") | ||
if len(matches) == 0: | ||
print("missing", resource, file=sys.stderr) | ||
continue | ||
elif len(matches) == 1: | ||
filename = matches[0].replace(".rst", ".po") | ||
else: | ||
print("multi match", resource, pattern, matches, file=sys.stderr) | ||
conf += tpl.format(project=project, resource=resource, filename=filename) | ||
return conf | ||
matches = list(glob.glob(pattern.replace(".po", ".rst"))) | ||
if not matches: | ||
print("missing", pattern) | ||
return f"{prefix}{resource}\n{override_prefix}{pattern}" | ||
elif len(matches) == 1: | ||
filename = matches[0].replace(".rst", ".po").replace("\\", "/") | ||
else: | ||
raise ValueError("multi match", resource, pattern, matches) | ||
return f"{prefix}{resource}\n{override_prefix}{filename}" | ||
|
||
|
||
def patch_config(path: str): | ||
tx_config_path = Path(".tx", "config") | ||
|
||
config_content = tx_config_path.read_text("utf-8") | ||
|
||
cwd = os.getcwd() | ||
os.chdir(path) | ||
config_content = FILTER_PATTERN.sub(name_replacer, config_content) | ||
os.chdir(cwd) | ||
|
||
tx_config_path.write_text(config_content, "utf-8") | ||
|
||
|
||
if __name__ == "__main__": | ||
import argparse | ||
from argparse import ArgumentParser | ||
import os | ||
|
||
parser = ArgumentParser() | ||
|
||
parser.add_argument("--token", required=True) | ||
parser.add_argument("--version", required=True) | ||
parser.add_argument("--doc-path", required=True) | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--token") | ||
parser.add_argument("--project") | ||
parser.add_argument("--doc-dir") | ||
args = parser.parse_args() | ||
params = parser.parse_args() | ||
|
||
resources = list_resources(args.token, args.project) | ||
conf = render_config(args.doc_dir, args.project, resources) | ||
print(conf) | ||
os.environ["TX_TOKEN"] = params.token | ||
|
||
# vim: set et ts=4 sw=4 sts=4: | ||
init_project() | ||
add_files(params.version) | ||
patch_config(params.doc_path) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,5 @@ | ||
#!/bin/bash | ||
|
||
set -ex | ||
|
||
script_dir="$(dirname "$(realpath "$0")")" | ||
|
||
if [[ -n "$TRANSIFEX_APIKEY" ]]; then | ||
cat > ~/.transifexrc << EOF | ||
[https://www.transifex.com] | ||
api_hostname = https://api.transifex.com | ||
hostname = https://www.transifex.com | ||
password = $TRANSIFEX_APIKEY | ||
username = api | ||
EOF | ||
fi | ||
|
||
tx=$(realpath ./tx) | ||
cd docs || exit 1 | ||
"$script_dir"/transifex_pull.py | ||
$tx pull --languages "$LOCALE" -t --use-git-timestamps |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: python-311 | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
schedule: | ||
- cron: "11 * * * *" | ||
|
||
jobs: | ||
sync: | ||
runs-on: ubuntu-latest | ||
env: | ||
LOCALE: zh_CN | ||
VERSION: "3.11" | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: prepare | ||
run: .github/scripts/prepare.sh | ||
- name: update | ||
run: .github/scripts/update.sh | ||
env: | ||
TX_TOKEN: ${{ secrets.TRANSIFEX_APIKEY }} | ||
- name: build | ||
run: .github/scripts/build.sh | ||
- name: commit | ||
run: .github/scripts/commit.sh | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.