Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1fbd5d0

Browse files
committed
Replace os with pahlib in babe_runner.py
1 parent 7878f4e commit 1fbd5d0

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

babel_runner.py

+15-18
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,32 @@
33
from __future__ import annotations
44

55
import argparse
6-
import os
76
import subprocess
7+
from pathlib import Path
88

99
import tomllib
1010

11-
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
11+
PROJECT_DIR = Path(__file__).resolve().parent
1212

13-
# Global variables used by pybabel below
13+
# Global variables used by pybabel below (paths relative to PROJECT_DIR)
1414
DOMAIN = "messages"
1515
COPYRIGHT_HOLDER = "Python Software Foundation"
16-
LOCALES_DIR = os.path.relpath(os.path.join(PROJECT_DIR, "locales"))
17-
POT_FILE = os.path.relpath(os.path.join(LOCALES_DIR, f"{DOMAIN}.pot"), PROJECT_DIR)
18-
SOURCE_DIR = os.path.relpath(
19-
os.path.join(PROJECT_DIR, "python_docs_theme"), PROJECT_DIR
20-
)
21-
MAPPING_FILE = os.path.relpath(os.path.join(PROJECT_DIR, ".babel.cfg"), PROJECT_DIR)
16+
LOCALES_DIR = "locales"
17+
POT_FILE = Path(LOCALES_DIR, f"{DOMAIN}.pot")
18+
SOURCE_DIR = "python_docs_theme"
19+
MAPPING_FILE = ".babel.cfg"
2220

2321

2422
def get_project_info() -> dict:
2523
"""Retrieve project's info to populate the message catalog template"""
26-
with open(os.path.join(PROJECT_DIR, "pyproject.toml"), "rb") as f:
24+
with open(Path(PROJECT_DIR / "pyproject.toml"), "rb") as f:
2725
data = tomllib.load(f)
2826
return data["project"]
2927

3028

3129
def extract_messages():
3230
"""Extract messages from all source files into message catalog template"""
33-
os.makedirs(LOCALES_DIR, exist_ok=True)
31+
Path(PROJECT_DIR, LOCALES_DIR).mkdir(parents=True, exist_ok=True)
3432
project_data = get_project_info()
3533
subprocess.run(
3634
[
@@ -50,34 +48,35 @@ def extract_messages():
5048
POT_FILE,
5149
SOURCE_DIR,
5250
],
51+
cwd=PROJECT_DIR,
5352
check=True,
5453
)
5554

5655

5756
def init_locale(locale: str):
5857
"""Initialize a new locale based on existing message catalog template"""
59-
pofile = os.path.join(LOCALES_DIR, locale, "LC_MESSAGES", f"{DOMAIN}.po")
60-
if os.path.exists(pofile):
58+
pofile = PROJECT_DIR / LOCALES_DIR / locale / "LC_MESSAGES" / f"{DOMAIN}.po"
59+
if pofile.exists():
6160
print(f"There is already a message catalog for locale {locale}, skipping.")
6261
return
6362
cmd = ["pybabel", "init", "-i", POT_FILE, "-d", LOCALES_DIR, "-l", locale]
64-
subprocess.run(cmd, check=True)
63+
subprocess.run(cmd, cwd=PROJECT_DIR, check=True)
6564

6665

6766
def update_catalogs(locale: str):
6867
"""Update translations from existing message catalogs"""
6968
cmd = ["pybabel", "update", "-i", POT_FILE, "-d", LOCALES_DIR]
7069
if locale != "":
7170
cmd.extend(["-l", locale])
72-
subprocess.run(cmd, check=True)
71+
subprocess.run(cmd, cwd=PROJECT_DIR, check=True)
7372

7473

7574
def compile_catalogs(locale: str):
7675
"""Compile existing message catalogs"""
7776
cmd = ["pybabel", "compile", "-d", LOCALES_DIR]
7877
if locale != "":
7978
cmd.extend(["-l", locale])
80-
subprocess.run(cmd, check=True)
79+
subprocess.run(cmd, cwd=PROJECT_DIR, check=True)
8180

8281

8382
def main():
@@ -96,8 +95,6 @@ def main():
9695
args = parser.parse_args()
9796
locale = args.locale if args.locale else ""
9897

99-
os.chdir(PROJECT_DIR)
100-
10198
if args.command == "extract":
10299
extract_messages()
103100
elif args.command == "init":

0 commit comments

Comments
 (0)