Skip to content

Commit 8fad8ad

Browse files
committed
Simplify scripts and logic
1 parent 30efbba commit 8fad8ad

File tree

5 files changed

+196
-141
lines changed

5 files changed

+196
-141
lines changed

.github/workflows/docbuild-and-upload.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
run: python -m pytest web/
5555

5656
- name: Build website
57-
run: python web/pandas_web.py web/pandas --target-path=web/build
57+
run: python web/pandas_web.py web/pandas --target-path=web/build --translations
5858

5959
- name: Build documentation
6060
run: doc/make.py --warnings-are-errors

web/pandas/config.yml

-33
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,6 @@ static:
2525
css:
2626
- static/css/pandas.css
2727
- static/css/codehilite.css
28-
navbar:
29-
- name: "About us"
30-
target:
31-
- name: "About pandas"
32-
target: about/
33-
- name: "Project roadmap"
34-
target: about/roadmap.html
35-
- name: "Governance"
36-
target: about/governance.html
37-
- name: "Team"
38-
target: about/team.html
39-
- name: "Sponsors"
40-
target: about/sponsors.html
41-
- name: "Citing and logo"
42-
target: about/citing.html
43-
- name: "Getting started"
44-
target: getting_started.html
45-
- name: "Documentation"
46-
target: docs/
47-
- name: "Community"
48-
target:
49-
- name: "Blog"
50-
target: community/blog/
51-
- name: "Ask a question (StackOverflow)"
52-
target: https://stackoverflow.com/questions/tagged/pandas
53-
- name: "Code of conduct"
54-
target: community/coc.html
55-
- name: "Ecosystem"
56-
target: community/ecosystem.html
57-
- name: "Benchmarks"
58-
target: community/benchmarks.html
59-
- name: "Contribute"
60-
target: contribute.html
6128
blog:
6229
num_posts: 50
6330
posts_path: community/blog

web/pandas/navbar.yml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
navbar:
2+
- name: "About us"
3+
target:
4+
- name: "About pandas"
5+
target: about/
6+
- name: "Project roadmap"
7+
target: about/roadmap.html
8+
- name: "Governance"
9+
target: about/governance.html
10+
- name: "Team"
11+
target: about/team.html
12+
- name: "Sponsors"
13+
target: about/sponsors.html
14+
- name: "Citing and logo"
15+
target: about/citing.html
16+
- name: "Getting started"
17+
target: getting_started.html
18+
- name: "Documentation"
19+
target: docs/
20+
- name: "Community"
21+
target:
22+
- name: "Blog"
23+
target: community/blog/
24+
- name: "Ask a question (StackOverflow)"
25+
target: https://stackoverflow.com/questions/tagged/pandas
26+
- name: "Code of conduct"
27+
target: community/coc.html
28+
- name: "Ecosystem"
29+
target: community/ecosystem.html
30+
- name: "Benchmarks"
31+
target: community/benchmarks.html
32+
- name: "Contribute"
33+
target: contribute.html

web/pandas_translations.py

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Utilities to download and extract translations from the GitHub repository.
4+
"""
5+
6+
import io
7+
import os
8+
import shutil
9+
from subprocess import (
10+
PIPE,
11+
Popen,
12+
)
13+
import sys
14+
import tarfile
15+
16+
import requests
17+
import yaml
18+
19+
20+
def get_config(config_fname: str) -> dict:
21+
"""
22+
Load the config yaml file and return it as a dictionary.
23+
"""
24+
with open(config_fname, encoding="utf-8") as f:
25+
context = yaml.safe_load(f)
26+
return context
27+
28+
29+
def download_and_extract_translations(url: str, dir_name: str) -> None:
30+
"""
31+
Download the translations from the GitHub repository.
32+
"""
33+
shutil.rmtree(dir_name, ignore_errors=True)
34+
response = requests.get(url)
35+
if response.status_code == 200:
36+
doc = io.BytesIO(response.content)
37+
with tarfile.open(None, "r:gz", doc) as tar:
38+
tar.extractall(dir_name)
39+
else:
40+
raise Exception(f"Failed to download translations: {response.status_code}")
41+
42+
43+
def get_languages(source_path: str) -> list[str]:
44+
"""
45+
Get the list of languages available in the translations directory.
46+
"""
47+
en_path = f"{source_path}/en/"
48+
if os.path.exists(en_path):
49+
shutil.rmtree(en_path)
50+
51+
paths = os.listdir(source_path)
52+
return [path for path in paths if os.path.isdir(f"{source_path}/{path}")]
53+
54+
55+
def remove_translations(source_path: str, languages: list[str]) -> None:
56+
"""
57+
Remove the translations from the source path.
58+
"""
59+
for language in languages:
60+
shutil.rmtree(os.path.join(source_path, language), ignore_errors=True)
61+
62+
63+
def copy_translations(source_path: str, target_path: str, languages: list[str]) -> None:
64+
"""
65+
Copy the translations to the appropriate directory.
66+
"""
67+
for lang in languages:
68+
dest = f"{target_path}/{lang}/"
69+
shutil.rmtree(dest, ignore_errors=True)
70+
cmds = [
71+
"rsync",
72+
"-av",
73+
"--delete",
74+
f"{source_path}/{lang}/",
75+
dest,
76+
]
77+
p = Popen(cmds, stdout=PIPE, stderr=PIPE)
78+
stdout, stderr = p.communicate()
79+
sys.stderr.write(f"\nCopying: {lang}...\n\n")
80+
sys.stderr.write(stdout.decode())
81+
sys.stderr.write(stderr.decode())
82+
83+
84+
def process_translations(
85+
config_fname: str, source_path: str, process_translations: bool
86+
) -> tuple[list[str], list[str]]:
87+
"""
88+
Process the translations by downloading and extracting them from
89+
the GitHub repository.
90+
"""
91+
base_folder = os.path.dirname(__file__)
92+
config = get_config(os.path.join(source_path, config_fname))
93+
translations_path = os.path.join(base_folder, f"{config['translations']['folder']}")
94+
translations_source_path = os.path.join(
95+
translations_path, config["translations"]["source_path"]
96+
)
97+
default_language = config["translations"]["default_language"]
98+
sys.stderr.write("\nDownloading and extracting translations...\n\n")
99+
download_and_extract_translations(config["translations"]["url"], translations_path)
100+
languages = [default_language]
101+
translated_languages = get_languages(translations_source_path)
102+
remove_translations(source_path, translated_languages)
103+
if process_translations:
104+
languages = [default_language] + translated_languages
105+
106+
sys.stderr.write("\nCopying translations...\n")
107+
copy_translations(translations_source_path, source_path, translated_languages)
108+
109+
return translated_languages, languages

0 commit comments

Comments
 (0)