1
1
"""Please note that this script requires a Transifex API token to run."""
2
+ import glob
2
3
import subprocess
3
4
from functools import partial
4
5
from pathlib import Path
@@ -32,20 +33,33 @@ def add_files(version: str):
32
33
def name_replacer (match : re .Match [str ]):
33
34
prefix , resource = match .group ("prefix" , "resource" )
34
35
override_prefix = prefix .replace ("file_filter" , "trans.zh_CN" )
35
- override_resource = (
36
+ pattern = (
36
37
resource .replace ("trans/<lang>/" , "" )
37
- .replace ("--" , "/" )
38
38
.replace ("glossary_" , "glossary" )
39
- .replace ("_" , "." )
39
+ .replace ("--" , "/" )
40
+ .replace ("_" , "?" )
40
41
)
41
- return f"{ prefix } { resource } \n { override_prefix } { override_resource } "
42
-
43
-
44
- def patch_config ():
42
+ matches = list (glob .glob (pattern .replace (".po" , ".rst" )))
43
+ if not matches :
44
+ print ("missing" , pattern )
45
+ return f"{ prefix } { resource } \n { override_prefix } { pattern } "
46
+ elif len (matches ) == 1 :
47
+ filename = matches [0 ].replace (".rst" , ".po" ).replace ("\\ " , "/" )
48
+ else :
49
+ raise ValueError ("multi match" , resource , pattern , matches )
50
+ return f"{ prefix } { resource } \n { override_prefix } { filename } "
51
+
52
+
53
+ def patch_config (path : str ):
45
54
tx_config_path = Path (".tx" , "config" )
46
55
47
56
config_content = tx_config_path .read_text ("utf-8" )
57
+
58
+ cwd = os .getcwd ()
59
+ os .chdir (path )
48
60
config_content = FILTER_PATTERN .sub (name_replacer , config_content )
61
+ os .chdir (cwd )
62
+
49
63
tx_config_path .write_text (config_content , "utf-8" )
50
64
51
65
@@ -55,13 +69,14 @@ def patch_config():
55
69
56
70
parser = ArgumentParser ()
57
71
58
- parser .add_argument ("--token" )
59
- parser .add_argument ("--version" )
72
+ parser .add_argument ("--token" , required = True )
73
+ parser .add_argument ("--version" , required = True )
74
+ parser .add_argument ("--doc-path" , required = True )
60
75
61
76
params = parser .parse_args ()
62
77
63
78
os .environ ["TX_TOKEN" ] = params .token
64
79
65
80
init_project ()
66
81
add_files (params .version )
67
- patch_config ()
82
+ patch_config (params . doc_path )
0 commit comments