3
3
from __future__ import annotations
4
4
5
5
import argparse
6
- import os
7
6
import subprocess
7
+ from pathlib import Path
8
8
9
9
import tomllib
10
10
11
- PROJECT_DIR = os . path . dirname ( os . path . abspath ( __file__ ))
11
+ PROJECT_DIR = Path ( __file__ ). resolve (). parent
12
12
13
- # Global variables used by pybabel below
13
+ # Global variables used by pybabel below (paths relative to PROJECT_DIR)
14
14
DOMAIN = "messages"
15
15
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"
22
20
23
21
24
22
def get_project_info () -> dict :
25
23
"""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 :
27
25
data = tomllib .load (f )
28
26
return data ["project" ]
29
27
30
28
31
29
def extract_messages ():
32
30
"""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 )
34
32
project_data = get_project_info ()
35
33
subprocess .run (
36
34
[
@@ -50,34 +48,35 @@ def extract_messages():
50
48
POT_FILE ,
51
49
SOURCE_DIR ,
52
50
],
51
+ cwd = PROJECT_DIR ,
53
52
check = True ,
54
53
)
55
54
56
55
57
56
def init_locale (locale : str ):
58
57
"""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 ():
61
60
print (f"There is already a message catalog for locale { locale } , skipping." )
62
61
return
63
62
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 )
65
64
66
65
67
66
def update_catalogs (locale : str ):
68
67
"""Update translations from existing message catalogs"""
69
68
cmd = ["pybabel" , "update" , "-i" , POT_FILE , "-d" , LOCALES_DIR ]
70
69
if locale != "" :
71
70
cmd .extend (["-l" , locale ])
72
- subprocess .run (cmd , check = True )
71
+ subprocess .run (cmd , cwd = PROJECT_DIR , check = True )
73
72
74
73
75
74
def compile_catalogs (locale : str ):
76
75
"""Compile existing message catalogs"""
77
76
cmd = ["pybabel" , "compile" , "-d" , LOCALES_DIR ]
78
77
if locale != "" :
79
78
cmd .extend (["-l" , locale ])
80
- subprocess .run (cmd , check = True )
79
+ subprocess .run (cmd , cwd = PROJECT_DIR , check = True )
81
80
82
81
83
82
def main ():
@@ -96,8 +95,6 @@ def main():
96
95
args = parser .parse_args ()
97
96
locale = args .locale if args .locale else ""
98
97
99
- os .chdir (PROJECT_DIR )
100
-
101
98
if args .command == "extract" :
102
99
extract_messages ()
103
100
elif args .command == "init" :
0 commit comments