Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1fbd5d0

Browse files
committed
Replace os with pahlib in babe_runner.py
1 parent7878f4e commit1fbd5d0

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

‎babel_runner.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,32 @@
33
from __future__importannotations
44

55
importargparse
6-
importos
76
importsubprocess
7+
frompathlibimportPath
88

99
importtomllib
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
defget_project_info()->dict:
2523
"""Retrieve project's info to populate the message catalog template"""
26-
withopen(os.path.join(PROJECT_DIR,"pyproject.toml"),"rb")asf:
24+
withopen(Path(PROJECT_DIR/"pyproject.toml"),"rb")asf:
2725
data=tomllib.load(f)
2826
returndata["project"]
2927

3028

3129
defextract_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
definit_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-
ifos.path.exists(pofile):
58+
pofile=PROJECT_DIR/LOCALES_DIR/locale/"LC_MESSAGES"/f"{DOMAIN}.po"
59+
ifpofile.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
defupdate_catalogs(locale:str):
6867
"""Update translations from existing message catalogs"""
6968
cmd= ["pybabel","update","-i",POT_FILE,"-d",LOCALES_DIR]
7069
iflocale!="":
7170
cmd.extend(["-l",locale])
72-
subprocess.run(cmd,check=True)
71+
subprocess.run(cmd,cwd=PROJECT_DIR,check=True)
7372

7473

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

8281

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

99-
os.chdir(PROJECT_DIR)
100-
10198
ifargs.command=="extract":
10299
extract_messages()
103100
elifargs.command=="init":

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp